繁体   English   中英

Magento 1.8中特定产品的图像缓存是否为空?

[英]Empty image cache for specific product in Magento 1.8?

我在Magento中有一个自定义模块,该模块可以自动从FTP目录更新产品图像。 使用新图像更新产品时,我需要手动Flush catalog image cache以在前端显示新图像。 但是,这将清除所有图像缓存,并且具有数千种产品的库并不是真正的选择。

是否可以为PHP的特定产品清除图像缓存?

不幸的是,Magento(afaik)没有为此提供本机功能。 在* nix上,您可以使用命令行管理程序在缓存文件夹中搜索(小写)SKU并将其删除。

请注意,PHP将需要执行shell命令的权利,以下代码才能正常工作。 调用:: findCacheImages之后,您可以遍历结果并删除缓存的图像。

我的一个班级的示例:

/**
 * Get array of all files in the image cache tree. Provide all SKU at once for better performance.
 *
 * @param array $skus
 * @return array
 */
static public function findCacheImages(Array $skus)
{
    if (!$skus) {
        return array();
    }
    $skus     = array_unique($skus);
    $toSearch = array();
    $result   = array();
    while (count($skus) > 0) {
        $sku = array_pop($skus);
        if (trim($sku) != '') {
            $toSearch[] = $sku;
        }
        if (count($toSearch) > 50 || count($skus) == 0) {
            // Perform file search
            $bigRegex = array();
            foreach ($toSearch as $fName) {
                // Build regex
                $bigRegex[] = '.*/' . strtolower($fName) . '.*';
            }
            $bigRegexStr = implode('|', $bigRegex);
            $dir         = escapeshellcmd(Mage::getBaseDir() . '/media/catalog/product/cache/');
            $result      = array_merge(self::findFilesRegex($dir, $bigRegexStr), $result);
            $toSearch    = array();
        }
    }
    return $result;
}

/**
 * @param string $dir
 * @param string $regex
 * @return array
 */
static public function findFilesRegex($dir, $regex)
{
    $files = shell_exec("find $dir -type f -regextype posix-extended -regex '$regex' -print");
    $files = explode("\n", trim($files));
    return $files;
}

如果您正在谈论默认的Magento缓存,则可以在导入图像功能末尾使用以下代码来刷新缓存

 Mage::app()->cleanCache('catalog_product_'.$productId);

暂无
暂无

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

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