CacheStorage.php 2.52 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
<?php
/**
 * @copyright  Vertex. All rights reserved.  https://www.vertexinc.com/
 * @author     Mediotype                     https://www.mediotype.com/
 */

namespace Vertex\Tax\Model\TaxRegistry;

use Magento\Framework\App\Cache\StateInterface;
use Magento\Framework\Cache\FrontendInterface;
use Vertex\Tax\Model\Cache\Type as CacheType;
use Vertex\Tax\Model\Cache\Serializer;

/**
 * Persistent storage for Vertex tax information.
 */
class CacheStorage extends GenericStorage
{
    const CACHE_ID_PREFIX = 'VERTEX_';

    /** @var FrontendInterface */
    private $cache;

    /** @var bool */
    private $enabled;

    /** @var SerializerInterface */
    private $serializer;

    /**
     * @param FrontendInterface $cache
     */
    public function __construct(
        FrontendInterface $cache,
        StateInterface $cacheState,
        Serializer $serializer
    ) {
        $this->cache = $cache;
        $this->enabled = $cacheState->isEnabled(CacheType::TYPE_IDENTIFIER);
        $this->serializer = $serializer;
    }

    /**
     * {@inheritdoc}
     */
    public function get($key, $default = null)
    {
        if (!$this->enabled) {
            return parent::get($key, $default);
        }

        $result = $this->cache->load($this->getCacheId($key));

        return $result === false ? $default : $this->unserialize($result);
    }

    /**
     * {@inheritdoc}
     */
    public function set($key, $value, $lifetime = 0)
    {
        if (!$this->enabled) {
            return parent::set($key, $value, $lifetime);
        }

        return $this->cache->save($this->serialize($value), $this->getCacheId($key), [], $lifetime);
    }

    /**
     * {@inheritdoc}
     */
    public function unsetData($key)
    {
        if (!$this->enabled) {
            return parent::unsetData($key);
        }

        $this->cache->remove($key);

        return $this->cache->load($key) === null;
    }

    /**
     * Generate a cache identifier from the given input.
     *
     * @param string $input
     * @return string
     */
    private function getCacheId($input)
    {
        return self::CACHE_ID_PREFIX . sha1($input);
    }

    /**
     * Serialize the given data.
     *
     * @param mixed $data
     * @return string
     */
    private function serialize($data)
    {
        return $this->serializer->serialize($data);
    }

    /**
     * Unserialize the given data.
     *
     * @param string $data
     * @return mixed
     */
    private function unserialize($data)
    {
        return $this->serializer->unserialize($data);
    }
}