mgleis / php-sqlite-key-value-store
A PHP library that implements a key/value store with SQLite as the persistence layer.
Package info
github.com/mgleis/php-sqlite-key-value-store
pkg:composer/mgleis/php-sqlite-key-value-store
Requires
- php: >=7.4
README
A PHP library that implements a key/value store with SQLite as the persistence layer.
Use it for small projects / prototypes with at most hundreds or thousands of values.
The library now includes RPC server and client capabilities for remote access over HTTP.
Install
composer require mgleis/php-sqlite-key-value-store
Usage
Basic Local Usage
$kv = new KeyValueStore();
Set a Key
$kv->set('key', 'value');
Get a Value by Key
$value = $kv->get('key');
Check if a key exists
$exists = $kv->has('key');
Delete by Key
$kv->delete('key');
Search by Value
$arr = $kv->searchValueStartsWith('text');
$arr = $kv->searchValueContains('text');
Search by Key
$arr = $kv->searchKeyStartsWith('text');
$arr = $kv->searchKeyContains('text');
RPC Server and Client
Starting an RPC Server
You can expose your KeyValueStore via HTTP using the KeyValueStoreRpcServer:
use Mgleis\PhpSqliteKeyValueStore\KeyValueStoreRpcServer; $server = new KeyValueStoreRpcServer('/rpc'); // Initialize server for route /rpc/* $server->start();
This creates an HTTP server that accepts JSON-RPC requests at endpoints like:
POST /rpc/get- Get a valuePOST /rpc/set- Set a key-value pairPOST /rpc/has- Check if key existsPOST /rpc/delete- Delete a keyPOST /rpc/searchValueContains- Search values containing textPOST /rpc/searchValueStartsWith- Search values starting with textPOST /rpc/searchKeyContains- Search keys containing textPOST /rpc/searchKeyStartsWith- Search keys starting with text
Using the RPC Client
Connect to a remote KeyValueStore server using the client:
use Mgleis\PhpSqliteKeyValueStore\KeyValueStoreRpcClient; $kv = new KeyValueStoreRpcClient('http://myserver.de:8080/rpc'); // Use exactly like the local KeyValueStore $kv->set('key1', 'value1'); echo $kv->get('key1'); echo $kv->has('key1'); $kv->delete('key1'); // Search operations work the same way $arr = $kv->searchValueStartsWith('text'); $arr = $kv->searchValueContains('text'); $arr = $kv->searchKeyStartsWith('text'); $arr = $kv->searchKeyContains('text');
The client implements the same interface as the local KeyValueStore, making it a drop-in replacement for remote access.