簡體   English   中英

DoctrineCacheBundle:通過SYmfony路由刷新緩存

[英]DoctrineCacheBundle: Flush cache via SYmfony route

在我的Symfony項目中,我使用DoctrineCacheBundle,當我訪問http://example.com/api/cache/flush時,我想取消緩存(刷新)任何緩存的鍵。

唯一的原因是因為我有一些應用程序訪問上面的URL,以便刪除所有緩存的結果。

到目前為止,我搜索了DoctrineCacheBundle使用命令來取消緩存結果(如通過php ./bin/console list doctrine:cache命令所看到的):

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

但是,我該如何以編程方式執行此操作?

最好的方法是按照以下兩種方法之一制作自己的緩存適配器:

方法1:使用專用的管理器進行緩存:

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

方法2:像“教義”那樣做:

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
}

也可以查看此問題,以獲取有關如何使用它的更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM