Developed with love by KnpLabs Hire us for your project!
53

KnpZendCacheBundle

by KnpLabs

Integrates Zend Cache framework into Symfony2. -- THIS PROJECT IS NO LONGER MAINTAINED by KnpLabs -- Contact us if you want to be the official maintainer...

This project is no longer maintained by KnpLabs


KnpZendCacheBundle

In Symfony2 you can use HTTP cache.
That's great to cache a page or part of the page.

But what if you want to cache a variable? That's where you should use KnpZendCacheBundle.

To avoid code duplication we use the well-known Zend Cache component in our Symfony2 application.

It works great and already has all sort of options - should you need it.

Behind the scene

This bundle allows to configure a Zend\Cache\Manager, and instanciate it, from the DIC.

It does not contain any caching logic: that's Zend Cache's role.

So you should read the Zend Cache documentation
if you need anything of the ordinary.

See how to declare cache templates, available cache frontends and cache backends.

Installation

Download KnpZendCacheBundle in vendor/bundles/Knp/Bundle/ZendCacheBundle dir

If you use git:

git submodule add http://github.com/KnpLabs/KnpZendCacheBundle.git vendor/bundles/Knp/Bundle/ZendCacheBundle

Download zend-cache to your vendor/Zend/Cache dir

If you use git:

git submodule add http://github.com/KnpLabs/zend-cache.git vendor/Zend/Cache
git submodule add http://github.com/KnpLabs/zend-filter.git vendor/Zend/Filter

Add the new namespaces to your autoloader

<?php
// app/autoload.php
$loader->registerNamespaces(array(
    'Knp'                       => __DIR__.'/../vendor/bundles',
    'Zend'                      => __DIR__.'/../vendor',
    // ...
));

Add KnpZendCacheBundle to your application kernel

<?php
// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new Knp\Bundle\ZendCacheBundle\KnpZendCacheBundle(),
        // ...
    );
}

Things you should know about Zend Cache

Frontends: what do you want to cache?

In Zend Framework caching is operated by frontends:

  • Core is a generic cache frontend and is extended by other classes. You will use this most of the time.
  • Output is an output-capturing frontend. It utilizes output buffering in PHP to capture everything between its start() and end() methods.
  • FunctionFrontend caches the results of function calls. It has a single main method named call() which takes a function name and parameters for the call in an array.
  • ClassFrontend is different from Function because it allows caching of object and static method calls
  • File is a frontend driven by the modification time of a "master file". It's really interesting for examples in configuration or templates issues. It's also possible to use multiple master files.
  • Page is like Output but designed for a complete page. It's impossible to use Page for caching only a single block.
  • Capture is Page but this class is specifically designed to operate in concert only with the Static backend to assist in caching entire pages of HTML / XML or other content to a physical static file on the local filesystem.

You can find more infos and options on Zend Framework cache frontends.

Backends: How do you want to cache it?

Cache records are stored through backend adapters:

  • File stores cache records into files
  • Memcached stores cache records into a memcached server
  • Sqlite stores cache records into a SQLite database
  • Apc stores cache records in shared memory through the APC extension
  • Xcache stores cache records in shared memory through the XCache extension
  • StaticBackend works in concert with Zend_Cache_Frontend_Capture (the two must be used together) to save the output from requests as static files. This means the static files are served directly on subsequent requests without any involvement of PHP or Zend Framework at all.
  • TwoLevels stores cache records in two other backends: a fast one (but limited) like Apc, Memcache... and a "slow" one like File, Sqlite
  • ZendPlatform uses content caching API of the Zend Platform product.

You can find more infos and options on Zend Framework cache backends.

KnpZendCacheBundle usage

Let's say you have a variable $myLastTweets.

To compute this variable, you have to call an API, decode some content, create some objects… this takes time.
You only want to compute it every 2 hours.

  • We'll define a Core frontend with a 2 hours life time and serialization of the variable
  • We'll use a File backend for now. In production, we could perhaps decide to use Memcache or APC : it's just a matter of changing one config file

So in order to do that, we'll have to:

  • Define a new cache template in our config file
  • Use it from the service container

1 - Define a new cache template

# app/config.yml
knp_zend_cache:
    templates:
        tweets_and_stuff:
            frontend:
                name: Core
                options:
                    lifetime: 7200
                    automatic_serialization: true
            backend:
                name: File
                options:
                    cache_dir: %kernel.root_dir%/cache/%kernel.environment%

2 - Use it

Get the cache you declared from the service container:

<?php

// Given you have access to the container $container
$cache = $container->get('knp_zend_cache.manager')->getCache('tweets_and_stuff');

// Or if you are in a controller:
$cache = $this->get('knp_zend_cache.manager')->getCache('tweets_and_stuff');

// see if a cache already exists:
if (false === ($myLastTweets = $cache->load('last_tweets'))) {

    // cache miss: call the webservice and do stuff
    $myLastTweets = $tweetService->doComplexStuff();

    $cache->save($myLastTweets, 'last_tweets');
}

return $myLastTweets;

That's it!

Copyright (c) 2010 Knp Labs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
  • Mention in README file that this bundle is no longer maintained
    By stloyd, 4 years ago
  • Lock on ZF beta1
    By stloyd, 5 years ago
  • Update composer.json with usage of official ZF package repo
    By stloyd, 5 years ago
  • Merge pull request #4 from mheleniak/patch-1
    By stloyd, 5 years ago
  • fixed composer.json
    By Mateusz Heleniak, 5 years ago
  • Update composer.json
    By stloyd, 5 years ago
  • Update composer.json
    By stloyd, 5 years ago
  • Update composer.json
    By stloyd, 5 years ago
  • Trying to fix composer.json
    By stloyd, 5 years ago
  • Merge pull request #3 from stloyd/composer
    By stloyd, 5 years ago
  • Add composer.json
    By stloyd, 5 years ago
  • Fix case in repo urls
    By lwagner, 5 years ago
  • [doc] Add the dependency to zend-filter
    By mbontemps, 6 years ago
  • [doc] use an existing directory in the example
    By mbontemps, 6 years ago
  • [doc] fix tag name
    By mbontemps, 6 years ago
  • [doc] Improve the doComplexStuff part
    By mbontemps, 6 years ago
  • Fix CS in doc and old copy and paste
    By mbontemps, 6 years ago
  • Fix doc. You should not use PHP and JS at the same time.
    By mbontemps, 6 years ago
  • Tweak some texts in the readme
    By mbontemps, 6 years ago
  • Rewrite the documentation so that it's easier to understand how it works
    By mbontemps, 6 years ago
  • Change Knplabs to Knp. BC break before Symfony2 release.
    By mbontemps, 6 years ago
  • Modernize DIC extension
    By ornicar, 6 years ago
  • fixed removed class
    By ornicar, 6 years ago
  • Update XML namespaces
    By ornicar, 6 years ago
  • Upgrade DIC extension to latest Symfony2 requirements
    By ornicar, 6 years ago
  • Update namespace in DIC extension
    By ornicar, 6 years ago
  • Update dependency injection extension to make use of FileLocator
    By ornicar, 6 years ago
  • Update DIC extension to handle multiple configs
    By ornicar, 6 years ago
  • Remove shared attribute from services definitions
    By ornicar, 6 years ago
  • Fix bundle path
    By ornicar, 6 years ago