This project is not being actively maintained. If you are interested in helping to maintain this project, take a look at the open issues on GitHub and submit pull requests. |
Introduction
Doctrine Cache is a library that provides an interface for caching data.
It comes with implementations for some of the most popular caching data
stores. Here is what the Cache
interface looks like.
Here is an example that uses Memcache.
Drivers
Doctrine ships with several common drivers that you can easily use. Below you can find information about all the available drivers.
ApcCache
The ApcCache
driver uses the apc_fetch
, apc_exists
, etc. functions that come
with PHP so no additional setup is required in order to use it.
1 $cache = new ApcCache();
ApcuCache
The ApcuCache
driver uses the apcu_fetch
, apcu_exists
, etc. functions that come
with PHP so no additional setup is required in order to use it.
1 $cache = new ApcuCache();
ArrayCache
The ArrayCache
driver stores the cache data in PHPs memory and is not persisted anywhere.
This can be useful for caching things in memory for a single process when you don't need
the cache to be persistent across processes.
1 $cache = new ArrayCache();
CouchbaseBucketCache
The CouchbaseBucketCache
driver uses Couchbase to store the cache data.
1 $bucketName = 'bucket-name';
$authenticator = new Couchbase\PasswordAuthenticator();
$authenticator->username('username')->password('password');
$cluster = new CouchbaseCluster('couchbase://127.0.0.1');
$cluster->authenticate($authenticator);
$bucket = $cluster->openBucket($bucketName);
$cache = new CouchbaseBucketCache($bucket);
2
3
4
5
6
7
8
9
10
11
FilesystemCache
The FilesystemCache
driver stores the cache data on the local filesystem.
1 $cache = new FilesystemCache('/path/to/cache/directory');
PhpFileCache
The PhpFileCache
driver stores the cache data on the local filesystem like the
FilesystemCache
driver except the data is serialized using the serialize()
and unserialize()
functions available in PHP. The files are included so this means
that the data can be cached in PHPs opcache.
1 $cache = new PhpFileCache('/path/to/cache/directory');
PredisCache
The PredisCache
driver stores the cache data in Redis
and depends on the predis/predis
package which can be installed with composer.
$ composer require predis/predis
Then you can use the Predis\Client
class to pass to the PredisCache
class.
RedisCache
The RedisCache
driver stores the cache data in Redis and depends on the
phpredis
extension which can be found here.
SQLite3Cache
The SQLite3Cache
driver stores the cache data in a SQLite database and depends on the
sqlite3
extension which can be found here.
VoidCache
The VoidCache
driver does not store the cache data anywhere. This can
be useful for test environments where you don't want to cache the data
anywhere but need to satisfy the dependency for the Doctrine\Common\Cache\Cache
interface.
1 $cache = new VoidCache();
WinCacheCache
The WinCacheCache
driver uses the wincache_ucache_get
, wincache_ucache_exists
, etc. functions that come
with the wincache
extension which can be found here.
1 $cache = new WinCacheCache();
XcacheCache
The XcacheCache
driver uses functions that come with the xcache
extension which can be found here.
1 $cache = new XcacheCache();
ZendDataCache
The ZendDataCache
driver uses the Zend Data Cache API available in the Zend Platform.
1 $cache = new ZendDataCache();
Custom Drivers
If you want to implement your own cache driver, you just need to implement
the Doctrine\Common\Cache\Cache
interface. Here is an example implementation
skeleton.
1 use Doctrine\Common\Cache\Cache;
class MyCacheDriver implements Cache
{
public function fetch($id)
{
// fetch $id from the cache
}
public function contains($id)
{
// check if $id exists in the cache
}
public function save($id, $data, $lifeTime = 0)
{
// save $data under $id in the cache for $lifetime
}
public function delete($id)
{
// delete $id from the cache
}
public function getStats()
{
// get cache stats
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29