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

时间:2014-06-27 13:34:00

标签: php magento magento-1.8

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

是否可以在PHP

中清除特定产品的图像缓存

2 个答案:

答案 0 :(得分:4)

不幸的是,Magento(afaik)没有提供原生功能。在* nix上,您可以使用Shell在缓存文件夹中搜索(小写)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;
}

答案 1 :(得分:2)

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

 Mage::app()->cleanCache('catalog_product_'.$productId);
相关问题