lioshi WonderCacheBundle
This Symfony 2 bundle provides a full response cache with automatic invalidation via doctrine events:
- just one service's call to manage cache of an action
- no wasted time setting up a cache invalidation system, (*1)
, (*2)
, (*3)
WonderCache is there at request and bypasses all framework. As a proxy.
But WonderCache knows when invalidate its cache., (*4)
Requirements
- PHP 5.3.x or more
- php5-memcached 2.x
Installation
Use Composer to install to install WonderCacheBundle with Composer just add the following to your composer.json file:, (*5)
{
require: {
"lioshi/wonder-cache-bundle": "dev-master",
...
}
}
The next thing you should do is install the bundle by executing the following command:, (*6)
php composer.phar update lioshi/wonder-cache-bundle
Finally, add the bundle to the registerBundles function of the AppKernel class in the app/AppKernel.php file:, (*7)
public function registerBundles()
{
$bundles = array(
...
new Lioshi\WonderCacheBundle\LioshiWonderCacheBundle(),
...
);
Configure the bundle by adding the following to app/config/config.yml:, (*8)
lioshi_wonder_cache:
activated: true
memcached_response:
hosts:
- { dsn: localhost, port: 11211 }
Dependencies
in Debian based systems, (*9)
apt-get install memcached php5-memcached
in Centos based systems, (*10)
yum install php-pecl-memcached
Do not forget to restart you web server after adding the Memcache module., (*11)
Commands
The wondercache:clear
command delete all cached items and wondercache:list
command can list all cache's keys and can display content of a choosen key., (*12)
Full configuration
lioshi_wonder_cache:
activated: true
included_headers_keys:
- email # Specify list of header's keys to include in url. Add only header's keys if page content return depends of. Or put ALL for all header's key
memcached_response:
hosts:
- { dsn: localhost, port: 11211, weight: 60 }
- { dsn: localhost, port: 11212, weight: 30 }
options:
compression: true
serializer: 'json'
prefix_key: ""
hash: default
distribution: 'consistent'
libketama_compatible: true
buffer_writes: true
binary_protocol: true
no_block: true
tcp_nodelay: false
socket_send_size: 4096
socket_recv_size: 4096
connect_timeout: 1000
retry_timeout: 0
send_timeout: 0
recv_timeout: 0
poll_timeout: 1000
cache_lookups: false
server_failure_limit: 0
Usage
Into a controller you can run() WonderCache and specified optionnaly entities which are linked to the controller response.
The following exemple means that the controller's response depends on (or is linked to):
- 3 packs with ids 1, 65 and 988
- 2 exports with ids 65 and 22
- all cars
And it expires before 3600s (by default duration is 0:infinite), (*13)
Exemple's code for a controller:, (*14)
$this->container->get('wonder.cache')
->run()
->addLinkedEntities(
array(
'Me\MyBundle\Entity\Pack' => array(1,65,988),
'Me\MyBundle\Entity\Export' => array(65,22),
'Me\MyBundle\Entity\Cars' => array()
)
)
->addDuration(3600)
;
Cache invalidation system
To see cache invalidation logs, just create file /tmp/wcInvalidationCache.log in your server., (*15)
touch /tmp/wcInvalidationCache.log
It's a roll log, its weight never up to 1M bytes., (*16)
With symfony toolbar you can follow how WonderCache performs.
If there's some error or warning:, (*17)
, (*18)
If all is good..., (*19)
, (*20)
... you can see more informations about how WonderCache save your time:, (*21)
, (*22)
Credits
Inspired by https://github.com/LeaseWeb/LswMemcacheBundle:
- DependencyInjection/Configuration.php
- Command/ClearCommand.php, (*23)