ecomdev / magento-psr6-bridge
连接 Magento 框架与 PSR-6 兼容缓存库的桥梁。
0.2.1
2016-06-01 08:11 UTC
Requires
- php: ~5.6|>=7.0
- ecomdev/cache-key: ^1.1
- psr/cache: ^1.0
Requires (Dev)
- ecomdev/phpspec-magento-di-adapter: ~0.1
- henrikbjorn/phpspec-code-coverage: ^2.0
- magento/zendframework1: *
- mikey179/vfsstream: ^1.6
- phpmd/phpmd: ^2.3
- phpspec/nyan-formatters: ^1.0
- phpspec/phpspec: ^2.4
- satooshi/php-coveralls: dev-master
- squizlabs/php_codesniffer: *
This package is auto-updated.
Last update: 2024-08-27 20:56:10 UTC
README
此小型模块是一个桥梁,允许您将任何 PSR-6 兼容库集成到您的 Magento 2.0 项目中。
使用基于 PSR-6 的缓存池比为 Magento 模块创建自定义缓存类型更为方便。
安装
-
将模块作为依赖项添加
composer require ecomdev/magento-psr6-bridge
-
启用模块
bin/magento module:enable EcomDev_MagentoPsr6Bridge
-
启用 PSR-6 缓存类型
bin/magento cache:enable psr6
使用方法
基本使用
如果您已经有了一个使用 Psr\Cache\CacheItemPoolInterface
的 PSR-6 兼容库,它将在其中某个组件中工作,它将直接使用。所有的缓存键将自动在标准的 Magento 缓存存储中加上 psr6_
前缀。同时,PSR6 缓存标签也将自动应用。
使用自定义缓存标签和前缀
如果您想在特定的动作(如 ERP 导入)上实现自己的缓存失效。
那么使用额外的自定义缓存标签可能会更方便,这样在调用 clear()
方法时,项目将被它们清理。
-
在您的
di.xml
中添加一个新的虚拟类型,并使用它来创建实例<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <virtualType name="yourModuleCustomCacheItemPoolInstance" type="EcomDev\MagentoPsr6Bridge\Model\CacheItemPool"> <arguments> <argument name="tags" xsi:type="array"> <item name="custom_tag" xsi:type="string">CUSTOM_TAG</item> </argument> <argument name="keyPrefix" xsi:type="string">my_custom_prefix</argument> </arguments> </virtualType> <type name="Your\Module\Model\Item"> <arguments> <argument name="cacheItemPool" xsi:type="object">yourModuleCustomCacheItemPoolInstance</argument> </arguments> </type> </config>
-
现在您可以在自定义类中使用它
namespace Your\Module\Model; class Item { private $cacheItemPool; public function __construct( \Psr\Cache\CacheItemPoolInterface $cacheItemPool ) { $this->cacheItemPool = $cacheItemPool; } public function doSomeStuff() { $item = $this->cacheItemPool->getItem('cache_key_id'); if ($item->isHit()) { return sprintf('Cached: %s', $item->get()); } $value = 'Value for cache'; $item->set($value); $this->cacheItemPool->save($item); return sprintf('Not cached: %s', $value); } public function invalidate() { $this->cacheItemPool->clear(); return $this; } }
使用缓存键生成器
此模块自带正确配置的缓存键生成器,因此如果您不希望关心缓存键结构,但需要 PSR6 兼容,可以使用它
namespace Your\Module\Model; use EcomDev\CacheKey\InfoProviderInterface; class Item implements InfoProviderInterface { private $cacheItemPool; private $cacheKeyGenerator; public function __construct( \Psr\Cache\CacheItemPoolInterface $cacheItemPool, \EcomDev\CacheKey\GeneratorInterface $cacheKeyGenerator ) { $this->cacheItemPool = $cacheItemPool; $this->cacheKeyGenerator = $cacheKeyGenerator; } public function getCacheKeyInfo() { return [ 'key' => 'value', 'key1' => 'value1' ]; } public function doSomeStuff() { $item = $this->cacheItemPool->getItem( $this->cacheKeyGenerator->generate($this) ); if ($item->isHit()) { return sprintf('Cached: %s', $item->get()); } $value = 'Value for cache'; $item->set($value); $this->cacheItemPool->save($item); return sprintf('Not cached: %s', $value); } }
这允许您移除所有基于类属性生成的缓存键的糟糕逻辑。
贡献
基于 develop 分支提交 pull request