Giter Site home page Giter Site logo

httprediscache's Introduction

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.

Requirements

HttpRedisCache is only supported by symfony 2.3 . Any other versions may or may not work. Redis php extension installed.

Installation

Add HttpRedisCache to your application's composer.json file

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

Install the library and it's dependencies using the following command:

$ php composer.phar update solilokiam\httprediscache

Activate your symfony internal http cache:

// 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:

// 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: ~

You can change it adding some method implementations in AppCache:

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

You can also configure key prefixes adding some implementation methods in AppCache:

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

// 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

Author

  • Miquel Company Rodriguez (@solilokiam)

TODO

  • Improve Tests
  • Symfony 2.4 support

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.