sputnik/cache-bundle

Doctrine cache integration.

v0.2.0 2013-08-17 22:21 UTC

README

This bundle integrates Doctrine caching library into Symfony. This is not a replacement for HTTP cache in any way. Please, use HTTP cache when relevant.

Main features
  • Ability to configure and use multiple cache drivers available in Doctrine.
  • Cache twig code blocks right in the template with {% sputnikcache %} tag.
  • Console command to flush cache.
  • Register your own cache drivers not implemented by Doctrine.

Note: There is a sample Sandbox application with Sputnik bundles installed and configured - https://github.com/sputnik-project/sandbox.

Build Status

Quick example

Service usage
class MyController extends Controller
{
    /**
     * Default driver usage example.
     */
    public function myAction()
    {
        $cache = $this->get('sputnik_cache.cache_manager')->getDefaultDriver();

        if (false === ($data = $cache->fetch('my_key'))) {
            // Do some heavy lifting to populate $data
            $data = $this->doResourceIntensiveStuff();
            // Save for 1 hour.
            $cache->save('my_key', $data, 3600);
        }

        // Do something with $data

        return $this->render(...);
    }

    /**
     * Use any configured cache driver.
     */
    public function anotherAction()
    {
        $cache = $this->get('sputnik_cache.cache_manager')->getDriver('apc')

        if (false === ($data = $cache->fetch('my_key'))) {
            $data = $this->doResourceIntensiveStuff();
            $cache->save('my_key', $data); // Save $data without time limit restriction.
        }

        return $this->render(...);
    }
}
Twig usage
{# Use default driver and lifetime #}
{% sputnikcache key="footer" %}
    <your-html-here>
{% sputnikcache %}

{% sputnikcache key="footer" lifetime=3600 driver="apc" %}
    <your-html-here>
{% sputnikcache %}

Documentation

Documentation can be found in Resources/doc. You can start with installation instructions.

API documentation.

Resources

Doctrine is not the only library implementing some sort of caching out there. However, I believe the simpliest and easy to use one. Some alternatives:

There is an ongoing discussion in Symfony community regarding "native" Symfony Cache component. Please, refer to below links for more information: