PHP code example of nilportugues / repository-cache

1. Go to this page and download the library: Download nilportugues/repository-cache 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/ */

    

nilportugues / repository-cache example snippets


use NilPortugues\Foundation\Infrastructure\Model\Repository\Cache\RepositoryCache;
use Stash\Driver\Ephemeral;
use Stash\Driver\Memcache;
use Stash\Pool;

//---------------------------------------------------------------------------
// Definition of cache drivers
//---------------------------------------------------------------------------
$memcached = new Memcache();
$memcached->setOptions(array('servers' => array('127.0.0.1', '11211')));

$cachePool = new Pool();
$cachePool->setDriver(new Ephemeral()); //hit in memory first as always will be faster
$cachePool->setDriver($memcached); //hit memcached second.

$ttl = 3600; //time in second for cache to expire.
$cacheNamespace = Color::class;

//---------------------------------------------------------------------------
// Adding cache to an existing repository
//---------------------------------------------------------------------------
$sqlRepository = new MySQLColorRepository();
$repository = new RepositoryCache($cachePool, $repository, $cacheNamespace, $ttl);

//---------------------------------------------------------------------------
// Now use as normal... 
//---------------------------------------------------------------------------
$color = new Color('#@@@@@@', 'New color');
$repository->add($color);
$repository->find(ColorId('#@@@@@@')); //should hit cache and return an instance of Color.