|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Robin Appelman <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
6
|
|
|
* @license AGPL-3.0 |
|
7
|
|
|
* |
|
8
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
10
|
|
|
* as published by the Free Software Foundation. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace OC\Cache; |
|
23
|
|
|
|
|
24
|
|
|
use OCP\ICache; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* In-memory cache with a capacity limit to keep memory usage in check |
|
28
|
|
|
* |
|
29
|
|
|
* Uses a simple FIFO expiry mechanism |
|
30
|
|
|
*/ |
|
31
|
|
|
class CappedMemoryCache implements ICache, \ArrayAccess { |
|
32
|
|
|
|
|
33
|
|
|
private $capacity; |
|
34
|
|
|
private $cache = []; |
|
35
|
|
|
|
|
36
|
3 |
|
public function __construct($capacity = 512) { |
|
37
|
3 |
|
$this->capacity = $capacity; |
|
38
|
3 |
|
} |
|
39
|
|
|
|
|
40
|
3 |
|
public function hasKey($key) { |
|
41
|
3 |
|
return isset($this->cache[$key]); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
public function get($key) { |
|
45
|
1 |
|
return isset($this->cache[$key]) ? $this->cache[$key] : null; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
3 |
|
public function set($key, $value, $ttl = 0) { |
|
49
|
3 |
|
$this->cache[$key] = $value; |
|
50
|
3 |
|
$this->garbageCollect(); |
|
51
|
3 |
|
} |
|
52
|
|
|
|
|
53
|
2 |
|
public function remove($key) { |
|
54
|
2 |
|
unset($this->cache[$key]); |
|
55
|
2 |
|
return true; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
3 |
|
public function clear($prefix = '') { |
|
59
|
3 |
|
$this->cache = []; |
|
60
|
3 |
|
return true; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function offsetExists($offset) { |
|
64
|
|
|
return $this->hasKey($offset); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function offsetGet($offset) { |
|
68
|
|
|
return $this->get($offset); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function offsetSet($offset, $value) { |
|
72
|
|
|
$this->set($offset, $value); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function offsetUnset($offset) { |
|
76
|
|
|
$this->remove($offset); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
3 |
|
private function garbageCollect() { |
|
81
|
3 |
|
while (count($this->cache) > $this->capacity) { |
|
82
|
1 |
|
reset($this->cache); |
|
83
|
1 |
|
$key = key($this->cache); |
|
84
|
1 |
|
$this->remove($key); |
|
85
|
1 |
|
} |
|
86
|
3 |
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|