Skip to content

lioshi/WonderCacheBundle

Repository files navigation

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

knpbundles.com

How works WonderCache screenshot

screenshot

WonderCache is there at request and bypasses all framework. As a proxy. But WonderCache knows when invalidate its cache.

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:

{
    require: {
        "lioshi/wonder-cache-bundle": "dev-master",
        ...
    }
}

The next thing you should do is install the bundle by executing the following command:

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:

public function registerBundles()
{
    $bundles = array(
        ...
        new Lioshi\WonderCacheBundle\LioshiWonderCacheBundle(),
        ...
    );

Configure the bundle by adding the following to app/config/config.yml:

    lioshi_wonder_cache:
        activated: true
        memcached_response:
            hosts: 
                - { dsn: localhost, port: 11211 }

Dependencies

in Debian based systems

apt-get install memcached php5-memcached

in Centos based systems

yum install php-pecl-memcached 

Do not forget to restart you web server after adding the Memcache module.

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.

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)

Exemple's code for a controller:

    $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.

touch /tmp/wcInvalidationCache.log

It's a roll log, its weight never up to 1M bytes.

Profiler's informations

With symfony toolbar you can follow how WonderCache performs. If there's some error or warning:

screenshot

If all is good...

screenshot

... you can see more informations about how WonderCache save your time:

screenshot

Credits

Inspired by https://github.com/LeaseWeb/LswMemcacheBundle:

  • DependencyInjection/Configuration.php
  • Command/ClearCommand.php

About

A full response cache with automatic invalidation.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published