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

This package is not auto-updated.

Last update: 2024-10-26 07:59:03 UTC


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