1. Go to this page and download the library: Download omnilog/psr6-dynamo-db library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
omnilog / psr6-dynamo-db example snippets
use Omnilog\DynamoDbCache\DynamoDbCache;
use Omnilog\DynamoDbCache\DynamoDbCacheBuilder;
use AsyncAws\DynamoDb\DynamoDbClient;
$cache = new DynamoDbCache('dynamoTableName', new DynamoDbClient([]));
// with custom field names
$cache = new DynamoDbCache('dynamoTableName', new DynamoDbClient([]), 'customPrimaryKeyField', 'customTtlField', 'customValueField');
// using builder
$cache = DynamoDbCacheBuilder::create('dynamoTableName', new DynamoDbClient([]))
->withPrimaryField('customPrimaryKeyField')
->withTtlField('customTtlField')
->withValueField('customValueField')
->build();
use AsyncAws\DynamoDb\DynamoDbClient;
use Omnilog\DynamoDbCache\DynamoDbCache;
function get(string $key): string
{
$dynamoDbClient = new DynamoDbClient([
'region' => 'eu-central-1',
'version' => 'latest'
]);
$cache = new DynamoDbCache('cache', $dynamoDbClient); // the default field names are used - id, ttl and value
$item = $cache->getItem($key);
if ($item->isHit()) {
return $item->get();
}
// do something to fetch the item
$result = '...';
$item->set($result);
$item->expiresAfter(3600); // expire after one hour
if (!$cache->save($item)) {
throw new RuntimeException('Could not save cache');
}
return $result;
}
use AsyncAws\DynamoDb\DynamoDbClient;
use Omnilog\DynamoDbCache\DynamoDbCache;
function get(string $key): string
{
$dynamoDbClient = new DynamoDbClient([
'region' => 'eu-central-1',
'version' => 'latest'
]);
$cache = new DynamoDbCache('cache', $dynamoDbClient); // the default field names are used - id, ttl and value
$value = $cache->get($key);
if ($value !== null) {
return $value;
}
// do something to fetch the item
$result = '...';
if (!$cache->set($key, $result, 3600)) {
throw new RuntimeException('Could not save cache');
}
return $result;
}
use Omnilog\DynamoDbCache\DynamoDbCacheBuilder;
use AsyncAws\DynamoDb\DynamoDbClient;
$cache = DynamoDbCacheBuilder::create('myTable', new DynamoDbClient([]))
->withPrefix('myCustomPrefix#')
->build();
$item = $cache->getItem('key1'); // fetches an item with key myCustomPrefix#key1
$key = $item->getKey(); // $key holds the full key including prefix, myCustomPrefix#key1
use Omnilog\DynamoDbCache\Converter\CacheItemConverterInterface;
use Psr\Cache\CacheItemInterface;
use Omnilog\DynamoDbCache\DynamoCacheItem;
use Omnilog\DynamoDbCache\Encoder\SerializeItemEncoder;
class MyCacheItemConverter implements CacheItemConverterInterface
{
/**
* If this methods returns true, the converter will be used
*/
public function supports(CacheItemInterface $cacheItem): bool
{
return $cacheItem instanceof MyCacheItem;
}
public function convert(CacheItemInterface $cacheItem): DynamoCacheItem
{
assert($cacheItem instanceof MyCacheItem);
return new DynamoCacheItem(
$cacheItem->getKey(),
$cacheItem->isHit(),
$cacheItem->get(),
$cacheItem->getExpirationDate(), // this is a custom method from the hypothetical MyCacheItem
new SerializeItemEncoder()
);
}
}
use Omnilog\DynamoDbCache\Converter\CacheItemConverterRegistry;
use Omnilog\DynamoDbCache\DynamoDbCache;
use AsyncAws\DynamoDb\DynamoDbClient;
use Omnilog\DynamoDbCache\DynamoDbCacheBuilder;
// you don't need to add the default one as well, it will be added automatically if it's missing
$converter = new CacheItemConverterRegistry(new MyCacheItemConverter());
$dynamoClient = new DynamoDbClient([]);
$cache = DynamoDbCacheBuilder::create('myTable', $dynamoClient)
->withConverterRegistry($converter)
->build();
$myOldCache = new MyCacheImplementation();
$cacheItem = $myOldCache->getItem('test'); // this is now an instance of MyCacheItem
// your custom converter will get used to convert it to DynamoCacheItem
// if you didn't supply your own converter, the \Omnilog\DynamoDbCache\Converter\DefaultCacheItemConverter
// would be used and the information about expiration date would be lost
$cache->save($cacheItem);
use Omnilog\DynamoDbCache\DynamoDbCache;
use Omnilog\DynamoDbCache\DynamoDbCacheBuilder;
use AsyncAws\DynamoDb\DynamoDbClient;
use Omnilog\DynamoDbCache\Encoder\JsonItemEncoder;
$encoder = new JsonItemEncoder(); // with default flags and depth
$encoder = new JsonItemEncoder(JSON_PRETTY_PRINT, JSON_THROW_ON_ERROR, 100); // with custom encode and decode flags and depth
$cache = DynamoDbCacheBuilder::create('myTable', new DynamoDbClient([]))
->withEncoder($encoder)
->build();
use Omnilog\DynamoDbCache\Encoder\CacheItemEncoderInterface;
class MyEncoder implements CacheItemEncoderInterface
{
/**
* @param mixed $input
* @return string
*/
public function encode($input) : string
{
// TODO: Implement encode() method.
}
/**
* @param string $input
* @return mixed
*/
public function decode(string $input)
{
// TODO: Implement decode() method.
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.