Commit 5d304167 by halweg

feat : cache feed

parent 5e43c3de
......@@ -2,12 +2,23 @@
namespace Joshine\InstagramFeed\Block;
use Joshine\InstagramFeed\Model\Cache\Type;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\View\Element\Template;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\Cache;
use phpDocumentor\Reflection\PseudoTypes\LiteralString;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Serialize\Serializer\Json;
class MediaFeed extends Template {
const TYPE_IDENTIFIER = 'instagram_feed';
const CACHE_TAG = 'INSTAGRAM_FEED';
const CACHE_LIFETIME = 60 * 60;
protected $request;
protected $_template = 'Joshine_InstagramFeed::mediafeed.phtml';
......@@ -28,6 +39,12 @@ class MediaFeed extends Template {
* @var \Magento\Framework\Json\Decoder
*/
private $jsonDecoder;
/**
* @var string
*/
private $id;
private $_json;
public function __construct(
Template\Context $context,
......@@ -37,16 +54,18 @@ class MediaFeed extends Template {
\Magento\Framework\HTTP\Client\Curl $curl,
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
\Magento\Framework\Json\Decoder $jsonDecoder,
Json $json,
array $data = []
) {
parent::__construct($context, $data);
$this->request = $request;
$this->id = $this->getId();
$this->_objectManager = $objectManager;
$this->apiAddress = $apiAddress;
$this->_curlClient = $curl;
$this->jsonEncoder = $jsonEncoder;
$this->jsonDecoder = $jsonDecoder;
$this->_json = $json;
}
public function getCurlClient()
......@@ -54,7 +73,18 @@ class MediaFeed extends Template {
return $this->_curlClient;
}
public function getMedia() {
public function getMediaByCache()
{
if ($this->_cacheState->isEnabled(Type::TYPE_IDENTIFIER)) {
if ($feed = $this->_cache->load($this->id)) {
return $this->_json->unserialize($feed);
}
}
return [];
}
public function getMediaByApi()
{
$uid = $this->_objectManager->get(ScopeConfigInterface::class)
->getValue(
'joshine_instagram_feed/general/user_id',
......@@ -80,4 +110,52 @@ class MediaFeed extends Template {
}
return $this->jsonDecoder->decode($this->getCurlClient()->getBody());
}
public function cacheFeed($feed)
{
var_dump($this->_cacheState->isEnabled(Type::TYPE_IDENTIFIER));
if ($this->_cacheState->isEnabled(Type::TYPE_IDENTIFIER)) {
$this->save($feed, $this->id);
}
return true;
}
public function getId()
{
try {
return base64_encode($this->_storeManager->getStore()->getCode() . Type::TYPE_IDENTIFIER);
} catch (NoSuchEntityException $e) {
return base64_encode(date('Y-m-d') . Type::TYPE_IDENTIFIER);
}
}
public function load($cacheId)
{
if ($this->_cacheState->isEnabled(Type::TYPE_IDENTIFIER)) {
return $this->_cache->load($cacheId) ?: false;
}
return false;
}
public function save($data, $cacheId)
{
if ($this->_cacheState->isEnabled(Type::TYPE_IDENTIFIER)) {
return $this->_cache->save($data, $cacheId, [Type::CACHE_TAG], Type::CACHE_LIFETIME);
}
return false;
}
public function getMedia() {
$cache = $this->getMediaByCache();
if (!empty($cache)) {
return $cache;
}
$feed = $this->getMediaByApi();
if (isset($feed['data'])) {
$this->cacheFeed($feed);
}
return $feed;
}
}
\ No newline at end of file
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Cache/etc/cache.xsd">
<type name="instagram_feed" translate="label,description" instance="Joshine\InstagramFeed\Model\Cache\Type">
<label>Instagram Feed</label>
<description>Instagram feed cache.</description>
</type>
</config>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment