Php 如何清除Magento 2中单个产品id的缓存
我编写了一个cron作业,它检查直接在数据库中进行的库存量更改,因此绕过了Magento core,而Magento core将处理缓存过期的问题 我希望能够以以下方式使用对象管理器:Php 如何清除Magento 2中单个产品id的缓存,php,caching,magento2,Php,Caching,Magento2,我编写了一个cron作业,它检查直接在数据库中进行的库存量更改,因此绕过了Magento core,而Magento core将处理缓存过期的问题 我希望能够以以下方式使用对象管理器: public function clearCacheforProduct($productID) { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $cacheManager = $objectManag
public function clearCacheforProduct($productID) {
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cacheManager = $objectManager->get('\Magento\Framework\App\Cache\Manager');
$cacheManager->clean('catalog_product_' . $productID);
}
当前,当cron作业运行时,此操作会自动失败
知道如何清除缓存中的单个产品ID吗?谢谢@bxN5。我在登录时遇到了一些错误,并很快发现
cacheManager
的名称空间有点错误
正确的代码是:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cacheManager = $objectManager->get('\Magento\Framework\App\CacheInterface');
$cacheManager->clean('catalog_product_' . $productID);
对于那些使用Magento运行Varnish的人来说,也有必要清除其中的数据,而且Magento调用似乎并没有完全做到这一点。因此,我添加了一个cURL请求来完成特定的清除:
$varnishurl = "www.domainYouWantToPurge.co.uk";
$varnishcommand = "PURGE";
$productID = '760'; // This is the Magento ProductID of the item you want to purge
$curl = curl_init($varnishurl);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $varnishcommand);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['X-Magento-Tags-Pattern: catalog_product_'.$productID]);
$result = curl_exec($curl);
curl_close($curl);
确保已在Varnish配置文件中正确设置清除权限。谢谢@bxN5。我在登录时遇到了一些错误,并很快发现
cacheManager
的名称空间有点错误
正确的代码是:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cacheManager = $objectManager->get('\Magento\Framework\App\CacheInterface');
$cacheManager->clean('catalog_product_' . $productID);
对于那些使用Magento运行Varnish的人来说,也有必要清除其中的数据,而且Magento调用似乎并没有完全做到这一点。因此,我添加了一个cURL请求来完成特定的清除:
$varnishurl = "www.domainYouWantToPurge.co.uk";
$varnishcommand = "PURGE";
$productID = '760'; // This is the Magento ProductID of the item you want to purge
$curl = curl_init($varnishurl);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $varnishcommand);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['X-Magento-Tags-Pattern: catalog_product_'.$productID]);
$result = curl_exec($curl);
curl_close($curl);
确保已在Varnish配置文件中正确设置清除权限。我发现最好的方法是使用此代码
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productR = $objectManager ->create('\Magento\Catalog\Api\ProductRepositoryInterface');
$product = $productR->get('product_sku');
$product->cleanCache();
$this->_eventManager->dispatch('clean_cache_by_tags', ['object' => $product]);
请在构造函数中初始化objectManager和ProductRepository
我现在正在使用这种方式,它就像魔咒一样工作我发现最好的方式就是使用这种代码
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productR = $objectManager ->create('\Magento\Catalog\Api\ProductRepositoryInterface');
$product = $productR->get('product_sku');
$product->cleanCache();
$this->_eventManager->dispatch('clean_cache_by_tags', ['object' => $product]);
请在构造函数中初始化objectManager和ProductRepository
我现在使用的是这种方式,它的工作原理就像魔咒一样如果您查看
\Magento\InventoryCache\Model\FlushCacheByProductIds
,您将看到以下方法(Magento 2.2.3):
有了DI的帮助,我会选择在可能的情况下使用核心代码,而不是使用自己的代码
亲吻并擦干。如果查看
\Magento\InventoryCache\Model\FlushCacheByProductIds
,您将看到以下方法(Magento 2.2.3):
有了DI的帮助,我会选择在可能的情况下使用核心代码,而不是使用自己的代码
亲吻并擦干。您还可以检查InvalidateVarnishObserver,查看清漆缓存是如何清除的。这个类观察“clean_cache_by_tags”事件(这个事件在每次产品保存调用后被调度)我尝试了这个,但它的行为确实导致了产品页面上缓存刷新的延迟,在运行$cacheManager->clean('catalog_product_.$productID')一分钟左右后,页面被刷新;您还可以检查InvalidateVarnishObserver并查看如何清除清漆缓存。这个类观察“clean_cache_by_tags”事件(这个事件在每次产品保存调用后被调度)我尝试了这个,但它的行为确实导致了产品页面上缓存刷新的延迟,在运行$cacheManager->clean('catalog_product_.$productID')一分钟左右后,页面被刷新;这是目前最好的方式。我也会展示一个合适的DI。这是目前为止最好的方式。我也会展示一个合适的DI。