2017-05-16 43 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; 
    } 
} 

在此希望它可以帮助别人。