简体   繁体   English

YII图像缓存

[英]YII IMAGE CACHE

I am using IWI extension to cache image at time of display in yii framework . 我正在使用IWI扩展在yii框架中显示时缓存图像。 It's working fine but the problem is when I am updating a image the the past cache file and folder exists. 它工作正常,但问题是当我更新映像时,过去的缓存文件和文件夹存在。 Please help me to delete those past cache folder after update image. 请帮助我在更新映像后删除那些过去的缓存文件夹。

Edit: 编辑:

$img = $image_name[0]['image_name']; 
$p = 'images/'.$img;
$newpath = Yii::app()->iwi->load($p)->resize(100,300,Image::AUTO)->cache();
$newpath = explode('=',$newpath); ?> 
Image : <br/><br/> 
<?php echo CHtml::image($newpath[1],"image"); ?> 
<div class="row">
<?php echo $form->labelEx($model, 'image'); ?>
<?php echo $form->fileField($model, 'image'); ?>
<?php echo $form->error($model, 'image'); ?>
</div>

When i am updating a particular picture. 当我更新特定图片时。 Say I am updating a image whose id on the table is 1.the new picture is updating with new cache folders and previous cache folders exists. 假设我正在更新一张ID为1.的图像,新图片正在使用新的缓存文件夹更新,并且以前的缓存文件夹存在。

Ok I had a look at the IwI extension and I think I understand what it is you ask. 好吧,我看了IwI扩展名,我想我明白你在问什么。

Unfortunately you can't achieve this out of the box. 不幸的是,您无法做到这一点。 Iwi uses a caching id with the last modified date. Iwi使用具有上次修改日期的缓存ID。 This means that changing an image creates a new cached file/folder rather than being replaced based on a dependency rule. 这意味着更改映像会创建新的缓存文件/文件夹,而不是根据依赖关系规则进行替换。

I think the best option would be to extend Iwi and then override the cache() method (preferably using Yii's caching) 我认为最好的选择是扩展Iwi ,然后重写cache()方法(最好使用Yii的缓存)

Here's an untested example: 这是一个未经测试的示例:

Extend Iwi class : This is very basicc and may not cover all your cases (ie: overwriting a file with another different file. Some of this is redundant but I did it so it was clear) 扩展Iwi :这是非常基础的,可能无法涵盖您的所有情况(即:用另一个不同的文件覆盖文件。其中有些是多余的,但我这样做是很清楚的)

class MyIwi extends Iwi
{
    public function cache()
    {
        if (!isset($this->image["file"])) {
            return false;
        } 
        $cacheFolder = YiiBase::getPathOfAlias('webroot.images.site.cache');   
        $imagePath = $this->image["file"];
        $info = pathinfo($imagePath);


        //create unique ID (filename for cached image) using actions and path. then serialize and hash it.
        $needle = $this->actions;
        array_unshift($needle, $imagePath);
        $cachedFilePath = $cacheFolder."/".md5(json_encode($needle)).".".$info["extension"];

        //if cache file doesn't exist or is older than file then cache 
        if(  
           $this->createOrNone() //did not look into this, keeping because it was in original method
           &&
           (!file_exists($cachedFilePath)
           || 
           filetime($imagePath) > filetime($cachedFilePath))
        )
            $this->save($cachedFilePath);
        return Yii::app()->createUrl($cachedFilePath);
    }

}

Hopefully that will set you on the right track. 希望这将使您走上正确的道路。 Good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM