blackbird / magento-2-clean-product-image-cache
清除 Magento 2 的产品图片缓存文件。如果图片名称没有更改,则很有用。
1.0.1
2024-04-29 07:20 UTC
Requires
- magento/framework: *
This package is auto-updated.
Last update: 2024-08-29 08:03:36 UTC
README
magento-2-clean-product-image-cache
有时您可能需要在不更改图片名称的情况下更改产品图片,原因有很多。
问题:如果更改后的图片已经在 pub/media/catalog/product/cache/
文件夹中生成,您需要删除所有生成的缓存图片,以便新的图片在前端和缓存中显示。
解决方案:该模块会在产品保存时清除生成的产品图片缓存。
如何工作 • 安装 • 添加自己的清理缓存策略 • 支持 • 联系 • 许可证
如何工作
当您在后台保存产品时,它将从 pub/media/catalog/product/cache/*
中删除保存产品的所有生成的图片缓存文件。
安装
composer require blackbird/magento-2-clean-product-image-cache
php bin/magento setup:upgrade
在生产模式下,不要忘记重新编译和重新部署静态资源。
添加自己的清理缓存策略
如果您使用像 Cloudflare 这样的 CDN,您可能需要使用其 API 清除 CDN 缓存。我们提供了两种方法来实现这一点
- 创建自己的策略类
- 在我们的事件
blackbird_image_cache_clean_after
上创建观察者
如果您正在使用 Cloudflare,我们为此模块提供了一个扩展,该扩展调用 Cloudflare API 来清除缓存:[blackbird/magento-2-clean-cloudflare-image-cache](https://github.com/blackbird-agency/magento-2-clean-cloudflare-image-cache)
创建自己的策略
创建一个实现 Blackbird\CleanProductImageCache\Api\CleanCacheStrategyInterface
的策略服务
class MyCleanCacheStrategy implements CleanCacheStrategyInterface { /** * {@inheritDoc} */ public function clean(ProductInterface $product): void { //Do what you need to do } }
将您的策略添加到 di.xml
中的池中
... <type name="Blackbird\CleanProductImageCache\Model\Service\CleanCacheStrategyPool"> <arguments> <argument name="cleanCacheStrategies" xsi:type="array"> <item name="my_clean" xsi:type="array"> <item name="class" xsi:type="object">Vendor\Module\Model\Service\MyCleanCacheStrategy</item> <item name="sortOrder" xsi:type="number">10</item> <item name="enabled" xsi:type="boolean">true</item> </item> </argument> </arguments> </type> ...
您可以使用 sortOrder 和 enabled 来更改执行顺序或禁用策略。
使用已分发的事件
创建观察者
class AfterCleanMagentoImageCache implements ObserverInterface { /** * {@inheritDoc} */ public function execute(Observer $observer): void { //Get absolute path off all cached images for the cleaned product $paths = $observer->getPaths(); //Do what you need to do } }
将观察者连接到 events.xml 事件
... <event name="blackbird_image_cache_clean_after"> <observer name="event_custom_name" instance="Vendor\Module\Observer\AfterCleanMagentoImageCache" /> </event> ...
支持
联系
如需更多信息,请联系我们
- 通过电子邮件:[hello@bird.eu](mailto:hello@bird.eu)
- 或通过表单:[https://black.bird.eu/en/contacts/](https://black.bird.eu/en/contacts/)
许可证
本项目采用 MIT 许可证 - 有关详细信息,请参阅LICENSE 文件。
就这些了,朋友们!