* */ namespace phpFastCache\Drivers\Zendshm; 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 (zend memory cache) * Requires Zend Data Cache Functions from ZendServer * @package phpFastCache\Drivers */ class Driver implements ExtendedCacheItemPoolInterface { use DriverBaseTrait; /** * 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() { if (extension_loaded('Zend Data Cache') && function_exists('zend_shm_cache_store')) { return true; } else { return false; } } /** * @param \Psr\Cache\CacheItemInterface $item * @return mixed * @throws phpFastCacheInvalidArgumentException */ protected function driverWrite(CacheItemInterface $item) { /** * Check for Cross-Driver type confusion */ if ($item instanceof Item) { $ttl = $item->getExpirationDate()->getTimestamp() - time(); return zend_shm_cache_store($item->getKey(), $this->driverPreWrap($item), ($ttl > 0 ? $ttl : 0)); } else { throw new phpFastCacheInvalidArgumentException('Cross-Driver type confusion detected'); } } /** * @param \Psr\Cache\CacheItemInterface $item * @return null|array */ protected function driverRead(CacheItemInterface $item) { $data = zend_shm_cache_fetch($item->getKey()); if ($data === false) { return null; } return $data; } /** * @param \Psr\Cache\CacheItemInterface $item * @return bool * @throws phpFastCacheInvalidArgumentException */ protected function driverDelete(CacheItemInterface $item) { /** * Check for Cross-Driver type confusion */ if ($item instanceof Item) { return zend_shm_cache_delete($item->getKey()); } else { throw new phpFastCacheInvalidArgumentException('Cross-Driver type confusion detected'); } } /** * @return bool */ protected function driverClear() { return @zend_shm_cache_clear(); } /** * @return bool */ protected function driverConnect() { return true; } /******************** * * PSR-6 Extended Methods * *******************/ /** * @return string */ public function getHelp() { return << This driver rely on Zend Server 8.5+, see: http://www.zend.com/en/products/zend_server

HELP; } /** * @return DriverStatistic */ public function getStats() { $stats = (array)zend_shm_cache_info(); return (new DriverStatistic()) ->setData(implode(', ', array_keys($this->itemInstances))) ->setInfo(sprintf("The Zend memory have %d item(s) in cache.\n For more information see RawData.", $stats[ 'items_total' ])) ->setRawData($stats) ->setSize($stats[ 'memory_total' ]); } }