1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\Cache; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* CappedStorage |
6
|
|
|
* @package PHPDaemon\Cache |
7
|
|
|
* @author Vasily Zorin <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
abstract class CappedStorage |
10
|
|
|
{ |
11
|
|
|
use \PHPDaemon\Traits\ClassWatchdog; |
12
|
|
|
use \PHPDaemon\Traits\StaticObjectWatchdog; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var callable Sorter function |
16
|
|
|
*/ |
17
|
|
|
public $sorter; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var integer Maximum number of cached elements |
21
|
|
|
*/ |
22
|
|
|
public $maxCacheSize = 64; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var integer Additional window to decrease number of sorter calls |
26
|
|
|
*/ |
27
|
|
|
public $capWindow = 16; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array Storage of cached items |
31
|
|
|
*/ |
32
|
|
|
public $cache = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Sets cache size |
36
|
|
|
* @param integer $size Maximum number of elements |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
public function setMaxCacheSize($size) |
40
|
|
|
{ |
41
|
|
|
$this->maxCacheSize = $size; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Sets cap window |
46
|
|
|
* @param integer $w Additional window to decrease number of sorter calls |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
public function setCapWindow($w) |
50
|
|
|
{ |
51
|
|
|
$this->capWindow = $w; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Puts element in cache |
56
|
|
|
* @param string $key Key |
57
|
|
|
* @param mixed $value Value |
58
|
|
|
* @param integer $ttl Time to live |
|
|
|
|
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
|
|
public function put($key, $value, $ttl = null) |
62
|
|
|
{ |
63
|
|
|
$k = $this->hash($key); |
64
|
|
|
if (isset($this->cache[$k])) { |
65
|
|
|
$item = $this->cache[$k]; |
66
|
|
|
$item->setValue($value); |
67
|
|
|
if ($ttl !== null) { |
68
|
|
|
$item->expire = microtime(true) + $ttl; |
69
|
|
|
} |
70
|
|
|
return $item; |
71
|
|
|
} |
72
|
|
|
$item = new Item($value); |
73
|
|
|
if ($ttl !== null) { |
74
|
|
|
$item->expire = microtime(true) + $ttl; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
$this->cache[$k] = $item; |
77
|
|
|
$s = sizeof($this->cache); |
78
|
|
|
if ($s > $this->maxCacheSize + $this->capWindow) { |
79
|
|
|
uasort($this->cache, $this->sorter); |
80
|
|
|
for (; $s > $this->maxCacheSize; --$s) { |
81
|
|
|
array_pop($this->cache); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
return $item; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Hash function |
89
|
|
|
* @param string $key Key |
90
|
|
|
* @return integer |
|
|
|
|
91
|
|
|
*/ |
92
|
|
|
public function hash($key) |
93
|
|
|
{ |
94
|
|
|
return $key; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Invalidates cache element |
99
|
|
|
* @param string $key Key |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
public function invalidate($key) |
103
|
|
|
{ |
104
|
|
|
$k = $this->hash($key); |
105
|
|
|
unset($this->cache[$k]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Gets element by key |
110
|
|
|
* @param string $key Key |
111
|
|
|
* @return object Item |
112
|
|
|
*/ |
113
|
|
View Code Duplication |
public function get($key) |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$k = $this->hash($key); |
116
|
|
|
if (!isset($this->cache[$k])) { |
117
|
|
|
return null; |
118
|
|
|
} |
119
|
|
|
$item = $this->cache[$k]; |
120
|
|
|
if (isset($item->expire)) { |
121
|
|
|
if (microtime(true) >= $item->expire) { |
122
|
|
|
unset($this->cache[$k]); |
123
|
|
|
return null; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
return $item; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Gets value of element by key |
131
|
|
|
* @param string $key Key |
132
|
|
|
* @return mixed |
133
|
|
|
*/ |
134
|
|
View Code Duplication |
public function getValue($key) |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
$k = $this->hash($key); |
137
|
|
|
if (!isset($this->cache[$k])) { |
138
|
|
|
return null; |
139
|
|
|
} |
140
|
|
|
$item = $this->cache[$k]; |
141
|
|
|
if (isset($item->expire)) { |
142
|
|
|
if (microtime(true) >= $item->expire) { |
143
|
|
|
unset($this->cache[$k]); |
144
|
|
|
return null; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
return $item->getValue(); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.