2017 © Pedro Peláez
 

library httprediscache

An HttpCache extension with redis for Symfony 2

image

solilokiam/httprediscache

An HttpCache extension with redis for Symfony 2

  • Monday, October 19, 2015
  • by solilokiam
  • Repository
  • 2 Watchers
  • 6 Stars
  • 405 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 7 Forks
  • 2 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

README

What is HttpRedisCache?

Redis Cache is an improvement to Symfony 2 internal http cache. All cached data is stored in Redis instead of filesystem. This can be useful when not much spacedisk is avaliable, or you want to share the cache data across several servers., (*1)

Requirements

HttpRedisCache is only supported by symfony 2.3 . Any other versions may or may not work. Redis php extension installed., (*2)

Installation

Add HttpRedisCache to your application's composer.json file, (*3)

{
    "require": {
        "solilokiam/httprediscache": "dev-master"
    }
}

Install the library and it's dependencies using the following command:, (*4)

$ php composer.phar update solilokiam\httprediscache

Activate your symfony internal http cache:, (*5)

// web/app.php
require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';
require_once __DIR__.'/../app/AppCache.php';

use Symfony\Component\HttpFoundation\Request;

$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
// wrap the default AppKernel with the AppCache one
$kernel = new AppCache($kernel);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

Make AppCache class extend HttpRedisCache instead on HttpCache like this:, (*6)

// app/AppCache.php
require_once __DIR__.'/AppKernel.php';


class AppCache extends \Solilokiam\HttpRedisCache\HttpRedisCache
{

}

Configuration

By default this library has the following configuration: - redis host: localhost - redis port: 6379 - redis password: ~ - redis database: ~ - redis options: ~, (*7)

You can change it adding some method implementations in AppCache:, (*8)

// app/AppCache.php
require_once __DIR__.'/AppKernel.php';


class AppCache extends \Solilokiam\HttpRedisCache\HttpRedisCache
{
    public function getConnectionParams()
    {
        return array(
            'host' => 'localhost',
            'port' => '6739',
            'password' => 'somepassword',
            'database' => 'somedatabase',
            'options' => array(
                Redis::OPT_SERIALIZER => Redis::SERIALIZER_NONE,
                Redis::OPT_PREFIX => 'myAppName:'
            )
        );
    }
}

The only required array element is host others are optional., (*9)

You can also configure key prefixes adding some implementation methods in AppCache:, (*10)

// app/AppCache.php
require_once __DIR__.'/AppKernel.php';


class AppCache extends \Solilokiam\HttpRedisCache\HttpRedisCache
{
    ...

    public function getDigestKeyPrefix()
    {
        return 'hrd';
    }

    public function getLockKey()
    {
        return 'hrl';
    }

    public function getMetadataKeyPrefix()
    {
        return 'hrm';
    }
    ...
}

Remember that you can configure other cache settings the sameway you do with default symfony internal http cache., (*11)

// app/AppCache.php
use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache;

class AppCache extends HttpCache
{
    protected function getOptions()
    {
        return array(
            'debug'                  => false,
            'default_ttl'            => 0,
            'private_headers'        => array('Authorization', 'Cookie'),
            'allow_reload'           => false,
            'allow_revalidate'       => false,
            'stale_while_revalidate' => 2,
            'stale_if_error'         => 60,
        );
    }
}

Build Status

Build Status SensioLabsInsight Latest Stable Version Total Downloads Latest Unstable Version License, (*12)

Author

  • Miquel Company Rodriguez (@solilokiam)

TODO

  • Improve Tests
  • Symfony 2.4 support

The Versions

19/10 2015

dev-master

9999999-dev

An HttpCache extension with redis for Symfony 2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Miquel Company Rodriguez