1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onoi\Cache; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Implements a simple LRU (Least Recently Used) algorithm for a fixed in-memory |
7
|
|
|
* hashmap |
8
|
|
|
* |
9
|
|
|
* @note For a size of more than 10K it is suggested to use PHP's SplFixedArray |
10
|
|
|
* instead as it is optimized for large array sets |
11
|
|
|
* |
12
|
|
|
* @license GNU GPL v2+ |
13
|
|
|
* @since 1.0 |
14
|
|
|
*/ |
15
|
|
|
class FixedInMemoryLruCache implements Cache { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $cache = array(); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $ttl = array(); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var integer |
29
|
|
|
*/ |
30
|
|
|
private $maxCacheCount; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var integer |
34
|
|
|
*/ |
35
|
|
|
private $count = 0; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var integer |
39
|
|
|
*/ |
40
|
|
|
private $cacheInserts = 0; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var integer |
44
|
|
|
*/ |
45
|
|
|
private $cacheDeletes = 0; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var integer |
49
|
|
|
*/ |
50
|
|
|
private $cacheHits = 0; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var integer |
54
|
|
|
*/ |
55
|
|
|
private $cacheMisses = 0; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @since 1.0 |
59
|
|
|
* |
60
|
|
|
* @param integer $maxCacheCount |
61
|
|
|
*/ |
62
|
12 |
|
public function __construct( $maxCacheCount = 500 ) { |
63
|
12 |
|
$this->maxCacheCount = (int)$maxCacheCount; |
64
|
12 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @since 1.0 |
68
|
|
|
* |
69
|
|
|
* {@inheritDoc} |
70
|
|
|
*/ |
71
|
10 |
|
public function contains( $id ) { |
72
|
|
|
|
73
|
10 |
|
$contains = isset( $this->cache[ $id ] ) || array_key_exists( $id, $this->cache ); |
74
|
|
|
|
75
|
10 |
|
if ( !$contains ) { |
76
|
10 |
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
10 |
|
if ( $this->ttl[ $id ] > 0 && $this->ttl[ $id ] <= microtime( true ) ) { |
80
|
1 |
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
10 |
|
return true; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @since 1.0 |
88
|
|
|
* |
89
|
|
|
* {@inheritDoc} |
90
|
|
|
*/ |
91
|
9 |
|
public function fetch( $id ) { |
92
|
|
|
|
93
|
9 |
|
if ( $this->contains( $id ) ) { |
94
|
9 |
|
$this->cacheHits++; |
95
|
9 |
|
return $this->moveToMostRecentlyUsedPosition( $id ); |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
$this->cacheMisses++; |
99
|
2 |
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @since 1.0 |
104
|
|
|
* |
105
|
|
|
* {@inheritDoc} |
106
|
|
|
*/ |
107
|
10 |
|
public function save( $id, $value, $ttl = 0 ) { |
108
|
|
|
|
109
|
10 |
|
$this->count++; |
110
|
10 |
|
$this->cacheInserts++; |
111
|
|
|
|
112
|
10 |
|
if ( $this->contains( $id ) ) { |
113
|
1 |
|
$this->count--; |
114
|
1 |
|
$this->moveToMostRecentlyUsedPosition( $id ); |
115
|
10 |
|
} elseif ( $this->count > $this->maxCacheCount ) { |
116
|
1 |
|
$this->count--; |
117
|
1 |
|
reset( $this->cache ); |
118
|
1 |
|
unset( $this->cache[ key( $this->cache ) ] ); |
119
|
1 |
|
} |
120
|
|
|
|
121
|
10 |
|
$this->cache[ $id ] = $value; |
122
|
10 |
|
$this->ttl[ $id ] = $ttl > 0 ? microtime( true ) + $ttl : 0; |
123
|
10 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @since 1.0 |
127
|
|
|
* |
128
|
|
|
* {@inheritDoc} |
129
|
|
|
*/ |
130
|
2 |
|
public function delete( $id ) { |
131
|
|
|
|
132
|
2 |
|
if ( $this->contains( $id ) ) { |
133
|
2 |
|
$this->count--; |
134
|
2 |
|
$this->cacheDeletes++; |
135
|
2 |
|
unset( $this->cache[ $id ] ); |
136
|
2 |
|
unset( $this->ttl[ $id ] ); |
137
|
2 |
|
return true; |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
return false; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @since 1.0 |
145
|
|
|
* |
146
|
|
|
* {@inheritDoc} |
147
|
|
|
*/ |
148
|
2 |
|
public function getStats() { |
149
|
|
|
return array( |
150
|
2 |
|
'inserts' => $this->cacheInserts, |
151
|
2 |
|
'deletes' => $this->cacheDeletes, |
152
|
2 |
|
'max' => $this->maxCacheCount, |
153
|
2 |
|
'count' => $this->count, |
154
|
2 |
|
'hits' => $this->cacheHits, |
155
|
2 |
|
'misses' => $this->cacheMisses |
156
|
2 |
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @since 1.2 |
161
|
|
|
* |
162
|
|
|
* {@inheritDoc} |
163
|
|
|
*/ |
164
|
1 |
|
public function getName() { |
165
|
1 |
|
return __CLASS__; |
166
|
|
|
} |
167
|
|
|
|
168
|
9 |
|
private function moveToMostRecentlyUsedPosition( $id ) { |
169
|
|
|
|
170
|
9 |
|
$value = $this->cache[ $id ]; |
171
|
9 |
|
unset( $this->cache[ $id ] ); |
172
|
9 |
|
$this->cache[ $id ] = $value; |
173
|
|
|
|
174
|
9 |
|
return $value; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
} |
178
|
|
|
|