2017-05-16 86 views
0

我使用Symfony 3.1的cache component來存儲我的實體的一些自定義元數據。只要在與這些元數據相關聯的任何文件中進行更改,我都想使其無效。Symfony 3.1緩存組件中的緩存失效

我沒有找到一種方法來告訴Symfony或緩存組件,特別是監視一組特定文件的更改,我錯過了什麼?

下面我用它來創建我的緩存條目池代碼:

<?php 
class MetadataCacheFactory implements MetadataCacheFactoryInterface 
{ 
    const CACHE_NAMESPACE = 'my_namespace'; 

    /** @var string */ 
    protected $cacheDir; 

    public function __construct(KernelInterface $kernel) 
    { 
     $this->cacheDir = $kernel->getCacheDir(); 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function create(): CacheItemPoolInterface 
    { 
     return AbstractAdapter::createSystemCache(self::CACHE_NAMESPACE, 0, null, $this->cacheDir); 
    } 
} 

和代碼使用它:

<?php 
class ExampleMetadataFactory implements MetadataFactoryInterface 
{ 
    const CACHE_KEY = 'example_metadata'; 

    [...] 

    /** @var ExampleMetadata */ 
    protected $metadata; 

    public function __construct(MetadataCacheFactoryInterface $cacheFactory) 
    { 
     $this->cache = $cacheFactory->create(); 
     $this->metadata = null; 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function create(): ExampleMetadata 
    { 
     if ($this->metadata !== null) { 
      return $this->metadata; 
     } 
     try { 
      $cacheItem = $this->cache->getItem(md5(self::CACHE_KEY)); 
      if ($cacheItem->isHit()) { 
       return $cacheItem->get(); 
      } 
     } catch (CacheException $e) { 
      // Ignore 
     } 
     $this->metadata = $this->createMetadata(); 
     if (!isset($cacheItem)) { 
      return $this->metadata; 
     } 
     $cacheItem->set($this->metadata); 
     $this->cache->save($cacheItem); 
     return $this->metadata; 
    } 
} 

當我看到在運行時代碼中,AbstractAdapter選擇放棄我PhpFilesAdapter(公平的開發我猜)。

但當我看這個適配器的代碼,我發現這一點:

<?php 
protected function doFetch(array $ids) { 
    [...] 

    foreach ($ids as $id) { 
     try { 
      $file = $this->getFile($id); 
      list($expiresAt, $values[$id]) = include $file; 
      if ($now >= $expiresAt) { 
       unset($values[$id]); 
      } 
     } catch (\Exception $e) { 
      continue; 
     } 
    } 
} 

所以沒有邏輯可言檢查期滿除了到期日。

你知道一種方法來使文件更改時的緩存無效(當然在開發環境中)嗎?或者讓我自己來實現它..?

感謝您的幫助。

回答

1

我找到了解決方案:ConfigCache。您可以給它一組資源來觀察,並且每次讀取緩存時都會檢查它們的最後修改時間。

上面的例子變成類似:

<?php 
class MetadataCacheFactory implements MetadataCacheFactoryInterface 
{ 
    const CACHE_NAMESPACE = 'my_namespace'; 

    /** @var \Symfony\Component\HttpKernel\KernelInterface */ 
    protected $kernel; 

    public function __construct(KernelInterface $kernel) 
    { 
     $this->kernel = $kernel; 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function create(): ConfigCache 
    { 
     return new ConfigCache($this->kernel->getCacheDir().'/'.self::CACHE_NAMESPACE, $this->kernel->isDebug()); 
    } 
} 

並使用它的代碼:

<?php 
class ExampleMetadataFactory implements MetadataFactoryInterface 
{ 
    const CACHE_KEY = 'example_metadata'; 

    [...] 

    /** @var ExampleMetadata */ 
    protected $metadata; 

    public function __construct(MetadataCacheFactoryInterface $cacheFactory) 
    { 
     $this->cache = $cacheFactory->create(); 
     $this->metadata = null; 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function create(): ExampleMetadata 
    { 
     if ($this->metadata !== null) { 
      return $this->metadata; 
     } 
     try { 
      if ($this->cache->isFresh()) { 
       $this->metadata = unserialize(file_get_contents($this->cache->getPath())); 
       return $this->metadata; 
      } 
     } catch (CacheException $e) { 
      // Ignore 
     } 
     $resources = []; 
     $this->metadata = $this->createMetadata($resources); 
     $this->cache->write(serialize($this->metadata), $resources); 
     return $this->metadata; 
    } 

    /** 
    * Creates the metadata. 
    * 
    * @param array $resources 
    * 
    * @return ExampleMetadata 
    */ 
    protected function createMetadata(&$resources): ExampleMetadata 
    { 
     $metadata = new ExampleMetadata(); 
     $resourcesCollection = $this->resourcesCollectionFactory->create(); 
     foreach ($resourcesCollection as $resourceClass => $resourcePath) { 
      // The important part, it's an instance of \Symfony\Component\Config\Resource\FileResource. 
      $resources[] = new FileResource($resourcePath); 
      $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); 
      $metadata->registerResourceMetadata($resourceMetadata); 
     } 
     return $metadata; 
    } 
} 

在此希望它可以幫助別人。