Symfony2: Managing the cache yourself
The cache is present everywhere in computing and fortunately elsewhere. Without it, processing times would be much longer! At a time when all frameworks worthy of the name use a cache system, why would we need to manage the cache ourselves?
In this section:
Cache in Symfony2
In Symfony2, you can easily set up a cache system. Besides, if we wanted to be exact, it looks like Symfony has its own cache system (app/cache/env/), and that we can also plug in a cache system at the server level (Apache, IIS, …). This cache is fully manageable and the memory is volatile. As soon as the server is restarted the server cache is deleted.
Caching systems
What caching systems exist? From memory, three programs come up regularly.
- xcache
- Memcached
- Services
APC is now deprecated since PHP 5.4, so I don't recommend trying to plug it in.
In my case, I will use XCache. You can also find my post on how to install XCache.
Why manage your cache yourself
I can see a few comments coming in and asking “why? ". While Doctrine allows you to easily use the cache via its method useResultCache(use, delay, id). My answer is simple: to do free treatments.
A small example to give you some ideas. Let's say I'm caching an item from a database. If I modify this entity, the cache will not be updated until the data has expired. With a personalized cache management, I will be able even before the cache reaches the allotted time to re-set the new data and overwrite the old ones. This requires significant management, but the performance gains can be substantial.
Symfony2 and how to manage your own cache
I offer you this small memo code to manage easily. In our case, the function of this Gist aims to return the result of an SQL query.
Prototype: function execute ($sql_query, $id = null, $delay = 3600);
Settings :
- _sqlQuery corresponds to the SQL query you wish to execute.
- id is the identifier bound to the cached data. For an ID x, you will have a content x.
- delay is the maximum time you give cache will be valid. By default, 3600 seconds. This means that after caching your data, it will be available for 3600 seconds.
12345678910111213141516171819202122232425262728function execute ($sql_query, $id = null, $delay = 3600){// We declare a cache instance$cacheDriver = new DoctrineCommonCacheXcacheCache();$results = null;//If an ID was entered and the cache system contains data for an ID "x"if ($id!= null && $cacheDriver->contains($id)) {//Then we take the cached data for an ID "x"$results = $cacheDriver->fetch($id);}//If there is no data, in case the cached data is equal to nullif ($results == null) {//We retrieve the data$results = $ this->container->get("doctrine")->getManager()->getRepository("NamespaceNameBundle:Entity")->findAll();//If we have a caching IDif ($id!= null) {//We save the data for an ID x and for a delay y$cacheDriver->save($id, $results, intval($delay));}}//We return the results by limiting future requestsreturn $results;}
Well, I hope this memo helps you. See you soon and thank you for reading this post!