|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace voku\cache; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* AdapterArray: simple array-cache adapter |
|
7
|
|
|
* |
|
8
|
|
|
* @package voku\cache |
|
9
|
|
|
*/ |
|
10
|
|
|
class AdapterArray implements iAdapter |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
private static $values = array(); |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
private static $expired = array(); |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @inheritdoc |
|
25
|
|
|
*/ |
|
26
|
6 |
|
public function exists($key) |
|
27
|
|
|
{ |
|
28
|
6 |
|
$this->removeExpired($key); |
|
29
|
|
|
|
|
30
|
6 |
|
return array_key_exists($key, self::$values); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @inheritdoc |
|
35
|
|
|
*/ |
|
36
|
5 |
|
public function get($key) |
|
37
|
|
|
{ |
|
38
|
5 |
|
return $this->exists($key) ? self::$values[$key] : null; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @inheritdoc |
|
43
|
|
|
*/ |
|
44
|
|
|
public function installed() |
|
45
|
|
|
{ |
|
46
|
|
|
return true; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @inheritdoc |
|
51
|
|
|
*/ |
|
52
|
|
|
public function remove($key) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->removeExpired($key); |
|
55
|
|
|
|
|
56
|
|
|
if (array_key_exists($key, self::$values) === true) { |
|
57
|
|
|
unset(self::$values[$key]); |
|
58
|
|
|
|
|
59
|
|
|
return true; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return false; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @inheritdoc |
|
67
|
|
|
*/ |
|
68
|
|
|
public function removeAll() |
|
69
|
|
|
{ |
|
70
|
|
|
self::$values = array(); |
|
71
|
|
|
self::$expired = array(); |
|
72
|
|
|
|
|
73
|
|
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @inheritdoc |
|
78
|
|
|
*/ |
|
79
|
3 |
|
public function set($key, $value) |
|
80
|
|
|
{ |
|
81
|
3 |
|
self::$values[$key] = $value; |
|
82
|
|
|
|
|
83
|
3 |
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @inheritdoc |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function setExpired($key, $value, $ttl) |
|
90
|
|
|
{ |
|
91
|
1 |
|
self::$values[$key] = $value; |
|
92
|
1 |
|
self::$expired[$key] = array(time(), $ttl); |
|
93
|
|
|
|
|
94
|
1 |
|
return true; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Remove expired cache. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $key |
|
101
|
|
|
* |
|
102
|
|
|
* @return boolean |
|
103
|
|
|
*/ |
|
104
|
6 |
|
private function removeExpired($key) |
|
105
|
|
|
{ |
|
106
|
|
|
if ( |
|
107
|
6 |
|
array_key_exists($key, self::$expired) === false |
|
108
|
6 |
|
|| |
|
109
|
1 |
|
array_key_exists($key, self::$values) === false |
|
110
|
6 |
|
) { |
|
111
|
5 |
|
return false; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
1 |
|
list($time, $ttl) = self::$expired[$key]; |
|
115
|
|
|
|
|
116
|
1 |
|
if (time() > ($time + $ttl)) { |
|
117
|
1 |
|
unset(self::$values[$key]); |
|
118
|
1 |
|
} |
|
119
|
|
|
|
|
120
|
1 |
|
if (!isset(self::$values[$key])) { |
|
121
|
1 |
|
unset(self::$expired[$key]); |
|
122
|
1 |
|
} |
|
123
|
|
|
|
|
124
|
1 |
|
return true; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|