PHP code example of omnilog / psr6-dynamo-db-bundle

1. Go to this page and download the library: Download omnilog/psr6-dynamo-db-bundle 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-bundle example snippets




use Omnilog\DynamoDbCacheBundle\Cache\DynamoDbCacheAdapter;
use Omnilog\DynamoDbCache\DynamoDbCache;
use Symfony\Contracts\Cache\ItemInterface;

class MyService
{
    public function __construct(DynamoDbCache $cache, DynamoDbCacheAdapter $adapter)
    {
        // it doesn't matter whether you use the adapter or not, the usage for PSR-6 is the same, the
        // only difference is that adapter implements the Symfony interface and thus you can
        // use it to replace the default AdapterInterface implementation
        $item = $cache->getItem('test');
        $item2 = $adapter->getItem('test');
    
        $item->set('some value');
        $adapter->save($item);

        // or using the CacheInterface api
        $value = $adapter->get('test', function (ItemInterface $item) {
            $item->expiresAfter(3600);
            return 'new-cache-value';
        });

        $cache->delete('test');
    }
}



use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;

class MyService
{
    public function __construct(AdapterInterface $cache)
    {
        $item = $cache->getItem('test');
        // do stuff with cache item
        $cache->save($item);
    }
}

class MyService2
{
    public function __construct(CacheInterface $cache)
    {
        $cache->get('test', function (ItemInterface $item) {
            return 'new-value';
        });
    }
}



use Psr\Cache\CacheItemPoolInterface;
use Psr\SimpleCache\CacheInterface;

class MyService
{
    public function __construct(CacheItemPoolInterface $psr6cache, CacheInterface $psr16cache)
    {
        $item = $psr6cache->getItem('test');
        $value = $item->get();
        $item->set('newValue');
        $item->expiresAfter(120);
        $psr6cache->save($item);
    
        $value = $psr16cache->get('test');
        $psr16cache->set('test', 'newValue', 120);
    }
}



use Omnilog\DynamoDbCache\Converter\CacheItemConverterInterface;
use Psr\Cache\CacheItemInterface;
use Omnilog\DynamoDbCache\DynamoCacheItem;

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
        );
    }
}