doushou9028 2017-11-16 12:09
浏览 71
已采纳

DoctrineCacheBundle:通过SYmfony路由刷新缓存

Over my Symfony project I use the DoctrineCacheBundle and I want when I visit http://example.com/api/cache/flush I want to uncache (flush) any cached key.

The sole reason is because I have applications that visit the url above in order to remove any cached result.

As far I searched the DoctrineCacheBundle uses a command in order to uncache the cached results (as you can see via php ./bin/console list doctrine:cache command):

Symfony 3.3.12 (kernel: app, env: dev, debug: true)

Usage:
  command [options] [arguments]

Options:
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
  -e, --env=ENV         The environment name [default: "dev"]
      --no-debug        Switches off debug mode
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands for the "doctrine:cache" namespace:
  doctrine:cache:clear     Flush a given cache
  doctrine:cache:contains  Check if a cache entry exists
  doctrine:cache:delete    Delete a cache entry
  doctrine:cache:flush     [doctrine:cache:clear] Flush a given cache
  doctrine:cache:stats     Get stats on a given cache provider

But how can I do this programmatically?

  • 写回答

1条回答 默认 最新

  • dongru2019 2017-11-16 12:09
    关注

    The best way is to make your own Cache adapter by following one of theese 2 approaches:

    Approach 1: Use dedicated manager for uncaching:

    namespace AppBundle\CacheManagers;
    
    use Doctrine\Common\Cache\FlushableCache;
    
    class PurgeAllcachesManager
    {
    
        /**
         * @var FlushableCache
         */
        private $purgeCachingHandler=null;
    
        public function __construct(FlushableCache $purgeCachingHandler)
        {
            $this->purgeCachingHandler=$purgeCachingHandler;
        }
    
        /**
         * Method that does all the dirty job to uncache all the keys
         */
        public function uncache()
        {
            $this->purgeCachingHandler->flushAll();
        }
    }
    

    Approach2: Do as Doctrine does:

    namespace AppBundle\CacheManagers;
    
    use Doctrine\Common\Cache\Cache as CacheHandler;
    
    class PurgeAllcachesManager
    {
    
        /**
         * @var CacheHandler
         */
        private $cacheHandler=null;
    
        public function __construct(CacheHandler $cacheHandler)
        {
            $this->cacheHandler=$cacheHandler;
        }
    
        /**
         * Method that does all the dirty job to uncache all the keys
         * @throws Exception
         */
        public function uncacheAllKeys()
        {
            if(!method_exists($this->purgeCachingHandler) ){
              throw new Exception("You cannot empty the cache");
            }
            $this->purgeCachingHandler->flushAll();
        }
    
        //Yet another methods to handle the cache
    }
    

    Also have a look on this question for extra info on how to use it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。