PHP code example of ecomdev / magento-psr6-bridge

1. Go to this page and download the library: Download ecomdev/magento-psr6-bridge library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ecomdev / magento-psr6-bridge example snippets


    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;
        }
    
    }
    

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);
    }
}