emag/cache-bundle

此包已被废弃且不再维护。作者建议使用emag-tech-labs/annotation-cache-bundle包。

eMAGTechLabs的CacheBundle

4.2.0 2018-10-01 09:14 UTC

README

安装

为了在方法上启用缓存,您需要使用composer安装它

  1. 添加需求
   $ composer require emag/cache-bundle
  1. 将以下代码添加到您的app/AppKernel.php中
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            //...
            
            new Emag\CacheBundle\EmagCacheBundle(),
            
            //...
        ];
        
        //...
    }
 
    //....
}
  1. 配置包所需信息

您必须配置一个符合PSR6规范的服务名称,这意味着它将需要实现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\\'

如何使用

将@Cache注解添加到您想要缓存的函数上

    
    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 ]]])
     */

以下是一个服务器的示例

    
    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;
        }
    }

想要贡献?

提交一个PR并加入我们的乐趣。

享受!