| Total Complexity | 12 |
| Total Lines | 81 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 6 | final class MemoryAdapter implements CacheAdapterInterface |
||
| 7 | { |
||
| 8 | /** |
||
| 9 | * @var array |
||
| 10 | */ |
||
| 11 | private $data = []; |
||
| 12 | /** |
||
| 13 | * @var array |
||
| 14 | */ |
||
| 15 | private $timeToLive = []; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @param string $key |
||
| 19 | * @return mixed |
||
| 20 | */ |
||
| 21 | public function get(string $key) |
||
| 22 | { |
||
| 23 | if (!isset($this->data[$key])) { |
||
| 24 | return null; |
||
| 25 | } |
||
| 26 | |||
| 27 | if (!isset($this->timeToLive[$key])) { |
||
| 28 | return $this->data[$key]; |
||
| 29 | } |
||
| 30 | |||
| 31 | if ($this->ttl($key) > 0) { |
||
| 32 | return $this->data[$key]; |
||
| 33 | } |
||
| 34 | |||
| 35 | unset($this->timeToLive[$key], $this->data[$key]); |
||
| 36 | return null; |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param string $key |
||
| 41 | * @param $value |
||
| 42 | * @param int|null $ttl |
||
| 43 | * @return void |
||
| 44 | */ |
||
| 45 | public function set(string $key, $value, int $ttl = null): void |
||
| 46 | { |
||
| 47 | $this->data[$key] = $value; |
||
| 48 | if ($ttl !== null) { |
||
| 49 | $this->timeToLive[$key] = time() + $ttl; |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param string $key |
||
| 55 | * @return void |
||
| 56 | */ |
||
| 57 | public function delete(string $key): void |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param string $key |
||
| 64 | * @return mixed |
||
| 65 | */ |
||
| 66 | public function keys($key = '*') |
||
| 67 | { |
||
| 68 | $matches = []; |
||
| 69 | foreach ($this->data as $name => $value) { |
||
| 70 | if (fnmatch($key, $name)) { |
||
| 71 | $matches[] = $name; |
||
| 72 | } |
||
| 73 | } |
||
| 74 | return $matches; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param $key |
||
| 79 | * @return int |
||
| 80 | */ |
||
| 81 | public function ttl($key): int |
||
| 87 | } |
||
| 88 | } |