Source of file EcommerceCache.php
Size: 4,735 Bytes - Last Modified: 2021-12-23T10:39:35+00:00
/var/www/docs.ssmods.com/process/src/src/Api/EcommerceCache.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 | <?php namespace Sunnysideup\Ecommerce\Api; use Psr\SimpleCache\CacheInterface; use SilverStripe\Control\Director; use SilverStripe\Core\Flushable; use SilverStripe\Core\Injector\Injectable; use SilverStripe\Core\Injector\Injector; use SilverStripe\Versioned\Versioned; use Sunnysideup\Ecommerce\Pages\Product; use Sunnysideup\Ecommerce\Pages\ProductGroup; /** * Provides a standard interface for caching product and group information. * * Can be used in conjuction with the standard Silverstripe Partial caching * functionality. * * usage: * ```php * $myCachedData = EcommerceCache::inst()->retrieve($key); * EcommerceCache::inst()->save($key, $myUncachedData); * ``` */ class EcommerceCache implements Flushable { use Injectable; /** * @var CacheInterface */ protected $cacheBackend; protected $productCacheKey = ''; public static function inst(): EcommerceCache { return Injector::inst()->get(self::class); } /** * Set the cache object to use when storing / retrieving partial cache blocks. */ public function setCacheBackend(CacheInterface $cacheBackend): EcommerceCache { $this->cacheBackend = $cacheBackend; return $this; } /** * Get the cache object to use when storing / retrieving stuff in the * Silverstripe Cache. * * @return CacheInterface */ public function getCacheBackend() { if (! $this->cacheBackend) { $this->cacheBackend = Injector::inst()->get(CacheInterface::class . '.Ecommerce'); } return $this->cacheBackend; } public function hasCache(string $cacheKey, $cacheKeyAlreadyRefined = false): bool { if ($this->AllowCaching()) { if (! $cacheKeyAlreadyRefined) { $cacheKey = $this->cacheKeyRefiner($cacheKey); } return $this->getCacheBackend()->has($cacheKey); } return false; } public function productCacheKey(): string { if (! $this->productCacheKey) { $this->productCacheKey .= '_' . Product::get()->count(); $this->productCacheKey .= '_' . strtotime(Product::get()->max('LastEdited')); $this->productCacheKey .= '_' . ProductGroup::get()->count(); $this->productCacheKey .= '_' . strtotime(ProductGroup::get()->max('LastEdited')); $this->productCacheKey .= '_' . Versioned::get_reading_mode(); $this->productCacheKey .= '_' . Director::get_environment_type(); } return $this->productCacheKey; } /** * Retrieve an object from the cache. * * @return null|mixed */ public function retrieve(string $cacheKey, ?bool $alreadyUnserialized = false) { $cacheKey = $this->cacheKeyRefiner($cacheKey); $data = $this->getCacheBackend()->get($cacheKey); if ($data) { if (false === $alreadyUnserialized) { $data = unserialize($data); } return $data; } return null; } public function retrieveAsIdList(string $cacheKey, ?bool $alreadyUnserialized = false) { return ArrayMethods::filter_array($this->retrieve($cacheKey, $alreadyUnserialized)); } /** * returns true when the data is saved... * * @param string $cacheKey - key under which the data is saved... * @param mixed $data */ public function save(string $cacheKey, $data, ?bool $alreadySerialized = false): bool { if ($this->AllowCaching()) { $cacheKey = $this->cacheKeyRefiner($cacheKey); if (false === $alreadySerialized) { $data = serialize($data); } $this->getCacheBackend()->set($cacheKey, $data); return true; } return false; } public function AllowCaching(): bool { return ! isset($_GET['no-cache']); } public function clear() { $this->getCacheBackend()->clear(); } public static function flush() { EcommerceCache::inst()->clear(); } /** * Most importantly, adds Product Last Changed + Count! * * @param string $cacheKey */ public function cacheKeyRefiner($cacheKey): string { if (is_array($cacheKey)) { $cacheKey = implode('_', $cacheKey); } $arrayOfReservedChars = [ '{', '}', '(', ')', '/', '\\', '@', ':', '.', ]; return str_replace($arrayOfReservedChars, '_', $cacheKey) . $this->productCacheKey(); } } |