walnut / lib_recordstorage
There is no license information available for the latest version (0.0.4) of this package.
0.0.4
2022-05-13 20:39 UTC
Requires
- walnut/lib_fileaccessor: ^0.0.2
- walnut/lib_jsonserializer: ^0.0.1
- walnut/lib_transactioncontext: ^0.0.1
Requires (Dev)
- phpunit/phpunit: ^9.5.20
- vimeo/psalm: ^4.23.0
README
A key/value based record storage abstraction
Usages
There are several ways to use the Record Storage.
Full example using in-memory storage and PHP serialization
//Create a storage $storage = new SerializedRecordStorage( new PhpArrayDataSerializer, new InMemoryKeyValueStorage ); //An accessor factory for easy access $accessorFactory = new ArrayDataAccessorFactory($recordStorage); //Get the product storage $accessor = $accessorFactory->accessor('products'); //Store some data $accessor->store('product-id-1', [ 'id' => 'product-id-1', 'name' => 'My first product', 'itemsInStock' => 5 ]); count($accessor->all()); //1 //Fetch the data by key $accessor->retreive('product-id-1'); //['id' => ..., 'name' => ...] //Fetch the data by filter $accessor->byFilter( fn(array $entry): bool => $entry['itemsInStock'] > 0 ); //[1 record] //Remove it $accessor->remove('product-id-1'); //
Using a JSON serializer
//Create a storage $storage = new SerializedRecordStorage( new JsonArrayDataSerializer( new JsonSerializer ), new InMemoryKeyValueStorage );
Using an in-file storage
//Create a storage $storage = new SerializedRecordStorage( new JsonArrayDataSerializer( new JsonSerializer ), new InFileKeyValueStorage( new PerFileKeyToFileNameMapper( baseDir: __DIR__ . '/data', fileExtension: 'json' ) ) );
Using cached storage (highly recommended)
$storage = new SerializedRecordStorage(/*...*/); $cacheableStorage = new CacheableRecordStorage($storage)
###More adapters and decorators
- Transaction Context decorator
- Redis Adapter
- ...other