1 | <?php |
||
10 | class LRUCache |
||
11 | { |
||
12 | /** |
||
13 | * The cached items. |
||
14 | * |
||
15 | * @var LRUCacheItem[] |
||
16 | */ |
||
17 | protected $cache; |
||
18 | |||
19 | /** |
||
20 | * The maximim capacity of the cache. |
||
21 | * |
||
22 | * @var int |
||
23 | */ |
||
24 | protected $capacity; |
||
25 | |||
26 | /** |
||
27 | * Initializes the instance. |
||
28 | * |
||
29 | * @param int $capacity The maximim capacity of the cache. |
||
30 | * |
||
31 | * @throws Exception Throws an Exception in case of errors. |
||
32 | */ |
||
33 | 1 | public function __construct($capacity) |
|
41 | |||
42 | /** |
||
43 | * Get a cached items given its key (return null if no cached items with the specified key). |
||
44 | * |
||
45 | * @param mixed $key |
||
46 | * |
||
47 | * @return mixed|null |
||
48 | */ |
||
49 | 464 | public function get($key) |
|
66 | |||
67 | /** |
||
68 | * Ensure that the cache has at least one empty slot (it the cache is full, we'll remove the less used cache item). |
||
69 | * |
||
70 | * @return null|mixed Returns null if no item has been removed, otherwise returns the value of the removed item. |
||
71 | */ |
||
72 | 23 | public function prune() |
|
90 | |||
91 | /** |
||
92 | * Add a new item to the cache. |
||
93 | * |
||
94 | * @param mixed $key The key to assign to the cached item. |
||
95 | * @param mixed $value The value to be cached. |
||
96 | */ |
||
97 | 23 | public function put($key, $value) |
|
104 | |||
105 | /** |
||
106 | * Clear the cache. |
||
107 | */ |
||
108 | public function clear() |
||
112 | |||
113 | /** |
||
114 | * Get the number of the currently cached items. |
||
115 | * |
||
116 | * @return int |
||
117 | */ |
||
118 | public function size() |
||
122 | } |
||
123 |