anime-db/cache-time-keeper-bundle

Cache time keeper

v1.0.6 2015-01-16 08:29 UTC

README

Latest Stable Version Latest Unstable Version Build Status Code Coverage SensioLabsInsight License

The library is intended for quick get a date change entities or entire project, without reference to a database in Symfony2 projects.

Library tracks changes in entities and stores date modified.

Notice: library tracks only changing patterns in general, not each one separately.

Installation

Step 1: Download the CacheTimeKeeperBundle

Add the following to the require section of your composer.json file:

"anime-db/cache-time-keeper-bundle": "1.0.*"

And update your dependencies

Step 2: Enable the bundle

Finally, enable the bundle in the kernel:

// app/appKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new AnimeDb\Bundle\CacheTimeKeeperBundle\AnimeDbCacheTimeKeeperBundle(),
    );
}

Usage

Getting a last modified date of the project:

$date = $this->get('cache_time_keeper')->getMax();

or

use AnimeDb\Bundle\CacheTimeKeeperBundle\Service\Keeper;

$date = $this->get('cache_time_keeper')->get(Keeper::LAST_UPDATE_KEY);

Adding a new value:

$this->get('cache_time_keeper')->set('foo', new \DateTime());

Getting the oldest date for a set of keys, taking into account the date of the change project:

$date = $this->get('cache_time_keeper')->getMax('foo');

Remove value:

$this->get('cache_time_keeper')->remove('foo');

Use in controllers:

namespace Acme\Bundle\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class HomeController extends Controller
{
    public function indexAction(Request $request, $id)
    {
        // caching
        $response = new Response();
        // cache becomes invalid if changing at least one of the pages, the catalog or the updated project
        $response->setLastModified(
            $this->get('cache_time_keeper')->getMax(['AcmeDemoBundle:Page', 'AcmeDemoBundle:Catalog'])
        );

        // response was not modified for this request
        if ($response->isNotModified($request)) {
            return $response;
        }

        // get entities
        $page = $this->getDoctrine()->getManager()->find('AcmeDemoBundle:Page', $id);
        $catalogs = $this->getDoctrine()->getManager()->getRepository('AcmeDemoBundle:Catalog')->findAll();

        return $this->render(
            'AcmeDemoBundle:Home:index.html.twig',
            [
                'page' => $page,
                'catalogs' => $catalogs
            ],
            $response
        );
    }
}

You can use a simplified method of configuring a response.

Getting the default response object:

$response = $this->get('cache_time_keeper')->getResponse();

Getting json response limited lifetime:

use Symfony\Component\HttpFoundation\JsonResponse;

$response = $this->get('cache_time_keeper')->getResponse('foo', 3600, new JsonResponse());

Use in controllers:

namespace Acme\Bundle\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class HomeController extends Controller
{
    public function indexAction(Request $request, $id)
    {
        // cache becomes invalid if changing at least one of the pages, the catalog or the updated project
        $response = $this->get('cache_time_keeper')->getResponse(['AcmeDemoBundle:Page', 'AcmeDemoBundle:Catalog']);

        // response was not modified for this request
        if ($response->isNotModified($request)) {
            return $response;
        }

        // get entities
        $page = $this->getDoctrine()->getManager()->find('AcmeDemoBundle:Page', $id);
        $catalogs = $this->getDoctrine()->getManager()->getRepository('AcmeDemoBundle:Catalog')->findAll();

        return $this->render(
            'AcmeDemoBundle:Home:index.html.twig',
            [
                'page' => $page,
                'catalogs' => $catalogs
            ],
            $response
        );
    }
}

Drivers

In the bundle there are several the data storage drivers.

  • Dummy (cache_time_keeper.driver.dummy) - stores data in a temporary variable, within the current thread of execution program.
  • File (cache_time_keeper.driver.file) - stores data in a file cache. Directory for storing files can be overridden by changing the parameter cache_time_keeper.dir.
  • Shmop (cache_time_keeper.driver.shmop) - stores the data in memory using PHP extension shmop. For work of this driver, you must install anime-db/shmop.
  • Multi (cache_time_keeper.driver.multi) - driver is a wrapper for multiple drivers. Takes the driver with quick access to the data (stored in memory) and slow (stored on the hard drive), and receives data on the possibility of fast drivers and if not luck reads data from slow. To change the drivers of fast and slow access to override the cache_time_keeper.driver.multi.fast and cache_time_keeper.driver.multi.slow respectively.

By default used the driver File, to change the driver override the parameter cache_time_keeper.driver.

License

This bundle is under the MIT license. See the complete license in the bundle: LICENSE