1. Go to this page and download the library: Download phpixie/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/ */
phpixie / cache example snippets
$cache = $builder->components->cache();
$slice = new \PHPixie\Slice();
$config = $slice->arrayData([
// configuration
'default' => [
'driver' => 'memory'
]
]);
// Optional dependency if you want to use file cache.
// This defines the root folder for file based drivers.
$filesystem = new \PHPixie\Filesystem();
$root = $filesystem->root('/tmp/cache/');
$cache = new \PHPixie\Cache($config, $root);
return [
'default' => [
// Doesn't store anything
'driver' => 'void'
],
'second' => [
// Stores in a simple array
'driver' => 'memory',
/* Optional */
/**
* Default lifetime,
* can be either number of seconds
* or a DateInterval value, e.g. 'P1D' is one day.
* Default is null, which is to store forever
*/
'defaultExpiry' => 10,
/**
* A number between 1 and 1000 that
* defines the frequency of garbage collection.
* Defaults to 10, so 1% of all cache queries will
* result in garbage collection to be run.
*/
'cleanupProbability' => 10
],
'third' => [
// Values stored as .php files in folder
'driver' => 'phpfile',
// Relative to the /assets/cache folder
'path' => 'third',
/* Optional */
'defaultExpiry' => null,
'cleanupProbability' => 10
],
'fourth' => [
'driver' => 'memcached',
/**
* Same argument as to the Memcached::addServers() method,
* but the port and weight parameters can be omitted and
* default to 11211 and 1 respectively
*/
'servers' => [
['127.0.0.1']
],
/* Optional */
'defaultExpiry' => null
],
'fifth' => [
'driver' => 'redis',
// Same argument as to the Predis\Client constructor
'connection' => array(
'scheme' => 'tcp',
'host' => '127.0.0.1',
'port' => 6379
),
/* Optional */
'defaultExpiry' => null
]
];
namespace PHPixie\Cache;
use PHPixie\Cache\Pool\Prefixed;
use Psr\Cache\CacheItemPoolInterface;
use Psr\SimpleCache\CacheInterface;
// Inherits both PSR-6 and PSR-16
interface Pool extends CacheItemPoolInterface, CacheInterface
{
/**
* Creates a PSR-6 Item instance without trying to retrieve it from cache
* @param string $key
* @param mixed $value
* @return Item
*/
public function createItem($key, $value = null);
/**
* Creates a namespaced prefix pool.
* We'll cover this later.
* @param string $prefix
* @return Prefixed
*/
public function prefixedPool($prefix);
}
// Getting one of the defined storages
$storage = $cache->storage('second');
// PSR-6
public function getFairies()
{
$item = $this->storage->getItem('fairies');
if (!$item->isHit()) {
$fairies = $this->generateFairies();
$item->set($fairies);
$item->expiresAfter(100);
$this->storage->save($item);
}
return $item->get();
}
// PSR-16
public function getFairies()
{
$fairies = $this->storage->get('fairies');
if($fairies === null) {
$fairies = $this->buildFairies();
$this->storage->set('fairies', $fairies, 100);
}
return $fairies;
}