【发布时间】:2014-06-27 13:34:00
【问题描述】:
我在 Magento 中有一个自定义模块,它可以自动从 FTP 目录更新产品图像。当使用新图像更新产品时,我需要手动Flush catalog image cache 以在前端显示新图像。但是,这会清除所有图像缓存,并且对于包含数千种产品的库,这并不是一个真正的选择。
是否可以清除PHP中特定产品的图片缓存?
【问题讨论】:
标签: php magento magento-1.8
我在 Magento 中有一个自定义模块,它可以自动从 FTP 目录更新产品图像。当使用新图像更新产品时,我需要手动Flush catalog image cache 以在前端显示新图像。但是,这会清除所有图像缓存,并且对于包含数千种产品的库,这并不是一个真正的选择。
是否可以清除PHP中特定产品的图片缓存?
【问题讨论】:
标签: php magento magento-1.8
不幸的是,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;
}
【讨论】:
如果你说的是默认的 Magento 缓存,那么你可以在 import images 函数末尾使用以下代码刷新缓存
Mage::app()->cleanCache('catalog_product_'.$productId);
【讨论】: