Symfony使用liipImagine删除缓存的图像

问题描述:

我想要删除或更新源图像时删除缓存的图像(这是用LiipImagineBundle创建的)。我已经发现它可以使用CacheManager完成(https://github.com/liip/LiipImagineBundle/issues/132) 问题是,我无法弄清楚如何正确使用它。这样做还有什么我需要添加(如图书馆),我的代码,尽管这三条线:Symfony使用liipImagine删除缓存的图像

$cacheManager = $this->get('liip_imagine.cache.manager'); 
    $cacheManager->resolve($this->getRequest(),$pngPath,$filter); 
    $cacheManager->remove($pngPath, $filter); 

我认为应该有类似

$cacheManager = new CacheManager(); 

我真的很感激,如果有人可以解释我如何这样做更详细。

因此,例如在你的控制器:

/** 
* Remove an image in the cache based on its relative path and the filter applied to it 
* 
* @param string $path 
* @param string $filter 
* 
* @return void 
*/ 
protected function removeCachedImageAction($path, $filter) 
{ 
    $cacheManager = $this->container->get('liip_imagine.cache.manager'); 

    // Remove the cached image corresponding to that path & filter, if it is stored 
    if ($cacheManager->isStored($path, $filter)) { 
     $cacheManager->remove($path, $filter); 
    } 

} 

/** 
* An action that doesn't do much except testing the function above 
* 
* @param Request $request 
* 
* @return void 
*/ 
protected function whateverAction(Request $request) 
{ 
    $path = //... probably from the request 
    $filter = //... probably from the request 

    // Remove the cached image 
    $this->removeCachedImage($path, $filter); 

    // ... 

} 

正如你可以在CacheManager看到,你想使用的功能是:

public function remove($paths = null, $filters = null){ ... } 
  • 如果$pathsnull,该函数假定您要删除缓存的图像es为所有PATHS已经解决与$filters提供。

  • 如果$filtersnull,该函数假定要删除相应的所提供的$paths缓存图像和以前已经解决了与所有过滤器

  • 如果$paths$filtersnull,函数假定要删除对应的所有路径,并为所有过滤器高速缓存的图像。基本上所有高速缓存的图像。

+0

是否有可能在我的实体的函数中使用它?它看起来像它可以在控制器中工作,但是当我尝试在实体中执行此操作时,出现此错误: 注意:未定义的属性:Keliones \ MainBundle \ Entity \ Article :: $ container – Einius 2014-09-19 12:52:59

+0

这不属于您的实体(你也会听到这个词的模型)。您的模型/实体拥有数据,就像您准备膳食所需的所有成分列表一样。然后,逻辑(例如描述如何使用成分来准备膳食或配方的功能)需要留在实体经理(管理您的实体的经理...)或控制器或任何服务或类中你要的那个。如果这是有道理的... – Mick 2014-09-20 01:46:17