| @@ 28-143 (lines=116) @@ | ||
| 25 | * Class Driver |
|
| 26 | * @package phpFastCache\Drivers |
|
| 27 | */ |
|
| 28 | class Driver extends DriverAbstract |
|
| 29 | { |
|
| 30 | /** |
|
| 31 | * Driver constructor. |
|
| 32 | * @param array $config |
|
| 33 | * @throws phpFastCacheDriverException |
|
| 34 | */ |
|
| 35 | public function __construct(array $config = []) |
|
| 36 | { |
|
| 37 | $this->setup($config); |
|
| 38 | ||
| 39 | if (!$this->driverCheck()) { |
|
| 40 | throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName())); |
|
| 41 | } |
|
| 42 | } |
|
| 43 | ||
| 44 | /** |
|
| 45 | * @return bool |
|
| 46 | */ |
|
| 47 | public function driverCheck() |
|
| 48 | { |
|
| 49 | if (extension_loaded('apc') && ini_get('apc.enabled')) { |
|
| 50 | return true; |
|
| 51 | } else { |
|
| 52 | return false; |
|
| 53 | } |
|
| 54 | } |
|
| 55 | ||
| 56 | /** |
|
| 57 | * @param \Psr\Cache\CacheItemInterface $item |
|
| 58 | * @return mixed |
|
| 59 | * @throws \InvalidArgumentException |
|
| 60 | */ |
|
| 61 | protected function driverWrite(CacheItemInterface $item) |
|
| 62 | { |
|
| 63 | /** |
|
| 64 | * Check for Cross-Driver type confusion |
|
| 65 | */ |
|
| 66 | if ($item instanceof Item) { |
|
| 67 | $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
| 68 | ||
| 69 | return apc_store($item->getKey(), $this->driverPreWrap($item), ($ttl > 0 ? $ttl : 0)); |
|
| 70 | } else { |
|
| 71 | throw new \InvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 72 | } |
|
| 73 | } |
|
| 74 | ||
| 75 | /** |
|
| 76 | * @param \Psr\Cache\CacheItemInterface $item |
|
| 77 | * @return mixed |
|
| 78 | */ |
|
| 79 | protected function driverRead(CacheItemInterface $item) |
|
| 80 | { |
|
| 81 | $data = apc_fetch($item->getKey(), $success); |
|
| 82 | if ($success === false) { |
|
| 83 | return null; |
|
| 84 | } |
|
| 85 | ||
| 86 | return $data; |
|
| 87 | } |
|
| 88 | ||
| 89 | /** |
|
| 90 | * @param \Psr\Cache\CacheItemInterface $item |
|
| 91 | * @return bool |
|
| 92 | * @throws \InvalidArgumentException |
|
| 93 | */ |
|
| 94 | protected function driverDelete(CacheItemInterface $item) |
|
| 95 | { |
|
| 96 | /** |
|
| 97 | * Check for Cross-Driver type confusion |
|
| 98 | */ |
|
| 99 | if ($item instanceof Item) { |
|
| 100 | return apc_delete($item->getKey()); |
|
| 101 | } else { |
|
| 102 | throw new \InvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 103 | } |
|
| 104 | } |
|
| 105 | ||
| 106 | /** |
|
| 107 | * @return bool |
|
| 108 | */ |
|
| 109 | protected function driverClear() |
|
| 110 | { |
|
| 111 | return @apc_clear_cache() && @apc_clear_cache('user'); |
|
| 112 | } |
|
| 113 | ||
| 114 | /** |
|
| 115 | * @return bool |
|
| 116 | */ |
|
| 117 | protected function driverConnect() |
|
| 118 | { |
|
| 119 | return true; |
|
| 120 | } |
|
| 121 | ||
| 122 | /******************** |
|
| 123 | * |
|
| 124 | * PSR-6 Extended Methods |
|
| 125 | * |
|
| 126 | *******************/ |
|
| 127 | ||
| 128 | /** |
|
| 129 | * @return driverStatistic |
|
| 130 | */ |
|
| 131 | public function getStats() |
|
| 132 | { |
|
| 133 | $stats = (array) apc_cache_info('user'); |
|
| 134 | $date = (new \DateTime())->setTimestamp($stats[ 'start_time' ]); |
|
| 135 | ||
| 136 | return (new driverStatistic()) |
|
| 137 | ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 138 | ->setInfo(sprintf("The APC cache is up since %s, and have %d item(s) in cache.\n For more information see RawData.", $date->format(DATE_RFC2822), |
|
| 139 | $stats[ 'num_entries' ])) |
|
| 140 | ->setRawData($stats) |
|
| 141 | ->setSize($stats[ 'mem_size' ]); |
|
| 142 | } |
|
| 143 | } |
|
| @@ 28-142 (lines=115) @@ | ||
| 25 | * Class Driver |
|
| 26 | * @package phpFastCache\Drivers |
|
| 27 | */ |
|
| 28 | class Driver extends DriverAbstract |
|
| 29 | { |
|
| 30 | /** |
|
| 31 | * Driver constructor. |
|
| 32 | * @param array $config |
|
| 33 | * @throws phpFastCacheDriverException |
|
| 34 | */ |
|
| 35 | public function __construct(array $config = []) |
|
| 36 | { |
|
| 37 | $this->setup($config); |
|
| 38 | ||
| 39 | if (!$this->driverCheck()) { |
|
| 40 | throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName())); |
|
| 41 | } |
|
| 42 | } |
|
| 43 | ||
| 44 | /** |
|
| 45 | * @return bool |
|
| 46 | */ |
|
| 47 | public function driverCheck() |
|
| 48 | { |
|
| 49 | if (extension_loaded('apcu') && ini_get('apc.enabled')) { |
|
| 50 | return true; |
|
| 51 | } else { |
|
| 52 | return false; |
|
| 53 | } |
|
| 54 | } |
|
| 55 | ||
| 56 | /** |
|
| 57 | * @param \Psr\Cache\CacheItemInterface $item |
|
| 58 | * @return mixed |
|
| 59 | * @throws \InvalidArgumentException |
|
| 60 | */ |
|
| 61 | protected function driverWrite(CacheItemInterface $item) |
|
| 62 | { |
|
| 63 | /** |
|
| 64 | * Check for Cross-Driver type confusion |
|
| 65 | */ |
|
| 66 | if ($item instanceof Item) { |
|
| 67 | $ttl = $item->getExpirationDate()->getTimestamp() - time(); |
|
| 68 | ||
| 69 | return apc_store($item->getKey(), $this->driverPreWrap($item), ($ttl > 0 ? $ttl : 0)); |
|
| 70 | } else { |
|
| 71 | throw new \InvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 72 | } |
|
| 73 | } |
|
| 74 | ||
| 75 | /** |
|
| 76 | * @param \Psr\Cache\CacheItemInterface $item |
|
| 77 | * @return mixed |
|
| 78 | */ |
|
| 79 | protected function driverRead(CacheItemInterface $item) |
|
| 80 | { |
|
| 81 | $data = apc_fetch($item->getKey(), $success); |
|
| 82 | if ($success === false) { |
|
| 83 | return null; |
|
| 84 | } |
|
| 85 | ||
| 86 | return $data; |
|
| 87 | } |
|
| 88 | ||
| 89 | /** |
|
| 90 | * @param \Psr\Cache\CacheItemInterface $item |
|
| 91 | * @return bool |
|
| 92 | * @throws \InvalidArgumentException |
|
| 93 | */ |
|
| 94 | protected function driverDelete(CacheItemInterface $item) |
|
| 95 | { |
|
| 96 | /** |
|
| 97 | * Check for Cross-Driver type confusion |
|
| 98 | */ |
|
| 99 | if ($item instanceof Item) { |
|
| 100 | return apc_delete($item->getKey()); |
|
| 101 | } else { |
|
| 102 | throw new \InvalidArgumentException('Cross-Driver type confusion detected'); |
|
| 103 | } |
|
| 104 | } |
|
| 105 | ||
| 106 | /** |
|
| 107 | * @return bool |
|
| 108 | */ |
|
| 109 | protected function driverClear() |
|
| 110 | { |
|
| 111 | return @apc_clear_cache() && @apc_clear_cache('user'); |
|
| 112 | } |
|
| 113 | ||
| 114 | /** |
|
| 115 | * @return bool |
|
| 116 | */ |
|
| 117 | protected function driverConnect() |
|
| 118 | { |
|
| 119 | return true; |
|
| 120 | } |
|
| 121 | ||
| 122 | /******************** |
|
| 123 | * |
|
| 124 | * PSR-6 Extended Methods |
|
| 125 | * |
|
| 126 | *******************/ |
|
| 127 | ||
| 128 | /** |
|
| 129 | * @return driverStatistic |
|
| 130 | */ |
|
| 131 | public function getStats() |
|
| 132 | { |
|
| 133 | $stats = (array) apc_cache_info('user'); |
|
| 134 | $date = (new \DateTime())->setTimestamp($stats[ 'start_time' ]); |
|
| 135 | ||
| 136 | return (new driverStatistic()) |
|
| 137 | ->setData(implode(', ', array_keys($this->itemInstances))) |
|
| 138 | ->setInfo(sprintf("The APCU cache is up since %s, and have %d item(s) in cache.\n For more information see RawData.", $date->format(DATE_RFC2822), $stats[ 'num_entries' ])) |
|
| 139 | ->setRawData($stats) |
|
| 140 | ->setSize($stats[ 'mem_size' ]); |
|
| 141 | } |
|
| 142 | } |
|