Giter Site home page Giter Site logo

epiphanysearch / service-cache-bundle Goto Github PK

View Code? Open in Web Editor NEW
0.0 13.0 1.0 200 KB

Allows caching of Symfony2 service method calls. This is achieved by annotating the required methods in the service class.

Home Page: http://www.epiphanysearch.co.uk/blog/2013/10/symfony2-service-data-caching-using-annotations/

License: MIT License

PHP 100.00%

service-cache-bundle's Introduction

Epiphany Service Cache Bundle

Allows caching of Symfony2 service method calls. This is achieved by annotating the required methods in the service class.

This functionality may be useful where:

  1. An application makes expensive calls to an API which could be cached or shared with another application, reducing overall API calls.
  2. An application makes time consuming calls to a service which could be cached for faster performance.
  3. As a means to hook into a method call for a service, then capture its parameters and return value.

Configuration

This assumes you're working with a Symfony2 (v2.3) application, and using composer for package management.

There are four steps required to use this bundle.

  • Add this package to your composer.json file and run a composer update, add the package to you AppKernel
  • Update your application's app/autoload.php file to include a call to ProxyGenerator::registerNamespace(), as below, so the application can load proxy classes
use Epiphany\ServiceCacheBundle\Proxy\ProxyGenerator;

....

ProxyGenerator::registerNamespace($loader,__DIR__); 

return $loader;
  • Tag any services you want to register for caching, and tag a service which implements the Epiphany\ServiceCacheBundle\Cache\ServiceCacheInterface. This should be done in your service.yml/xml files
services:
    # note the 'epiphany_service_cache.register' tag - this indicates our weather data
    # service should have some (or all) of its method calls cached 
    weather_data_service:
        class: Epiphany\ServiceCacheDemoBundle\Service\WeatherDataService
        arguments: []
        tags:
            - { name: epiphany_service_cache.register}

    # note the 'epiphany_service_cache.cache' tag - this indicates the service
    # should be used by the service_cache as the caching mechanism 
    simple_cache_service:
        class: Epiphany\ServiceCacheDemoBundle\Service\SimpleCacheService
        arguments: [localhost, 12345]
        tags:
            - { name: epiphany_service_cache.cache}
  • Annotate any services you want to cache..
class WeatherDataService
{
    /**
     * Get the weather forecast for a date and location
     *
     * @service-cache-enable
     *
     * @service-cache-key param date
     * @service-cache-key param location
     * @service-cache-key date Y-m-d-H
     *
     * @service-cache-option compressed y 
     * 
     * @service-cache-expires 0
     * 
     * @param  DateTime $date     
     * @param  string   $location 
     * @return array    forecast data
     */
    public function forecastForDate(\DateTime $date, $location) {

        //....

Annotations

@service-cache-enable - This marks a method for use with the Service Cache

@service-cache-key <date|param> <value> - One or more of these annotations must be used to define a unique key to store any data against. Any method parameter which can be cast as a string, or the current date (with a format string) can be used.

@service-cache-option <name> <value> - Name/value pairs that will be passed to the caching service during get/set operations via an associative array. See the Epiphany\ServiceCacheBundle\Cache\ServiceCacheInterface. This allows a the user to pass extra information that might be specific to their implementation of the cache service such as compression, data expiry, (collection name if you're using MongoDB)

@service-cache-expires <n> - Expiry in seconds of the cached data. Zero for never expires.

Implementing the Cache Mechanism

It is at the user's discretion as to what means of caching should be used. All that is required is that the user provides a Symfony2 service marked with the 'epiphany_service_cache.cache' tag, which implements the Epiphany\ServiceCacheBundle\Cache\ServiceCacheInterface. The getDataForKey() method should return a null value when no data can be retrieved from the cache.

Notes

This package expects the Symfony2 logger service to be available in the container (usually monolog, though any service implementing the Symfony\Component\HttpKernel\Log\LoggerInterface can be used).

To insert the caching layer, this package produces proxy objects for any marked service, and then overrides the service definition in Symfony's pre-optimization compiler pass of the service container. Currently, the proxy objects must be manually deleted from the application's app/cache/dsproxy directory whenever their service is updated.

class EpiphanyServiceCacheBundle extends Bundle
{
    public function build(ContainerBuilder $container)
    {
        parent::build($container);

        // this pass will override standard service classes with our generated proxy classes
        $container->addCompilerPass(new OverrideServiceCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION);
    }
}

Release Notes

v1.0.2 - September 23 2013

  • Readme updates.

v1.0.1 - September 23 2013

  • Readme updates.

v1.0.0 - September 23 2013

  • Initial Commit, basic functionality.

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.