How to Cache in Symfony 2

How to cache in Symfony 2?

If you are using Doctrine already just use those cache classes.

Add a service to config.yml:

services:
cache:
class: Doctrine\Common\Cache\ApcCache

And use it in your controller:

if ($fooString = $this->get('cache')->fetch('foo')) {
$foo = unserialize($fooString);
} else {
// do the work
$this->get('cache')->save('foo', serialize($foo));
}

How to cache a Doctrine 2 query properly on Symfony?

Doctrine have several types of caches:

  1. for class metadata (where class mappings to database are stored)
  2. for queries (where parsed DQL queries are stored)
  3. for query results (where actual results fetched by queries are stored)

First 2 types of Doctrine caches are not relevant for your question because they're specific to Doctrine and there is no direct alternatives for them in Symfony. Query results cache can be (indirectly) replaced with Symfony cache.

Use of query result cache and decision about which cache to use should depend on your application's logic. You can prefer to use Doctrine query results cache in a case if following criteria is met:

  1. You need to store raw data fetched from database (means it requires no further processing before being stored in cache)
  2. Your query results can be stored in cache for specific amount of time and unlikely to became stale within this period of time

In this case Doctrine query results cache can be useful for you because it will be transparent for your application. It may be useful just for some queries since you can control Doctrine cache usage and lifetime on per-query basis.

In a case if you need to apply some further processing for results fetched from database before storing them in cache or you have some additional logic to apply to decide if cache contents became stale - it is better to use Symfony cache because it is specific for your application and can be controlled by you.

Symfony2 cache and parameters

I ended up writing a service to parse the host name, that service was then available within my Controllers.

class GameSystem {

public function getGameSystemName()
{
$ar = explode(".", $_SERVER['HTTP_HOST']);
$first = reset($ar);

$game_system_short_name = strtolower($first);

return $game_system_short_name;
}
}

Initially I had tried to use the constant value set in each subdomain's index file. But this was failing and having some really odd effects.

Cache variable in controller in Symfony2

Symfony doesn't provide a mechanism for what you are describing. But any solution that would work for PHP more generally, will work for Symfony.

It depends if you want to remember advertisements for each user or for all users. If you want to remember it for each user, use sessions as Gareth Parker suggested. If you want to remember it for all users, then you would need APC user caching, memcache or another memory-based key-value store.

You may also have luck using Doctrine result cache. See http://doctrine-orm.readthedocs.org/en/latest/reference/caching.html

Caching Doctrine results Symfony2

You need to have your cache driver installed and configured in doctrine configuration (result_cache_driver is important in your case). Once you have this done you can make Doctrine to use result cache by setting useResultCache(true)

$cachedResult = $doctrine->getManager()
->createQueryBuilder()
->(...)
->useResultCache(true)
->(...)

Check this blog post

NOTE: by default, in dev environment, result cache won't be used

EDIT: as you're using DBAL and not using ORM - SymfonyDoctrineBundle doesn't support this kind of cache out of the box, but you can add this support by yourself by following this detailed guide



Related Topics



Leave a reply



Submit