@@ -31,66 +31,66 @@ |
||
| 31 | 31 | */ |
| 32 | 32 | class CappedMemoryCache implements ICache, \ArrayAccess { |
| 33 | 33 | |
| 34 | - private $capacity; |
|
| 35 | - private $cache = []; |
|
| 36 | - |
|
| 37 | - public function __construct($capacity = 512) { |
|
| 38 | - $this->capacity = $capacity; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - public function hasKey($key) { |
|
| 42 | - return isset($this->cache[$key]); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - public function get($key) { |
|
| 46 | - return isset($this->cache[$key]) ? $this->cache[$key] : null; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - public function set($key, $value, $ttl = 0) { |
|
| 50 | - if (is_null($key)) { |
|
| 51 | - $this->cache[] = $value; |
|
| 52 | - } else { |
|
| 53 | - $this->cache[$key] = $value; |
|
| 54 | - } |
|
| 55 | - $this->garbageCollect(); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - public function remove($key) { |
|
| 59 | - unset($this->cache[$key]); |
|
| 60 | - return true; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - public function clear($prefix = '') { |
|
| 64 | - $this->cache = []; |
|
| 65 | - return true; |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - public function offsetExists($offset) { |
|
| 69 | - return $this->hasKey($offset); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - public function &offsetGet($offset) { |
|
| 73 | - return $this->cache[$offset]; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - public function offsetSet($offset, $value) { |
|
| 77 | - $this->set($offset, $value); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - public function offsetUnset($offset) { |
|
| 81 | - $this->remove($offset); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - public function getData() { |
|
| 85 | - return $this->cache; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - |
|
| 89 | - private function garbageCollect() { |
|
| 90 | - while (count($this->cache) > $this->capacity) { |
|
| 91 | - reset($this->cache); |
|
| 92 | - $key = key($this->cache); |
|
| 93 | - $this->remove($key); |
|
| 94 | - } |
|
| 95 | - } |
|
| 34 | + private $capacity; |
|
| 35 | + private $cache = []; |
|
| 36 | + |
|
| 37 | + public function __construct($capacity = 512) { |
|
| 38 | + $this->capacity = $capacity; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + public function hasKey($key) { |
|
| 42 | + return isset($this->cache[$key]); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + public function get($key) { |
|
| 46 | + return isset($this->cache[$key]) ? $this->cache[$key] : null; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + public function set($key, $value, $ttl = 0) { |
|
| 50 | + if (is_null($key)) { |
|
| 51 | + $this->cache[] = $value; |
|
| 52 | + } else { |
|
| 53 | + $this->cache[$key] = $value; |
|
| 54 | + } |
|
| 55 | + $this->garbageCollect(); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + public function remove($key) { |
|
| 59 | + unset($this->cache[$key]); |
|
| 60 | + return true; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + public function clear($prefix = '') { |
|
| 64 | + $this->cache = []; |
|
| 65 | + return true; |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + public function offsetExists($offset) { |
|
| 69 | + return $this->hasKey($offset); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + public function &offsetGet($offset) { |
|
| 73 | + return $this->cache[$offset]; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + public function offsetSet($offset, $value) { |
|
| 77 | + $this->set($offset, $value); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + public function offsetUnset($offset) { |
|
| 81 | + $this->remove($offset); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + public function getData() { |
|
| 85 | + return $this->cache; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + |
|
| 89 | + private function garbageCollect() { |
|
| 90 | + while (count($this->cache) > $this->capacity) { |
|
| 91 | + reset($this->cache); |
|
| 92 | + $key = key($this->cache); |
|
| 93 | + $this->remove($key); |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | 96 | } |