http://www.phpfastcache.com * @author Georges.L (Geolim4) * */ namespace phpFastCache\Drivers\Memstatic; use phpFastCache\Core\Pool\DriverBaseTrait; use phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface; use phpFastCache\Entities\DriverStatistic; use phpFastCache\Exceptions\phpFastCacheDriverCheckException; use phpFastCache\Exceptions\phpFastCacheDriverException; use phpFastCache\Exceptions\phpFastCacheInvalidArgumentException; use Psr\Cache\CacheItemInterface; /** * Class Driver * @package phpFastCache\Drivers */ class Driver implements ExtendedCacheItemPoolInterface { use DriverBaseTrait; /** * @var array */ protected $staticStack = []; /** * Driver constructor. * @param array $config * @throws phpFastCacheDriverException */ public function __construct(array $config = []) { $this->setup($config); if (!$this->driverCheck()) { throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName())); } } /** * @return bool */ public function driverCheck() { return true; } /** * @param \Psr\Cache\CacheItemInterface $item * @return mixed * @throws phpFastCacheInvalidArgumentException */ protected function driverWrite(CacheItemInterface $item) { /** * Check for Cross-Driver type confusion */ if ($item instanceof Item) { return $this->staticStack[ md5($item->getKey()) ] = $this->driverPreWrap($item); } else { throw new phpFastCacheInvalidArgumentException('Cross-Driver type confusion detected'); } } /** * @param \Psr\Cache\CacheItemInterface $item * @return null|array */ protected function driverRead(CacheItemInterface $item) { $key = md5($item->getKey()); if (isset($this->staticStack[ $key ])) { return $this->staticStack[ $key ]; } return null; } /** * @param \Psr\Cache\CacheItemInterface $item * @return bool * @throws phpFastCacheInvalidArgumentException */ protected function driverDelete(CacheItemInterface $item) { /** * Check for Cross-Driver type confusion */ if ($item instanceof Item) { $key = md5($item->getKey()); if (isset($this->staticStack[ $key ])) { unset($this->staticStack[ $key ]); return true; } return false; } else { throw new phpFastCacheInvalidArgumentException('Cross-Driver type confusion detected'); } } /** * @return bool */ protected function driverClear() { unset($this->staticStack); $this->staticStack = []; return true; } /** * @return bool */ protected function driverConnect() { return true; } /******************** * * PSR-6 Extended Methods * *******************/ /** * @return DriverStatistic */ public function getStats() { $stat = new DriverStatistic(); $stat->setInfo('[Memstatic] A memory static driver') ->setSize(mb_strlen(serialize($this->staticStack))) ->setData(implode(', ', array_keys($this->itemInstances))) ->setRawData($this->staticStack); return $stat; } }