ecomdev / magento-psr6-bridge
A bridge between Magento framework and PSR-6 cache compatible libraries.
Installs: 39 127
Dependents: 0
Suggesters: 0
Security: 0
Stars: 14
Watchers: 3
Forks: 3
Open Issues: 0
Type:magento2-module
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-10-27 21:21:14 UTC
README
This small module is a bridge, that allows you to integrate any PSR-6 compatible library into your Magento 2.0 project.
As well it is more convenient to use PSR-6 based cache pool than creating your custom cache type for a Magento module.
Installation
-
Add module as dependency:
composer require ecomdev/magento-psr6-bridge
-
Enable module
bin/magento module:enable EcomDev_MagentoPsr6Bridge
-
Enable PSR-6 cache type
bin/magento cache:enable psr6
Usage
Basic usage
If you already have a PSR-6 compatible library, that uses Psr\Cache\CacheItemPoolInterface
in one of its components, it will work out of the box.
All the cache keys will be automatically prefixed with psr6_
prefix in standard Magento cache storage. Also PSR6 cache tag will be applied automatically.
Using custom cache tags and prefix
If you would like to implement own cache invalidation on particular actions (ERP import, etc).
Then you might find it convenient to use additional custom cache tags so items will be cleaned by them on invoking clear()
method.
-
Add a new virtual type into your
di.xml
and use it for your instances<?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>
-
Now you can use it in your custom class
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; } }
Use cache key generator
Modules comes out of the box with properly configured cache key generator, so if you do not want to take care about your cache key structure, but have PSR6 compatible, you can use it:
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); } }
This allows you remove all that terrible logic of cache key generation based on your class properties.
Read more about cache key generator
Contribution
Make a pull request based on develop branch