Source of file CacheModel.php
Size: 2,628 Bytes - Last Modified: 2021-12-24T06:50:58+00:00
/var/www/docs.ssmods.com/process/src/src/CacheModel.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 | <?php namespace Heyday\CacheInclude; use Doctrine\Common\Cache\CacheProvider; use Heyday\CacheInclude\Configs\ConfigInterface; /** * @package Heyday\CacheInclude */ class CacheModel { /** * @param \Doctrine\Common\Cache\CacheProvider $cache * @param \Heyday\CacheInclude\Configs\ConfigInterface $config */ public function __construct( CacheProvider $cache, ConfigInterface $config ) { $this->cache = $cache; $this->config = $config; } /** * @return array */ public function getAll() { $caches = []; foreach ($this->config as $name => $config) { $caches[] = [ 'name' => $name, 'keys' => $this->getKeys($name), 'config' => $config ]; } return $caches; } /** * @param $name * @return array */ public function get($name) { $caches = []; foreach ($this->getKeys($name) as $key => $information) { $caches[] = $this->getByKey($name, $key); } return $caches; } /** * @param $name * @param $key * @return array */ public function getByKey($name, $key) { $this->assertHasName($name); $this->assertHasKey($key); return [ 'key' => $key, 'value' => $this->cache->fetch($key) ]; } /** * @param $name * @return bool|mixed|string */ public function getKeys($name) { $this->assertHasName($name); return (array) $this->cache->fetch($name); } public function delete($name) { $this->assertHasName($name); $keys = (array) $this->cache->fetch($name); foreach (array_keys($keys) as $key) { $this->cache->delete($key); } $this->cache->save($name, []); } /** * @param $name * @throw \InvalidArgumentException */ protected function assertHasName($name) { if (!isset($this->config[$name])) { throw new \InvalidArgumentException(sprintf( "The cache name '%s' doesn't exist in the config", $name )); } } /** * @param $key * @throw \InvalidArgumentException */ protected function assertHasKey($key) { if (null === $this->cache->fetch($key)) { throw new \InvalidArgumentException(sprintf( "The cache key '%s' doesn't exist", $key )); } } } |