I need to cache some application specific data using Symfony 2's caching system so that I can run cache:clear
to clear it. All the cache relies under app/cache
but how do I actually go about caching data?
http://symfony.com/doc/current/cookbook/index.html
The only topic I see is about HTML caching with Varnish.
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));
}
Simple way use Doctrine cache providers.
At first, register service(sample in config.yml):
services:
memcached:
class: Memcached
calls:
- [ addServer, ['localhost', 11211] ]
memcached_cache:
class: Doctrine\Common\Cache\MemcachedCache
calls:
- [ setMemcached, [@memcached] ]
Then to use get service, for example in controler:
$cache = $this->get('memcached_cache');
to send in another service use calls:
calls:
- [ setCacheProvider, [@memcached_cache] ]
or arguments:
arguments:
- @memcached_cache
In the same way, you can use other interfaces of Doctrine Cache package.
Doctrine Cache provides a very simple interface for which several out of the box implementations are provided:
- ApcCache (requires ext/apc)
- ArrayCache (in memory, lifetime of the request)
- FilesystemCache (not optimal for high concurrency)
- MemcacheCache (requires ext/memcache)
- MemcachedCache (requires ext/memcached)
- PhpFileCache (not optimal for high concurrency)
- RedisCache.php (requires ext/phpredis)
- WinCacheCache.php (requires ext/wincache)
- XcacheCache.php (requires ext/xcache)
- ZendDataCache.php (requires Zend Server Platform)
If you do not already use Doctrine, you may require Common Library for Doctrine projects: php composer.phar require doctrine/common
or require only Caching library offering an object-oriented API for many cache backends: php composer.phar require doctrine/cache
How to use Doctrine Caching you can read in Doctrine Common documentation on Doctrine Project web site
Symfony 3.1 provide a new Cache component.
Symfony2 does not provide any component for application layer caching.
Like you were already told, you can use the Doctrine Common caching library http://docs.doctrine-project.org/projects/doctrine-common/en/latest/reference/caching.html
If you want something more advanced, you can also use one of the cache bundle provided by the community. For instance, the https://github.com/TheBigBrainsCompany/TbbcCacheBundle#cachebundle which provides tools for a good caching strategy.
There is no partial cache in Symfony2, the build-in cache is full HTTP only.
You have to use a reverse proxy, and if you only want to cache a piece of code, you have to use ESI. It's maybe more work than with symfony 1 but performances worth it.
Anyway, nothing stop you to use a memcached and store some stuff in it, look at this Bundle i.e.
If as your question state it, you only have data to store, that's perfect (and a memcache cache is much faster than a filesystem one).