Redis is an open source key-value cache and storage, which designed to be very fast since it uses in-memory datasets. Of course, you can persist your data by dumping it to the disk if you need it. Redis supports different data structures such as strings, lists, sets/sorted sets, hashes; atomic operations on these types, master-slave asynchronous replication, transactions, etc.
If you are using Doctrine with Symfony, Redis is a great tool to be used for caching, which is very easy to setup and it can drastically improve performance of your application.
Doctrine supports 3 types of caching:
So let’s try to configure Doctrine to work with Redis for caching.
If you under OS X, you can use homebrew:
1 2 3 4 |
brew update brew install redis # start server redis-server |
For Ubuntu, you need to build it from sources:
1 2 3 4 5 6 7 8 9 |
wget http://download.redis.io/releases/redis-2.8.19.tar.gz tar xzf redis-2.8.19.tar.gz cd redis-2.8.19 make sudo make install cd utils sudo ./install_server.sh # start server service redis_6379 start |
If you’ve used default config, now redis is running on your machine on 6379 port.
We will use this great bundle to work with Redis inside our application. Note, that SncRedisBundle bundle can work with both Predis orPHPRedis libraries to communicate with Redis, so it’s your choice which one to use. I went with Predis.
Let’s add bundle itself and Predis as a needed dependency inside our composer.json and run composer install after that:
1 2 |
"predis/predis": "0.8.x-dev", "snc/redis-bundle": "1.1.x-dev" |
Activate bundle in app/AppKernel.php:
Configure Redis client itself and set it to be used for Doctrine in app/config/config.yml
snc_redis:
# configure predis as client
clients:
default:
type: predis
alias: default
dsn: redis://localhost
doctrine:
type: predis
alias: doctrine
dsn: redis://localhost
# configure doctrine caching
doctrine:
metadata_cache:
client: doctrine
entity_manager: default
document_manager: default
result_cache:
client: doctrine
entity_manager: [default]
query_cache:
client: doctrine
entity_manager: default
In the same file, we need to enable metadata and query caching for Doctrine:
In the most cases, you don’t need cache results from all of your queries, so let’s see how you can enable result cache for some individual one only. The best place to do it is inside of an entity repository class.
Let’s say we have Country entity with respective repository. In this repository, we have a method to find active countries and return a list with 3 items sorted by country’s sort field. Let’s add caching for the results of this method.
That’s all. Now Doctrine will get countries data from the database for the first time, cache it in Redis for 1 hour and update it again after expiration.
Redis comes with redis-cli tool, which can be used to check cached values by running command KEYS *:
To see what contains individual cache entity, you can run GET key_string command.
Another great thing to check Doctrine cache is Symfony Profiler toolbar.