1. Go to this page and download the library: Download tobento/service-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/ */
tobento / service-cache example snippets
use Tobento\Service\Cache\FileStorageCacheItemPool;
use Tobento\Service\Cache\CanDeleteExpiredItems;
use Tobento\Service\Clock\SystemClock;
use Tobento\Service\FileStorage\StorageInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Clock\ClockInterface;
$pool = new FileStorageCacheItemPool(
// Any storage where to store cache items:
storage: $storage, // StorageInterface
// A path used as the path prefix for the storage:
path: 'cache', // string
// The clock used for calculating expiration:
clock: new SystemClock(), // ClockInterface
// The default Time To Live in seconds, null forever:
ttl: null, // null|int
);
var_dump($pool instanceof CacheItemPoolInterface);
// bool(true)
var_dump($pool instanceof CanDeleteExpiredItems);
// bool(true)
use Tobento\Service\Cache\StorageCacheItemPool;
use Tobento\Service\Cache\CanDeleteExpiredItems;
use Tobento\Service\Clock\SystemClock;
use Tobento\Service\Storage\StorageInterface;
use Tobento\Service\Storage\JsonFileStorage;
use Tobento\Service\Storage\Tables\Tables;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Clock\ClockInterface;
// Create the storage:
$tables = new Tables();
$tables->add('cache_items', ['id', 'data', 'expiration', 'namespace'], 'id');
$storage = new JsonFileStorage(
dir: __DIR__.'/tmp/',
tables: $tables
);
$pool = new StorageCacheItemPool(
// Any storage where to store cache items:
storage: $storage, // StorageInterface
// A namespace used as prefix for cache item keys:
namespace: 'default', // string
// The clock used for calculating expiration:
clock: new SystemClock(), // ClockInterface
// The default Time To Live in seconds, null forever:
ttl: null, // null|int
// Specify the table name:
table: 'cache_items', // string
);
var_dump($pool instanceof CacheItemPoolInterface);
// bool(true)
var_dump($pool instanceof CanDeleteExpiredItems);
// bool(true)
use Tobento\Service\Cache\ArrayCacheItemPool;
use Tobento\Service\Clock\SystemClock;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Clock\ClockInterface;
$pool = new ArrayCacheItemPool(
// The clock used for calculating expiration:
clock: new SystemClock(), // ClockInterface
// The default Time To Live in seconds, null forever:
ttl: null, // null|int
);
var_dump($pool instanceof CacheItemPoolInterface);
// bool(true)
use Tobento\Service\Cache\Psr16CacheItemPool;
use Tobento\Service\Cache\CanDeleteExpiredItems;
use Tobento\Service\Clock\SystemClock;
use Psr\SimpleCache\CacheInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Clock\ClockInterface;
$pool = new Psr16CacheItemPool(
cache: $cache, // CacheInterface
// A namespace used as prefix for cache item keys:
namespace: 'default', // string
// The clock used for calculating expiration:
clock: new SystemClock(), // ClockInterface
// The default Time To Live in seconds, null forever:
ttl: null, // null|int
);
var_dump($pool instanceof CacheItemPoolInterface);
// bool(true)
var_dump($pool instanceof CanDeleteExpiredItems);
// bool(true)
use Tobento\Service\Cache\CacheItemPools;
use Tobento\Service\Cache\CacheItemPoolsInterface;
$pools = new CacheItemPools();
var_dump($pools instanceof CacheItemPoolsInterface);
// bool(true)
use Psr\Cache\CacheItemPoolInterface;
$pools->add(
name: 'primary',
pool: $pool, // CacheItemPoolInterface
);
use Psr\Cache\CacheItemPoolInterface;
$pools->register(
name: 'name',
pool: function(string $name): CacheItemPoolInterface {
// create the pool:
return $pool;
},
);
use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\CacheException;
$pool = $pools->get(name: 'file');
var_dump($pool instanceof CacheItemPoolInterface);
// bool(true)
$pools->get(name: 'unknown');
// throws CacheException
use Tobento\Service\Cache\CacheItemPools;
use Tobento\Service\Cache\CacheItemPoolsInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\CacheException;
$pools = new CacheItemPools();
// add "file" pool:
$pools->add(name: 'file', pool: $pool);
// add default:
$pools->addDefault(name: 'primary', pool: 'file');
// get default pool for the specified name.
$primaryPool = $pools->default('primary');
var_dump($primaryPool instanceof CacheItemPoolInterface);
// bool(true)
var_dump($pools->hasDefault('primary'));
// bool(true)
var_dump($pools->getDefaults());
// { ["primary"]=> string(4) "file" }
$pools->default('unknown');
// throws CacheException
namespace Tobento\Service\Cache;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\CacheException;
interface CacheItemPoolFactoryInterface
{
/**
* Create a new CacheItemPool based on the configuration.
*
* @param string $name
* @param array $config
* @return CacheItemPoolInterface
* @throws CacheException
*/
public function createCacheItemPool(string $name, array $config = []): CacheItemPoolInterface;
}
namespace Tobento\Service\Cache;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\CacheException;
interface CacheItemPoolsInterface
{
/**
* Add a pool.
*
* @param string $name The pool name.
* @param CacheItemPoolInterface $pool
* @return static $this
*/
public function add(string $name, CacheItemPoolInterface $pool): static;
/**
* Register a pool.
*
* @param string $name The pool name.
* @param callable $pool
* @return static $this
*/
public function register(string $name, callable $pool): static;
/**
* Returns the pool by name.
*
* @param string $name The pool name
* @return CacheItemPoolInterface
* @throws CacheException
*/
public function get(string $name): CacheItemPoolInterface;
/**
* Returns true if the pool exists, otherwise false.
*
* @param string $name The pool name.
* @return bool
*/
public function has(string $name): bool;
/**
* Adds a default name for the specified pool.
*
* @param string $name The default name.
* @param string $pool The pool name.
* @return static $this
*/
public function addDefault(string $name, string $pool): static;
/**
* Returns the default pools.
*
* @return array<string, string> ['general' => 'files']
*/
public function getDefaults(): array;
/**
* Returns the pool for the specified default name.
*
* @param string $name The type such as pdo.
* @return CacheItemPoolInterface
* @throws CacheException
*/
public function default(string $name): CacheItemPoolInterface;
/**
* Returns true if the default pool exists, otherwise false.
*
* @param string $name The default name.
* @return bool
*/
public function hasDefault(string $name): bool;
/**
* Returns the names.
*
* @return array<int, string>
*/
public function getNames(): array;
}
use Tobento\Service\Cache\Simple\Psr6Cache;
use Tobento\Service\Cache\ArrayCacheItemPool;
use Tobento\Service\Cache\CanDeleteExpiredItems;
use Tobento\Service\Clock\SystemClock;
use Psr\Cache\CacheItemPoolInterface;
use Psr\SimpleCache\CacheInterface;
use Psr\Clock\ClockInterface;
$pool = new ArrayCacheItemPool(
clock: new SystemClock(),
);
$cache = new Psr6Cache(
pool: $pool, // CacheItemPoolInterface
// A namespace used as prefix for cache item keys:
namespace: 'default', // string
// The default Time To Live in seconds, null forever:
ttl: null, // null|int
);
var_dump($cache instanceof CacheInterface);
// bool(true)
var_dump($cache instanceof CanDeleteExpiredItems);
// bool(true)
use Tobento\Service\Cache\Simple\Caches;
use Tobento\Service\Cache\Simple\CachesInterface;
$caches = new Caches();
var_dump($caches instanceof CachesInterface);
// bool(true)
use Psr\SimpleCache\CacheInterface;
$caches->add(
name: 'primary',
cache: $cache, // CacheInterface
);
use Psr\SimpleCache\CacheInterface;
$caches->register(
name: 'name',
cache: function(string $name): CacheInterface {
// create the cache:
return $cache;
},
);
use Psr\SimpleCache\CacheInterface;
use Psr\SimpleCache\CacheException;
$cache = $caches->get(name: 'file');
var_dump($cache instanceof CacheInterface);
// bool(true)
$caches->get(name: 'unknown');
// throws CacheException
namespace Tobento\Service\Cache\Simple;
use Psr\SimpleCache\CacheInterface;
use Psr\SimpleCache\CacheException;
interface CacheFactoryInterface
{
/**
* Create a new Cache based on the configuration.
*
* @param string $name
* @param array $config
* @return CacheInterface
* @throws CacheException
*/
public function createCache(string $name, array $config = []): CacheInterface;
}
namespace Tobento\Service\Cache\Simple;
use Psr\SimpleCache\CacheInterface;
use Psr\SimpleCache\CacheException;
interface CachesInterface
{
/**
* Add a cache.
*
* @param string $name The cache name.
* @param CacheInterface $cache
* @return static $this
*/
public function add(string $name, CacheInterface $cache): static;
/**
* Register a cache.
*
* @param string $name The cache name.
* @param callable $cache
* @return static $this
*/
public function register(string $name, callable $cache): static;
/**
* Returns the cache by name.
*
* @param string $name The cache name
* @return CacheInterface
* @throws CacheException
*/
public function get(string $name): CacheInterface;
/**
* Returns true if the cache exists, otherwise false.
*
* @param string $name The cache name.
* @return bool
*/
public function has(string $name): bool;
/**
* Adds a default name for the specified cache.
*
* @param string $name The default name.
* @param string $cache The cache name.
* @return static $this
*/
public function addDefault(string $name, string $cache): static;
/**
* Returns the default caches.
*
* @return array<string, string> ['general' => 'files']
*/
public function getDefaults(): array;
/**
* Returns the cache for the specified default name.
*
* @param string $name The type such as pdo.
* @return CacheInterface
* @throws CacheException
*/
public function default(string $name): CacheInterface;
/**
* Returns true if the default cache exists, otherwise false.
*
* @param string $name The default name.
* @return bool
*/
public function hasDefault(string $name): bool;
/**
* Returns the names.
*
* @return array<int, string>
*/
public function getNames(): array;
}
namespace Tobento\Service\Cache;
interface CanDeleteExpiredItems
{
/**
* Removes all expired items from the pool or cache.
*
* @return bool
* True if the items were successfully removed. False if there was an error.
*/
public function deleteExpiredItems(): bool;
}
service-file-storage
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.