emag/cache-bundle

This package is abandoned and no longer maintained. The author suggests using the emag-tech-labs/annotation-cache-bundle package instead.

CacheBundle by eMAGTechLabs

4.2.0 2018-10-01 09:14 UTC

README

Installation

In order to have caching on methods you need to install it using composer:

  1. Add requirement:
   $ composer require emag/cache-bundle
  1. Add to your app/AppKernel.php
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            //...
            
            new Emag\CacheBundle\EmagCacheBundle(),
            
            //...
        ];
        
        //...
    }
 
    //....
}
  1. Configure the bundle required info

You have to configure the name of the service that is PSR6 compliant, that means it will have to implement Psr\Cache\CacheItemPoolInterface:

    # app/config/services.yml
    
    services:
        cache.array:
            class: Symfony\Component\Cache\Adapter\ArrayAdapter
            
        cache.redis:
            class: Symfony\Component\Cache\Adapter\RedisAdapter
            arguments: ['@predis']
    #app/config/config.yml
    
    # eMAG CachingBundle
    emag_cache:
        provider: cache.redis
        ignore_namespaces:
          - 'Symfony\\'
          - 'Doctrine\\'
          - 'Twig_'
          - 'Monolog\\'
          - 'Swift_'
          - 'Sensio\\Bundle\\'

How to use

Add @Cache annotation to the methods you want to be cached:

    
    use Emag\CacheBundle\Annotation\Cache;
    
   /**
     * @Cache(cache="<put your prefix>", [key="<name of argument to include in cache key separated by comma>",  [ttl=600, [reset=true ]]])
     */

Here is an example from a service:

    
    namespace AppCacheBundle\Service;
    
    use Emag\CacheBundle\Annotation\Cache;
    
    class AppService
    {
        
        /**
         * @Cache(cache="app_high_cpu", ttl=60)
         *
         * @return int
         */
        public function getHighCPUOperation()
        {
            // 'Simulate a time consuming operation';
            
            sleep(10);
    
            return 20;
        }
    }

Want to contribute?

Submit a PR and join the fun.

Enjoy!