我在Magento中有一个自定义模块,它自动从FTP目录更新产品图像。当使用新图像更新产品时,我需要手动Flush catalog image cache
在前端显示新图像。但是,这将清除所有图像缓存,并且对于包含数千个产品的库来说,这不是一个真正的选择。
是否可以清除PHP
中特定产品的图像缓存?
遗憾的是,Magento (afaik)没有提供相应的本地函数。在*nix上,您可以使用Shell在缓存文件夹中搜索(小写)SKU并删除它们。
请注意,PHP需要执行shell命令的权限才能使下面的代码工作。调用::findacheimages后,您可以遍历结果并删除缓存的图像。
从我的一个类的例子:
/**
* 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缓存,那么您可以通过在import images函数末尾使用以下代码来刷新缓存
Mage::app()->cleanCache('catalog_product_'.$productId);