PHP Cache¶
The PHP Cache organization is dedicated to providing solid, powerful, flexible, and lightweight caching libraries for PHP projects. All of the adapters we have created are PSR-6 and PSR-16 compliant. If you are a library implementer, we even have a repository of tests to help you meet the PSR specification.
Below you will find information about what features our libraries offer, and what adapters we have. You can also find out framework integration libraries.
If you are new to PSR-6 caching you may want to have a look at our introduction.
Note: The PHP-cache organization has been pushing the limits of caching for many years. But currently we fall behind the quicker and better maintained Symfony Cache.
Cache pool implementations¶
There are plenty of adapters in this organization. Each of them lives in a different repository. Splitting them up in multiple packages complies with the Common reuse principle and makes it easier for the developer to follow the changes of a specific adapter.
Each adapter has it own features. The table below lists all our adapters and their features.
Adapter | Tagging | Hierarchy* | Badges |
---|---|---|---|
Apc | Yes | No | |
Apcu | Yes | No | |
Array | Yes | No | |
Couchbase 1.x (via Doctrine) | Yes | No | |
Filesystem (via Flysystem) | Yes | No | |
Illuminate | Yes | No | |
Memcache | Yes | No | |
Memcached | Yes | Yes | |
MongoDB | Yes | No | |
Predis | Yes | Yes | |
Redis | Yes | Yes | |
Riak (via Doctrine) | Yes | No | |
SQLite3 (via Doctrine) | Yes | No | |
Void | Yes | Yes | |
WinCache (via Doctrine) | Yes | No | |
Xcache (via Doctrine) | Yes | No | |
ZendData (via Doctrine) | Yes | No | |
Chain | Yes | ||
Doctrine | Yes | No |
* Hierarchy store lots of extra items in cache that are never actively removed. Some implementations of cache storages like Redis and Memcache will automatically remove these items when they're stale or no longer used. That is why hierarchy will work better on such cache storages.
Chain adapter¶
We also have a chain adapter where you can chain multiple pool together. It is great if you have a fast storage with limited memory and a slower storage with loads of memory.
Doctrine adapter¶
The doctrine adapter is a PSR-6 adapter that wraps a Doctrine\Common\Cache\Cache
object. With this adapter you can use
storages like Riak and WinCache which currently do not have any PHP Cache adapters.
Installation¶
Use composer to install any of the adapters above. Some adapters may require configuration before they can be used. Refer to the adapter's Github page to see how they are configured. You could also use the Symfony AdapterBundle to configure the adapters.
composer require cache/[any]-adapter
You can also install all of the adapters with the cache/cache
composer require cache/cache
Requirements¶
Unless other is specified, all adapters support PHP version ^5.5
and ^7.0
. Most adapters do also have requirements
on PHP extension. Like the Redis adapter requires ext-redis
.
Features¶
Tagging¶
Tags is used to control the invalidation of items.
$item = $pool->getItem('tobias');
$item->set('value')->setTags(['tag0', 'tag1'])
$pool->save($item);
$item = $pool->getItem('aaron');
$item->set('value')->setTags(['tag0']);
$pool->save($item);
// Remove everything tagged with 'tag1'
$pool->invalidateTags(['tag1']);
$pool->getItem('tobias')->isHit(); // false
$pool->getItem('aaron')->isHit(); // true
$item = $pool->getItem('aaron');
echo $item->getPreviousTags(); // array('tag0')
// No tags will be saved again. This is the same as saving
// an item with no tags.
$pool->save($item);
Hierarchy¶
Think of a hierarchy like a file system. If you remove a folder "Foo", all items and folders in "Foo" will also be removed. A hierarchical cache key must start with a pipe ("|").
$pool->hasItem('|users|4711|followers|12|likes'); // True
$pool->deleteItem('|users|4711|followers');
$pool->hasItem('|users|4711|followers|12|likes'); // False
Namespace¶
Namespace can be used to separate the storage of different systems in the cache. This allows different sections to be cleared on an individual level, while also preventing overlapping keys.
$pool = new ArrayCachePool();
$namespaceFoo = new NamespacedCachePool($pool, 'foo');
$item = $namespaceFoo->getItem('key')->set('value');
$namespaceFoo->save($item);
$namespaceBar = new NamespacedCachePool($pool, 'bar');
$namespaceBar->hasItem('key'); // False
$item = $namespaceBar->getItem('key')->set('value');
$namespaceBar->save($item);
$namespaceBar->hasItem('key'); // True
$namespaceFoo->deleteItem('key');
$namespaceFoo->hasItem('key'); // False
$namespaceBar->hasItem('key'); // True
$namespaceFoo->clear();
$namespaceBar->hasItem('key'); // True
Prefix¶
A prefix will help you to avoid cache key collisions. The prefixed cache pool supports any PSR-6 cache implementations. The PrefixedCachePool differs from the NamespacedCachePool in two aspects:
1) You could still have conflicts if one cache key includes the prefix 2) When clearing the cache all cache items will be cleared, not only the prefixed ones.
$pool = new ArrayCachePool();
$prefixedFoo = new PrefixedCachePool($pool, 'foo');
$item = $prefixedFoo->getItem('key')->set('value');
$prefixedFoo->save($item);
$pool->hasItem('key'); // False
$item = $pool->getItem('key')->set('value');
$pool->save($item);
$pool->hasItem('key'); // True
$prefixedFoo->deleteItem('key');
$prefixedFoo->hasItem('key'); // False
$pool->hasItem('key'); // True
$prefixedFoo->clear();
$pool->hasItem('key'); // False
Framework integration¶
Symfony¶
There are two Symfony bundles; AdapterBundle and CacheBundle.
The AdapterBundle is used to configure and register a PSR-6 cache pool as a Symfony service. The CacheBundle is used to integrate any PSR-6 cache service with the framework. It supports session cache, doctrine cache, validation cache and many more.
We would LOVE to see integration with Zend, Laravel, Yii, Cake, and even Code Igniter. If you would like to contribute, we would love to see your code.
Organisation overview¶
Excluding our adapters, we have the following packages
Name | Description | Badges |
---|---|---|
Cache | Base Cache Repository. Contains all adapters. | |
AdapterBundle | Bundle to register adapters to services. | |
Adapter common | The AbstractCachePool and CacheItem live here. |
|
CacheBundle | Bundle to integrate any PSR-6 service with the Symfony framework. |
|
Doctrine bridge | A bridge from PSR-6 to DoctrineCache | |
Encryption | Encrypt data you store | |
Hierarchical cache | A trait and interface to support cache hierachy | |
Integration tests | Used to verify any PSR-6 implementation | |
Namespaced cache | Pool to support a namespace | |
Prefixed cache | Pool to support a prefix | |
Session handler | Implementation of \SessionHandlerInterface |
|
Simple Cache Bridge | Bridge from PSR-6 to PSR-16 SimpleCache | |
Taggable cache | Decorator to make any PSR-6 cache taggable | |
Tag interop | Interfaces to support cache tagging. (Soon PSR) |
Contact¶
We would love to hear form you. Ping us on twitter @aequasi and @tobiasnyholm. You could also join us on Gitter.