You are browsing a version that is no longer maintained.
Doctrine provides cache drivers in the
Common package for some
of the most popular caching implementations such as APC, Memcache
and Xcache. We also provide an
ArrayCache driver which stores
the data in a PHP array. Obviously, the cache does not live between
requests but this is useful for testing in a development
The cache drivers follow a simple interface that is defined in
Doctrine\Common\Cache\Cache. All the cache drivers extend a
Doctrine\Common\Cache\AbstractCache which implements
the before mentioned interface.
The interface defines the following methods for you to publicly use.
- fetch($id) - Fetches an entry from the cache.
- contains($id) - Test if an entry exists in the cache.
- save($id, $data, $lifeTime = false) - Puts data into the cache.
- delete($id) - Deletes a cache entry.
Each driver extends the
AbstractCache class which defines a few
abstract protected methods that each of the drivers must
- _doSave($id, $data, $lifeTime = false)
The public methods
contains(), etc. utilize the
above protected methods that are implemented by the drivers. The
code is organized this way so that the protected methods in the
drivers do the raw interaction with the cache implementation and
AbstractCache can build custom functionality on top of
In order to use the APC cache driver you must have it compiled and enabled in your php.ini. You can read about APC in the PHP Documentation. It will give you a little background information about what it is and how you can use it as well as how to install it.
Below is a simple example of how you could use the APC cache driver by itself.
In order to use the Memcache cache driver you must have it compiled and enabled in your php.ini. You can read about Memcache on the PHP website. It will give you a little background information about what it is and how you can use it as well as how to install it.
Below is a simple example of how you could use the Memcache cache driver by itself.
In order to use the Xcache cache driver you must have it compiled and enabled in your php.ini. You can read about Xcache here. It will give you a little background information about what it is and how you can use it as well as how to install it.
Below is a simple example of how you could use the Xcache cache driver by itself.
In order to use the Redis cache driver you must have it compiled and enabled in your php.ini. You can read about what is Redis from here. Also check A PHP extension for Redis for how you can use and install Redis PHP extension.
Below is a simple example of how you could use the Redis cache driver by itself.
In this section we'll describe how you can fully utilize the API of
the cache drivers to save cache, check if some cache exists, fetch
the cached data and delete the cached data. We'll use the
ArrayCache implementation as our example here.
To save some data to the cache driver it is as simple as using the
save() method accepts three arguments which are described
$id- The cache id
$data- The cache entry/data.
$lifeTime- The lifetime. If != false, sets a specific lifetime for this cache entry (null => infinite lifeTime).
You can save any type of data whether it be a string, array, object, etc.
Checking whether some cache exists is very simple, just use the
contains() method. It accepts a single argument which is the ID
of the cache entry.
Now if you want to retrieve some cache entry you can use the
fetch() method. It also accepts a single argument just like
contains() which is the ID of the cache entry.
As you might guess, deleting is just as easy as saving, checking and fetching. We have a few ways to delete cache entries. You can delete by an individual ID, regular expression, prefix, suffix or you can delete all entries.
If you heavily use caching in your application and utilize it in
multiple parts of your application, or use it in different
applications on the same server you may have issues with cache
naming collisions. This can be worked around by using namespaces.
You can set the namespace a cache driver should use by using the
The Doctrine ORM package is tightly integrated with the cache drivers to allow you to improve performance of various aspects of Doctrine by just simply making some additional configurations and method calls.
It is highly recommended that in a production environment you cache the transformation of a DQL query to its SQL counterpart. It doesn't make sense to do this parsing multiple times as it doesn't change unless you alter the DQL query.
This can be done by configuring the query cache implementation to use on your ORM configuration.
The result cache can be used to cache the results of your queries so that we don't have to query the database or hydrate the data again after the first time. You just need to configure the result cache implementation.
Now when you're executing DQL queries you can configure them to use the result cache.
You can also configure an individual query to use a different result cache driver.
Setting the result cache driver on the query will
automatically enable the result cache for the query. If you want to
disable it pass false to
If you want to set the time the cache has to live you can use the
The ID used to store the result set cache is a hash which is
automatically generated for you if you don't set a custom ID
yourself with the
You can also set the lifetime and cache ID by passing the values as
the second and third argument to
Your class metadata can be parsed from a few different sources like YAML, XML, Annotations, etc. Instead of parsing this information on each request we should cache it using one of the cache drivers.
Just like the query and result cache we need to configure it first.
Now the metadata information will only be parsed once and stored in the cache driver.
We've already shown you previously how you can use the API of the cache drivers to manually delete cache entries. For your convenience we offer a command line task for you to help you with clearing the query, result and metadata cache.
From the Doctrine command line you can run the following command.
1 $ ./doctrine clear-cache
Running this task with no arguments will clear all the cache for all the configured drivers. If you want to be more specific about what you clear you can use the following options.
To clear the query cache use the
1 $ ./doctrine clear-cache --query
To clear the metadata cache use the
1 $ ./doctrine clear-cache --metadata
To clear the result cache use the
1 $ ./doctrine clear-cache --result
When you use the
--result option you can use some other options
to be more specific about what queries result sets you want to
Just like the API of the cache drivers you can clear based on an ID, regular expression, prefix or suffix.
1 $ ./doctrine clear-cache --result --id=cache_id
Or if you want to clear based on a regular expressions.
1 $ ./doctrine clear-cache --result --regex=users_.*
Or with a prefix.
1 $ ./doctrine clear-cache --result --prefix=users_
And finally with a suffix.
1 $ ./doctrine clear-cache --result --suffix=_my_account
Something to be careful of when utilizing the cache drivers is cache slams. If you have a heavily trafficked website with some code that checks for the existence of a cache record and if it does not exist it generates the information and saves it to the cache. Now if 100 requests were issued all at the same time and each one sees the cache does not exist and they all try and insert the same cache entry it could lock up APC, Xcache, etc. and cause problems. Ways exist to work around this, like pre-populating your cache and not letting your users requests populate the cache.
You can read more about cache slams in this blog post.