Symfony2: Managing the cache yourself
Web agency » Digital news » Symfony2: Managing the cache yourself

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?

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 :

  1. _sqlQuery corresponds to the SQL query you wish to execute.
  2. id is the identifier bound to the cached data. For an ID x, you will have a content x.
  3. 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.
    1
    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
    <?php
    function 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 null
    if ($results == null) {
    //We retrieve the data
    $results = $ this->container->get("doctrine")->getManager()->getRepository("NamespaceNameBundle:Entity")->findAll();
    //If we have a caching ID
    if ($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 requests
    return $results;
    }

Well, I hope this memo helps you. See you soon and thank you for reading this post!

★ ★ ★ ★ ★