【问题标题】:Check if file exists in cache before removing it with Liip Imagine Bundle ( and Symfony3 )在使用 Liip Imagine Bundle (和 Symfony3 )删除之前检查文件是否存在于缓存中
【发布时间】:2016-09-25 22:54:08
【问题描述】:

似乎很基本:我想在删除之前检查 Liip Imagine Bundle 缓存中是否存在文件。一个例子:在照片更新后,所有的缓存都被删除了,只有一些缩略图被重新生成(比如 175px 而不是 250px 的),我想删除相应的照片。 我在 Symfony 中使用监听器来做这件事,就像我看到其他人所做的那样。下面是它的外观:

<?php
namespace AppBundle\EventListener;

use Doctrine\ORM\Event\LifecycleEventArgs;
use AppBundle\Entity\Photo;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Description of CachePhotoListener
 *
 * @author Norman
 */
class CachePhotoListener 
{
    protected $cacheManager;
    protected $request;

public function __construct($cacheManager, RequestStack $request_stack) 
{
    $this->cacheManager = $cacheManager;
    $this->request = $request_stack->getCurrentRequest();
}

public function postUpdate(LifecycleEventArgs $args)
{
    $entity = $args->getEntity();        

    if ($entity instanceof Photo) {
        $this->cacheManager->remove($entity->userPath());
    }
}

// Case : remove photo
public function preRemove(LifecycleEventArgs $args)
{
    $entity = $args->getEntity();

    $filters = array('thumb_prospect_250', 'thumb_prospect_175');

    foreach($filters as $filter){
        if ($entity instanceof Photo) {                
            $expectedCachePath = $this->cacheManager->getBrowserPath($entity->getPath(), $filter);            

            if (file_exists($expectedCachePath)) {
                $this->cacheManager->resolve($this->request, $entity->getPath(), $filter);
                $this->cacheManager->remove($entity->getPath());
            }
        }
    }
}

问题:即使缩略图存在,file_exists 也总是返回“false”。这是 $expectedCachePath 变量的示例:

'http://dev.playermanager/media/cache/thumb_prospect_250/uploads/photos/11/4d176797ca5c7dd753b23ca17b77630eeff0ba8d.jpg' (length=118)

boolean false

'http://dev.playermanager/media/cache/thumb_prospect_175/uploads/photos/11/4d176797ca5c7dd753b23ca17b77630eeff0ba8d.jpg' (length=118)

boolean false

我做错了什么? (我也尝试使用“is_readable”检查文件并获得相同的结果)

【问题讨论】:

    标签: php listener symfony liipimaginebundle


    【解决方案1】:

    好的,我设法解决了我的问题。我修改了两件事。 首先,我使用 isStored 方法检查了 Liip 缓存中照片的存在。其次,我的cacheManager->resolve参数有错误,$this->request参数不应该在那里。

    这在侦听器中为我提供了以下 preRemove 函数:

    public function preRemove(LifecycleEventArgs $args)
        {
            $entity = $args->getEntity();
    
            $filters = array('thumb_prospect_250', 'thumb_prospect_175');
    
            foreach($filters as $filter){
                if ($entity instanceof Photo) {                         
                    $cacheExists = $this->cacheManager->isStored($entity->getPath(), $filter);        
    
                    if ($cacheExists) {
                        $this->cacheManager->resolve($entity->getPath(), $filter);
                        $this->cacheManager->remove($entity->getPath());
                    }
                }
            }
        }
    

    【讨论】:

      【解决方案2】:

      我想在你想要目录路径时调用$this-&gt;cacheManager-&gt;getBrowserPath returns URL

      $entity-&gt;getPath() 中存储了什么?也许你可以自己检查"%kernel.cache_dir%"目录中是否存在该文件。

      【讨论】:

      • $entity->getPath() 返回一个相对 URL,如下所示:“uploads/photos/1/13cf43e720b12bdcafff8a002e046e186559cdc0.jpg” 同时我找到了一个解决方案,但你将如何检查是否该文件以“%kernel.cache_dir%”存在?
      猜你喜欢
      • 2016-02-10
      • 1970-01-01
      • 2018-03-24
      • 2022-12-09
      • 2010-09-27
      • 1970-01-01
      • 2013-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多