terrylinooo /
simple-cache
| 1 | <?php |
||
| 2 | /* |
||
| 3 | * This file is part of the Shieldon Simple Cache package. |
||
| 4 | * |
||
| 5 | * (c) Terry L. <[email protected]> |
||
| 6 | * |
||
| 7 | * For the full copyright and license information, please view the LICENSE |
||
| 8 | * file that was distributed with this source code. |
||
| 9 | */ |
||
| 10 | |||
| 11 | declare(strict_types=1); |
||
| 12 | |||
| 13 | namespace Shieldon\SimpleCache\Driver; |
||
| 14 | |||
| 15 | use Shieldon\SimpleCache\CacheProvider; |
||
| 16 | use Shieldon\SimpleCache\Exception\CacheException; |
||
| 17 | use function serialize; |
||
| 18 | use function str_replace; |
||
| 19 | use function unserialize; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Provided by APCu extension. |
||
| 23 | */ |
||
| 24 | use APCUIterator; |
||
| 25 | use function apcu_clear_cache; |
||
| 26 | use function apcu_delete; |
||
| 27 | use function apcu_exists; |
||
| 28 | use function apcu_fetch; |
||
| 29 | use function apcu_store; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * A cache driver class provided by APCu (APC User Cache) |
||
| 33 | * |
||
| 34 | * @see https://www.php.net/manual/en/book.apcu.php |
||
| 35 | */ |
||
| 36 | class Apcu extends CacheProvider |
||
| 37 | { |
||
| 38 | protected $type = 'apcu'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Constructor. |
||
| 42 | * |
||
| 43 | * @param array $setting The settings. |
||
| 44 | * |
||
| 45 | * @throws CacheException |
||
| 46 | */ |
||
| 47 | 14 | public function __construct(array $setting = []) |
|
| 48 | { |
||
| 49 | 14 | if (!function_exists('apcu_fetch')) { |
|
| 50 | // @codeCoverageIgnoreStart |
||
| 51 | throw new CacheException( |
||
| 52 | 'APCu extension is not enable.' |
||
| 53 | ); |
||
| 54 | // @codeCoverageIgnoreEnd |
||
| 55 | } |
||
| 56 | |||
| 57 | 14 | unset($setting); |
|
| 58 | 14 | } |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Fetch a cache by an extended Cache Driver. |
||
| 62 | * |
||
| 63 | * @param string $key The key of a cache. |
||
| 64 | * |
||
| 65 | * @return array |
||
| 66 | */ |
||
| 67 | 8 | protected function doGet(string $key): array |
|
| 68 | { |
||
| 69 | 8 | $success = false; |
|
| 70 | 8 | $content = apcu_fetch($this->getKeyName($key), $success); |
|
| 71 | |||
| 72 | 8 | if (empty($content) || !$success) { |
|
| 73 | 8 | return []; |
|
| 74 | } |
||
| 75 | 8 | $data = unserialize($content); |
|
| 76 | |||
| 77 | 8 | return $data; |
|
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Set a cache by an extended Cache Driver. |
||
| 82 | * |
||
| 83 | * @param string $key The key of a cache. |
||
| 84 | * @param mixed $value The value of a cache. (serialized) |
||
| 85 | * @param int $ttl The time to live for a cache. |
||
| 86 | * @param int $timestamp The time to store a cache. |
||
| 87 | * |
||
| 88 | * @return bool |
||
| 89 | */ |
||
| 90 | 10 | protected function doSet(string $key, $value, int $ttl, int $timestamp): bool |
|
| 91 | { |
||
| 92 | $contents = [ |
||
| 93 | 10 | 'timestamp' => $timestamp, |
|
| 94 | 10 | 'ttl' => $ttl, |
|
| 95 | 10 | 'value' => $value, |
|
| 96 | ]; |
||
| 97 | |||
| 98 | 10 | $result = apcu_store( |
|
| 99 | 10 | $this->getKeyName($key), |
|
| 100 | 10 | serialize($contents), |
|
| 101 | 10 | $ttl |
|
| 102 | ); |
||
| 103 | |||
| 104 | 10 | return $result; |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Delete a cache by an extended Cache Driver. |
||
| 109 | * |
||
| 110 | * @param string $key The key of a cache. |
||
| 111 | * |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | 2 | protected function doDelete(string $key): bool |
|
| 115 | { |
||
| 116 | 2 | return apcu_delete($this->getKeyName($key)); |
|
|
0 ignored issues
–
show
|
|||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Delete all caches by an extended Cache Driver. |
||
| 121 | * |
||
| 122 | * @return bool |
||
| 123 | */ |
||
| 124 | 6 | protected function doClear(): bool |
|
| 125 | { |
||
| 126 | 6 | return apcu_clear_cache(); |
|
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Check if the cache exists or not. |
||
| 131 | * |
||
| 132 | * @param string $key The key of a cache. |
||
| 133 | * |
||
| 134 | * @return bool |
||
| 135 | */ |
||
| 136 | 4 | protected function doHas(string $key): bool |
|
| 137 | { |
||
| 138 | 4 | return apcu_exists($this->getKeyName($key)); |
|
|
0 ignored issues
–
show
|
|||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Fetch all cache items. |
||
| 143 | * |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | 4 | protected function getAll(): array |
|
| 147 | { |
||
| 148 | 4 | $list = []; |
|
| 149 | |||
| 150 | 4 | foreach (new APCUIterator('/^sc_/') as $item) { |
|
| 151 | 2 | $key = str_replace('sc_', '', $item['key']); |
|
| 152 | 2 | $value = unserialize($item['value']); |
|
| 153 | |||
| 154 | 2 | $list[$key] = $value; |
|
| 155 | } |
||
| 156 | |||
| 157 | 4 | return $list; |
|
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Get the key name of a cache. |
||
| 162 | * |
||
| 163 | * @param string $key The key of a cache. |
||
| 164 | * |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | 10 | private function getKeyName(string $key): string |
|
| 168 | { |
||
| 169 | 10 | return 'sc_' . $key; |
|
| 170 | } |
||
| 171 | } |
||
| 172 |