Elgg  Version master
LRUCache.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Cache;
4 
6 
17 class LRUCache implements \ArrayAccess {
18 
22  protected $maximumSize;
23 
29  protected $data = [];
30 
38  public function __construct(int $size) {
39  if ($size <= 0) {
40  throw new RangeException('"size" must be greater than 0');
41  }
42 
43  $this->maximumSize = $size;
44  }
45 
54  public function get($key, $default = null) {
55  if ($this->containsKey($key)) {
56  $this->recordAccess($key);
57 
58  return $this->data[$key];
59  }
60 
61  return $default;
62  }
63 
72  public function set($key, $value) {
73  if ($this->containsKey($key)) {
74  $this->data[$key] = $value;
75  $this->recordAccess($key);
76  } else {
77  $this->data[$key] = $value;
78  if ($this->size() > $this->maximumSize) {
79  // remove least recently used element (front of array)
80  reset($this->data);
81  unset($this->data[key($this->data)]);
82  }
83  }
84  }
85 
91  public function size() {
92  return count($this->data);
93  }
94 
102  public function containsKey($key) {
103  return array_key_exists($key, $this->data);
104  }
105 
113  public function remove($key) {
114  if ($this->containsKey($key)) {
115  $value = $this->data[$key];
116  unset($this->data[$key]);
117  return $value;
118  }
119 
120  return null;
121  }
122 
128  public function clear() {
129  $this->data = [];
130  }
131 
139  protected function recordAccess($key) {
140  $value = $this->data[$key];
141  unset($this->data[$key]);
142  $this->data[$key] = $value;
143  }
144 
154  #[\ReturnTypeWillChange]
155  public function offsetSet($offset, $value) {
156  $this->set($offset, $value);
157  }
158 
167  #[\ReturnTypeWillChange]
168  public function offsetGet($offset) {
169  return $this->get($offset);
170  }
171 
180  #[\ReturnTypeWillChange]
181  public function offsetUnset($offset) {
182  $this->remove($offset);
183  }
184 
193  #[\ReturnTypeWillChange]
194  public function offsetExists($offset) {
195  return $this->containsKey($offset);
196  }
197 }
$default
Definition: checkbox.php:30
offsetUnset($offset)
Unsets a key.
Definition: LRUCache.php:181
Exception thrown to indicate range errors during program execution.
size()
Get the number of elements in the cache.
Definition: LRUCache.php:91
Least Recently Used Cache.
Definition: LRUCache.php:17
if(empty($count)) $offset
Definition: pagination.php:26
offsetExists($offset)
Does key exist?
Definition: LRUCache.php:194
recordAccess($key)
Moves the element from current position to end of array.
Definition: LRUCache.php:139
$value
Definition: generic.php:51
clear()
Clear the cache.
Definition: LRUCache.php:128
$size
Definition: thumb.php:23
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
containsKey($key)
Does the cache contain an element with this key.
Definition: LRUCache.php:102
offsetGet($offset)
Get the value for specified key.
Definition: LRUCache.php:168
offsetSet($offset, $value)
Assigns a value for the specified key.
Definition: LRUCache.php:155
__construct(int $size)
Create a LRU Cache.
Definition: LRUCache.php:38