Skip to content

Commit

Permalink
Merge pull request #6 from webshopapps/SHQ16-992_A
Browse files Browse the repository at this point in the history
SHQ16-992 implement config switch for caching rates
  • Loading branch information
wsagen authored Feb 2, 2017
2 parents b83b72f + 2cf596c commit 300b8ab
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/Helper/CarrierCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,18 @@ class CarrierCache extends \Magento\Framework\App\Helper\AbstractHelper
*/
protected static $quotesCache = [];

protected $useCache = false;

/**
* @param Context $context
*/
public function __construct(\Magento\Framework\App\CacheInterface $cache, \Magento\Framework\App\Helper\Context $context)
public function __construct(\Magento\Framework\App\CacheInterface $cache,
\Shipperhq\Shipper\Helper\Data $shipperDataHelper,
\Magento\Framework\App\Helper\Context $context)
{
$this->cache = $cache;
$this->shipperDataHelper = $shipperDataHelper;
$this->useCache = $this->shipperDataHelper->getConfigValue('carriers/shipper/use_cache');
parent::__construct($context);
}

Expand Down Expand Up @@ -90,9 +96,12 @@ protected function getQuotesCacheKey($requestParams, $carrierCode)
public function getCachedQuotes($requestParams, $carrierCode)
{
$key = $this->getQuotesCacheKey($requestParams, $carrierCode);
// return isset(self::$quotesCache[$key]) ? self::$quotesCache[$key] : null;
$cachedResult = $this->cache->load($key);
$cachedResult = false;
if($this->useCache) {
$cachedResult = $this->cache->load($key);
}
return $cachedResult ? unserialize($cachedResult) : $cachedResult;

}

/**
Expand All @@ -104,9 +113,10 @@ public function getCachedQuotes($requestParams, $carrierCode)
*/
public function setCachedQuotes($requestParams, $response, $carrierCode)
{
$key = $this->getQuotesCacheKey($requestParams, $carrierCode);
$this->cache->save(serialize($response), $key, [self::CACHE_TAG]);
// self::$quotesCache[$key] = $response;
if($this->useCache) {
$key = $this->getQuotesCacheKey($requestParams, $carrierCode);
$this->cache->save(serialize($response), $key, [self::CACHE_TAG]);
}
return $this;
}

Expand Down

0 comments on commit 300b8ab

Please sign in to comment.