2017 © Pedro Peláez
 

library combinedhttpcache

Combined HttpCache with Redis centralized storage for metadata

image

itkg/combinedhttpcache

Combined HttpCache with Redis centralized storage for metadata

  • Wednesday, February 15, 2017
  • by pdenis
  • Repository
  • 7 Watchers
  • 1 Stars
  • 20 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 5 Forks
  • 5 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

README

About

This library provides integration of distributed HttpCache based on HttpCache Symfony component on Redis storage. - Metadata of request and response stay on Redis - Cache entries come from Redis and are flushed locally so that fast integration and ESI support is kept with local filesystem, (*1)

Installation

Add the library to your project thanks to composer:, (*2)

composer require itkg/combinedhttpcache

Integrate in Symfony

Edit app/AppCache.php to change HttpCache base class : ``` php <?php, (*3)

require_once DIR.'/AppKernel.php';, (*4)

use Itkg\CombinedHttpCache\HttpCache\HttpCache;, (*5)

class AppCache extends HttpCache { }, (*6)


Then change for testing in dev environment the web/app_dev.php. **Please note you have to push correct Redis configuration string as a second argument of AppCache constructor.** ``` php ... $loader = require_once __DIR__.'/../app/bootstrap.php.cache'; Debug::enable(); require_once __DIR__.'/../app/AppKernel.php'; require_once __DIR__.'/../app/AppCache.php'; $kernel = new AppKernel('dev', true); $kernel->loadClassCache(); $kernel = new AppCache($kernel, array('redis_connection' => 'tcp://127.0.0.1:6379')); // When using the HttpCache, you need to call the method in your front controller instead of relying on the configuration parameter Request::enableHttpMethodParameterOverride(); $request = Request::createFromGlobals(); $response = $kernel->handle($request); $response->send(); $kernel->terminate($request, $response);

If everything works correctly, cache can then be activated for 'prod' environment. For that, just uncomment the code lines that Symfony keeps in comment to activate the cache., (*7)

Using cache

From that point, cache annotations or explicit cache settings for a Response will be managed to make storage of the current request (or ESI request) in conformity to URL unicity and Vary headers., (*8)

See : - http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/cache.html - http://symfony.com/doc/current/book/http_cache.html, (*9)

Manage tagging for cached requests

Any request can be cached (if needed of course) and any cached request can be tagged. To rely on the same conventions has been the choice to manage tagging. For example, it will ease the process to move to Varnish even for the projects relying at the moment on this Redis version of HttpCache., (*10)

The library expects meta data tagging in responses by positionning X-ITKG-Cache-Tags. Tags must be separated thanks to ',' character. Each tag name is trimmed in case space-like characters are present (when splitting the header on comas)., (*11)

Note : - We cannot temporarily use the convention defined in FosHttpCache (https://foshttpcachebundle.readthedocs.org/en/latest/features/tagging.html) . - The tagging can be followed easily thanks to the web debug toolbar for any "master request" and any ESI request., (*12)

Internal design

The cache storage is managed in two places: - in Redis for metadata - in Redis and filesystem for content digest, (*13)

Note 1 : The double storage for content digest is quite handy because it keeps the reliability and speed for ESI management like the standard HttpCache that deals with a PHP include strategy to avoid unserialiazation and get benefits of standard opcode cache., (*14)

Note 2 : Opcode cache needs however correct memory settings because it must mostly avoid evicting cache entries to be efficient., (*15)

A default configuration value can be done however for starting., (*16)

memory = size(all php files) + size(all cache/prod/http_cache/)

In bash it becomes:, (*17)

du -h --max-depth=0 /var/frontend/cache/prod/http_cache/ /var/www

In case (for ITKG it is the case), the best is to measure memory usage thanks to APC.php script to see how it behaves., (*18)

For simple invalidation out of Symfony

A specific integration is possible for backend responsible of blocks configuration hence the need of invalidation., (*19)

``` php <?php use Itkg\CombinedHttpCache\Client\RedisClient;, (*20)

// declare correct autoloading require_once DIR.'/../vendor/autoload.php';, (*21)

// create the client $client = new RedisClient('tcp://127.0.0.1:6379');, (*22)


Then the client can be used this way: ``` php // Here, we remove the keys being both in tag-a and tag-b which means an intersection is computed to make the removal $res = $client->removeKeysFromTags(array('tag-a', 'tag-b')); // Here, all the keys present in tag-a are removed and all keys in tag-b too (it makes a union). $res = $client->removeKeysFromTags(array(array('tag-a'), array('tag-b')));

Note : manual key set/get/del should not be performed here because key hashing is a very complex task that only HttpCache should manage., (*23)

License

See LICENSE file, (*24)

The Versions

15/02 2017

dev-master

9999999-dev

Combined HttpCache with Redis centralized storage for metadata

  Sources   Download

MIT

The Requires

 

by Gilles Taupenas

15/02 2017

dev-SOL-1337

dev-SOL-1337

Combined HttpCache with Redis centralized storage for metadata

  Sources   Download

MIT

The Requires

 

by Gilles Taupenas

24/08 2016

dev-SOL-874-exclude-specific-tags

dev-SOL-874-exclude-specific-tags

Combined HttpCache with Redis centralized storage for metadata

  Sources   Download

MIT

The Requires

 

by Gilles Taupenas

08/08 2016

v1.1.0

1.1.0.0

Combined HttpCache with Redis centralized storage for metadata

  Sources   Download

MIT

The Requires

 

by Gilles Taupenas

16/05 2016

v1.0.0

1.0.0.0

Combined HttpCache with Redis centralized storage for metadata

  Sources   Download

MIT

The Requires

 

by Gilles Taupenas

21/10 2015

dev-itkg-rcarles-patch-1

dev-itkg-rcarles-patch-1

Combined HttpCache with Redis centralized storage for metadata

  Sources   Download

MIT

The Requires

 

by Gilles Taupenas