Phossa-cache is a PSR-6 compliant caching library. It supports various drivers
and useful features like bypass, encrypt, stampede protection, garbage collect,
taggable item etc., (*3)
-
Fully PSR-6 compliant. Maybe the most feature-rich PSR-6 caching package you
will find at github at this time., (*7)
-
Support all serializable PHP datatypes., (*8)
-
Extensions:, (*9)
-
Bypass: If sees a trigger in URL (e.g. '?nocache=true'), bypass the
cache., (*10)
-
Stampede: Whenever cached object's lifetime is less than a configurable
time, by certain percentage, the cache will return false on 'isHit()' which
will trigger re-generation of the object., (*11)
-
Encrypt: A simple extension to encrypt the serialized content, (*12)
-
GarbageCollect: A simple extension to auto-clean the cache pool., (*13)
-
Taggable: Item is taggable and can be cleared by tag., (*14)
-
DistributeMiss: Even out the spikes of item misses by alter expiration
time a little bit., (*15)
-
Drivers, (*16)
The filesystem driver stores cached item in filesystem. It stores cached
items in a md5-filename flat file. Configurable settings are, (*17)
-
dir_root
: the base directory for the filesystem cache
-
hash_level
: hashed subdirectory level. default to 2
-
file_pref
: cached item filename prefix
-
file_suff
: cached item filename suffix
/*
* construct the driver manually
*/
$driver = new Driver\FilesystemDriver([
'hash_level' => 1,
'file_pref' => 'cache.',
'dir_root' => '/var/tmp/cache',
]);
The blackhole driver. used as fallback driver for all other drivers., (*18)
User may configure a fallback driver if the desired driver is not ready.
The NullDriver
is the final fallback for all other drivers., (*19)
// default memcache driver
$driver = new Driver\MemcacheDriver([
'server' => [ '127.0.0.1', 11211 ]
]);
// set a fallback filesystem driver
$driver->setFallback(new Driver\FilesystemDriver([
'dir_root' => '/var/tmp/cache'
]));
$cache = new CachePool($driver);
The CompositeDriver
consists of two drivers, the front-end driver and
the backend driver. User filters cachable objects by defining a tester
callable which will determine which objects stores to both ends or backend
only., (*20)
/*
* set the composite driver
*/
$driver = new Driver\CompositeDriver(
// front-end driver
new Driver\MemcacheDriver([
'server' => [ '127.0.0.1', 11211 ]
]),
// backend driver
new Driver\FilesystemDriver([
'dir_root' => '/var/tmp/cache'
]),
// other settings
[
// if size > 10k, stores at backend only
'tester' => function($item) {
if (strlen($item->get()) > 10240) return false;
return true;
}
]
);
-
Logging, (*21)
The phossa-cache supports psr-3 compliant logger. Also provides a log()
method for logging., (*22)
/*
* set the logger
*/
$cache->setLogger($psrLogger);
$cache->log('info', 'this is an info');
Or configure with logger at cache init, (*23)
/*
* the third argument is used for configuring CachePool
*/
$cache = new CachePool($driver, [],
'logger' => $psrLogger
);
$cache->log('info', 'this is an info');
-
Error, (*24)
No exceptions thrown during caching process. So only errors will be used., (*25)
/*
* create cache pool, exceptions may thrown here
*/
$cache = new CachePool();
$cache->setLogger($psrLogger);
$item = $cache->getItem('widget_list');
$val = $item->get();
if ($cache->hasError()) {
$cache->log('error', $cache->getError());
$widget_list = compute_expensive_widget_list();
$item->set($widget_list);
$item->expiresAfter(3600); // expires after an hour
$cache->save($item);
if ($cache->hasError()) $cache->log('error', $cache->getError());
} else {
$widget_list = $val;
}
-
I18n, (*26)
Messages are in Message\Message.php
. I18n is possible. See phossa-shared
package for detail., (*27)
-
Support PHP 5.4+, PHP 7.0, HHVM., (*28)
-
PHP7 ready for return type declarations and argument type declarations., (*29)
-
PSR-1, PSR-2, PSR-3, PSR-4, PSR-6 compliant., (*30)
-
The simple usage, (*31)
/*
* use the default FilesystemDriver which also set default cache
* directory to sys_get_temp_dir() .'/cache'
*/
$cache = new CachePool();
$item = $cache->getItem('widget_list');
if (!$item->isHit()) {
$value = compute_expensive_widget_list();
$item->set($value);
$cache->save($item);
}
$widget_list = $item->get();
-
Configure the driver, (*32)
$driver = new Driver\FilesystemDriver([
'hash_level' => 1, // subdirectory hash levels
'file_pref' => 'cache.', // cache file prefix
'file_suff' => '.txt', // cache file suffix
'dir_root' => '/var/tmp/cache' // reset cache root
]);
$cache = new CachePool($driver);
-
Use extensions, (*33)
/*
* SerializeExtension is the default ext, always used.
* Second argument is an array of ExtensionInterface or config array
*/
$cache = new CachePool(
$driver,
[
new Extension\BypassExtension(),
new Extension\StampedeExtension(['probability' => 80 ])
]
);
or addExtension()
, (*34)
$cache = new CachePool($driver);
$cache->addExtension(new Extension\BypassExtension());
-
Hierarchal cache support, (*35)
Directory-style hierarchal structure is supported in FilesystemDriver
and
so other coming drivers., (*36)
// hierarchy key
$item = $cache->getItem('mydomain/host1/newfile_xxx');
// ending '/' means delete the hierarchy structure
$cache->deleteItem('mydomain/host1/');