|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Stuff\Webclient\Extension\Cache; |
| 4 | + |
| 5 | +use DateInterval; |
| 6 | +use Psr\SimpleCache\CacheInterface; |
| 7 | + |
| 8 | +class ArrayCache implements CacheInterface |
| 9 | +{ |
| 10 | + |
| 11 | + /** |
| 12 | + * @var array |
| 13 | + */ |
| 14 | + private $storage = []; |
| 15 | + |
| 16 | + /** |
| 17 | + * @inheritDoc |
| 18 | + */ |
| 19 | + public function get($key, $default = null) |
| 20 | + { |
| 21 | + if (!$this->has($key)) { |
| 22 | + return $default; |
| 23 | + } |
| 24 | + return $this->storage[$key]['value']; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @inheritDoc |
| 29 | + */ |
| 30 | + public function set($key, $value, $ttl = null) |
| 31 | + { |
| 32 | + $item = [ |
| 33 | + 'value' => $value, |
| 34 | + ]; |
| 35 | + $seconds = is_int($ttl) ? $ttl : 0; |
| 36 | + if ($ttl instanceof DateInterval) { |
| 37 | + if ($ttl->invert === 1) { |
| 38 | + return false; |
| 39 | + } |
| 40 | + $seconds = $ttl->days * 86400 + $ttl->h * 3600 + $ttl->i * 60 + $ttl->s; |
| 41 | + } |
| 42 | + if ($seconds) { |
| 43 | + $item['expired'] = time() + $seconds; |
| 44 | + } |
| 45 | + $this->storage[$key] = $item; |
| 46 | + return true; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @inheritDoc |
| 51 | + */ |
| 52 | + public function delete($key) |
| 53 | + { |
| 54 | + if (array_key_exists($key, $this->storage)) { |
| 55 | + unset($this->storage[$key]); |
| 56 | + } |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @inheritDoc |
| 62 | + */ |
| 63 | + public function clear() |
| 64 | + { |
| 65 | + $this->storage = []; |
| 66 | + return true; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @inheritDoc |
| 71 | + */ |
| 72 | + public function getMultiple($keys, $default = null) |
| 73 | + { |
| 74 | + $result = []; |
| 75 | + foreach ($keys as $key) { |
| 76 | + $result[$key] = $this->get($key, $default); |
| 77 | + } |
| 78 | + return $result; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @inheritDoc |
| 83 | + */ |
| 84 | + public function setMultiple($values, $ttl = null) |
| 85 | + { |
| 86 | + $result = true; |
| 87 | + foreach ($values as $key => $value) { |
| 88 | + if (!$this->set($key, $value, $ttl)) { |
| 89 | + $result = false; |
| 90 | + } |
| 91 | + } |
| 92 | + return $result; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @inheritDoc |
| 97 | + */ |
| 98 | + public function deleteMultiple($keys) |
| 99 | + { |
| 100 | + $result = true; |
| 101 | + foreach ($keys as $key) { |
| 102 | + if (!$this->delete($key)) { |
| 103 | + $result = false; |
| 104 | + } |
| 105 | + } |
| 106 | + return $result; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @inheritDoc |
| 111 | + */ |
| 112 | + public function has($key) |
| 113 | + { |
| 114 | + if (!array_key_exists($key, $this->storage)) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + if (!array_key_exists('value', $this->storage[$key])) { |
| 118 | + unset($this->storage[$key]); |
| 119 | + return false; |
| 120 | + } |
| 121 | + $expired = false; |
| 122 | + if (array_key_exists('expired', $this->storage[$key])) { |
| 123 | + $expired = time() >= $this->storage[$key]['expired']; |
| 124 | + } |
| 125 | + if ($expired) { |
| 126 | + unset($this->storage[$key]); |
| 127 | + return false; |
| 128 | + } |
| 129 | + return true; |
| 130 | + } |
| 131 | +} |
0 commit comments