|
| 1 | +<?php namespace Tests\Support\Cache\Handlers; |
| 2 | + |
| 3 | +use CodeIgniter\Cache\CacheInterface; |
| 4 | + |
| 5 | +class MockHandler implements CacheInterface |
| 6 | +{ |
| 7 | + /** |
| 8 | + * Prefixed to all cache names. |
| 9 | + * |
| 10 | + * @var string |
| 11 | + */ |
| 12 | + protected $prefix; |
| 13 | + |
| 14 | + /** |
| 15 | + * Mock cache storage. |
| 16 | + * |
| 17 | + * @var array |
| 18 | + */ |
| 19 | + protected $cache = []; |
| 20 | + |
| 21 | + //-------------------------------------------------------------------- |
| 22 | + |
| 23 | + /** |
| 24 | + * Takes care of any handler-specific setup that must be done. |
| 25 | + */ |
| 26 | + public function initialize() |
| 27 | + { |
| 28 | + // Not to see here... |
| 29 | + } |
| 30 | + |
| 31 | + //-------------------------------------------------------------------- |
| 32 | + |
| 33 | + /** |
| 34 | + * Attempts to fetch an item from the cache store. |
| 35 | + * |
| 36 | + * @param string $key Cache item name |
| 37 | + * |
| 38 | + * @return mixed |
| 39 | + */ |
| 40 | + public function get(string $key) |
| 41 | + { |
| 42 | + $key = $this->prefix . $key; |
| 43 | + |
| 44 | + return array_key_exists($key, $this->cache) |
| 45 | + ? $this->cache[$key] |
| 46 | + : null; |
| 47 | + } |
| 48 | + |
| 49 | + //-------------------------------------------------------------------- |
| 50 | + |
| 51 | + /** |
| 52 | + * Saves an item to the cache store. |
| 53 | + * |
| 54 | + * The $raw parameter is only utilized by Mamcache in order to |
| 55 | + * allow usage of increment() and decrement(). |
| 56 | + * |
| 57 | + * @param string $key Cache item name |
| 58 | + * @param $value the data to save |
| 59 | + * @param null $ttl Time To Live, in seconds (default 60) |
| 60 | + * @param boolean $raw Whether to store the raw value. |
| 61 | + * |
| 62 | + * @return mixed |
| 63 | + */ |
| 64 | + public function save(string $key, $value, int $ttl = 60, bool $raw = false) |
| 65 | + { |
| 66 | + $key = $this->prefix . $key; |
| 67 | + |
| 68 | + $this->cache[$key] = $value; |
| 69 | + |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + //-------------------------------------------------------------------- |
| 74 | + |
| 75 | + /** |
| 76 | + * Deletes a specific item from the cache store. |
| 77 | + * |
| 78 | + * @param string $key Cache item name |
| 79 | + * |
| 80 | + * @return mixed |
| 81 | + */ |
| 82 | + public function delete(string $key) |
| 83 | + { |
| 84 | + unset($this->cache[$key]); |
| 85 | + } |
| 86 | + |
| 87 | + //-------------------------------------------------------------------- |
| 88 | + |
| 89 | + /** |
| 90 | + * Performs atomic incrementation of a raw stored value. |
| 91 | + * |
| 92 | + * @param string $key Cache ID |
| 93 | + * @param integer $offset Step/value to increase by |
| 94 | + * |
| 95 | + * @return mixed |
| 96 | + */ |
| 97 | + public function increment(string $key, int $offset = 1) |
| 98 | + { |
| 99 | + $key = $this->prefix . $key; |
| 100 | + |
| 101 | + $data = $this->cache[$key] ?: null; |
| 102 | + |
| 103 | + if (empty($data)) |
| 104 | + { |
| 105 | + $data = 0; |
| 106 | + } |
| 107 | + elseif (! is_int($data)) |
| 108 | + { |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + return $this->save($key, $data + $offset); |
| 113 | + } |
| 114 | + |
| 115 | + //-------------------------------------------------------------------- |
| 116 | + |
| 117 | + /** |
| 118 | + * Performs atomic decrementation of a raw stored value. |
| 119 | + * |
| 120 | + * @param string $key Cache ID |
| 121 | + * @param integer $offset Step/value to increase by |
| 122 | + * |
| 123 | + * @return mixed |
| 124 | + */ |
| 125 | + public function decrement(string $key, int $offset = 1) |
| 126 | + { |
| 127 | + $key = $this->prefix . $key; |
| 128 | + |
| 129 | + $data = $this->cache[$key] ?: null; |
| 130 | + |
| 131 | + if (empty($data)) |
| 132 | + { |
| 133 | + $data = 0; |
| 134 | + } |
| 135 | + elseif (! is_int($data)) |
| 136 | + { |
| 137 | + return false; |
| 138 | + } |
| 139 | + |
| 140 | + return $this->save($key, $data - $offset); |
| 141 | + } |
| 142 | + |
| 143 | + //-------------------------------------------------------------------- |
| 144 | + |
| 145 | + /** |
| 146 | + * Will delete all items in the entire cache. |
| 147 | + * |
| 148 | + * @return mixed |
| 149 | + */ |
| 150 | + public function clean() |
| 151 | + { |
| 152 | + $this->cache = []; |
| 153 | + } |
| 154 | + |
| 155 | + //-------------------------------------------------------------------- |
| 156 | + |
| 157 | + /** |
| 158 | + * Returns information on the entire cache. |
| 159 | + * |
| 160 | + * The information returned and the structure of the data |
| 161 | + * varies depending on the handler. |
| 162 | + * |
| 163 | + * @return mixed |
| 164 | + */ |
| 165 | + public function getCacheInfo() |
| 166 | + { |
| 167 | + return []; |
| 168 | + } |
| 169 | + |
| 170 | + //-------------------------------------------------------------------- |
| 171 | + |
| 172 | + /** |
| 173 | + * Returns detailed information about the specific item in the cache. |
| 174 | + * |
| 175 | + * @param string $key Cache item name. |
| 176 | + * |
| 177 | + * @return mixed |
| 178 | + */ |
| 179 | + public function getMetaData(string $key) |
| 180 | + { |
| 181 | + return false; |
| 182 | + } |
| 183 | + |
| 184 | + //-------------------------------------------------------------------- |
| 185 | + |
| 186 | + /** |
| 187 | + * Determines if the driver is supported on this system. |
| 188 | + * |
| 189 | + * @return boolean |
| 190 | + */ |
| 191 | + public function isSupported(): bool |
| 192 | + { |
| 193 | + return true; |
| 194 | + } |
| 195 | + |
| 196 | + //-------------------------------------------------------------------- |
| 197 | + |
| 198 | +} |
0 commit comments