|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kemist\Cache; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Cache Info |
|
7
|
|
|
* |
|
8
|
|
|
* @version 1.0.4 |
|
9
|
|
|
*/ |
|
10
|
|
|
class Info implements \ArrayAccess, \IteratorAggregate { |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Cache info data |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $data = array(); |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Constructor |
|
20
|
|
|
* |
|
21
|
|
|
* @param array $data |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(array $data = array()) { |
|
24
|
|
|
$this->data = $data; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* ArrayAccess offsetExists |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $offset |
|
31
|
|
|
* @return bool |
|
32
|
|
|
*/ |
|
33
|
|
|
public function offsetExists($offset) { |
|
34
|
|
|
return isset($this->data[$offset]); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* ArrayAccess offsetGet |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $offset |
|
41
|
|
|
* @return mixed |
|
42
|
|
|
*/ |
|
43
|
|
|
public function offsetGet($offset) { |
|
44
|
|
|
return $this->data[$offset]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* ArrayAccess offsetSet |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $offset |
|
51
|
|
|
* @param mixed $value |
|
52
|
|
|
*/ |
|
53
|
|
|
public function offsetSet($offset, $value) { |
|
54
|
|
|
$this->data[$offset] = $value; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* ArrayAccess offsetUnset |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $offset |
|
61
|
|
|
*/ |
|
62
|
|
|
public function offsetUnset($offset) { |
|
63
|
|
|
unset($this->data[$offset]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* IteratorAggregate |
|
68
|
|
|
* |
|
69
|
|
|
* @return \ArrayIterator |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getIterator() { |
|
72
|
|
|
return new \ArrayIterator($this->data); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Updates timestamp items in cache info |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $name |
|
79
|
|
|
* @param array|string $itemNames |
|
80
|
|
|
*/ |
|
81
|
|
|
public function touchItem($name, $itemNames = array()) { |
|
82
|
|
|
if (!is_array($itemNames)) { |
|
83
|
|
|
$itemNames = array($itemNames); |
|
84
|
|
|
} |
|
85
|
|
|
foreach ($itemNames as $itemName) { |
|
86
|
|
|
$this->data[$name][$itemName] = time(); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Gets an item from cache info |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $name |
|
94
|
|
|
* @param string $itemName |
|
95
|
|
|
* @param string $type |
|
96
|
|
|
* @param string $format |
|
97
|
|
|
* |
|
98
|
|
|
* @return string|int|bool |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getItem($name, $itemName, $type = 'int', $format = 'U') { |
|
101
|
|
|
if (!isset($this->data[$name])) { |
|
102
|
|
|
return false; |
|
103
|
|
|
} |
|
104
|
|
|
switch ($type) { |
|
105
|
|
|
case 'date': |
|
106
|
|
|
return isset($this->data[$name][$itemName]) ? date($format, $this->data[$name][$itemName]) : null; |
|
107
|
|
|
case 'int': |
|
108
|
|
|
return (int) $this->getItemOrDefault($name, $itemName, 0); |
|
109
|
|
|
default: |
|
110
|
|
|
return $this->getItemOrDefault($name, $itemName); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Gets an item value or default if not exists |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $name |
|
118
|
|
|
* @param string $itemName |
|
119
|
|
|
* @param mixed $default |
|
120
|
|
|
* |
|
121
|
|
|
* @return mixed |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function getItemOrDefault($name, $itemName, $default = null) { |
|
124
|
|
|
return isset($this->data[$name][$itemName]) ? $this->data[$name][$itemName] : $default; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Sets an item |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $name |
|
131
|
|
|
* @param string $item |
|
132
|
|
|
* @param mixed $value |
|
133
|
|
|
*/ |
|
134
|
|
|
public function setItem($name, $item, $value) { |
|
135
|
|
|
$this->data[$name][$item] = $value; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Inreases an item in cache info |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $name |
|
142
|
|
|
* @param string $itemName |
|
143
|
|
|
*/ |
|
144
|
|
|
public function increaseItem($name, $itemName) { |
|
145
|
|
|
$this->data[$name][$itemName] = (isset($this->data[$name][$itemName]) && is_int($this->data[$name][$itemName]) ? ++$this->data[$name][$itemName] : 1); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Gets Cache info data |
|
150
|
|
|
* |
|
151
|
|
|
* @param $name Cache key |
|
152
|
|
|
* |
|
153
|
|
|
* @return array |
|
154
|
|
|
*/ |
|
155
|
|
|
public function getData($name = '') { |
|
156
|
|
|
return $name == '' ? $this->data : (isset($this->data[$name]) ? $this->data[$name] : null); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Gets all cache key names |
|
161
|
|
|
* |
|
162
|
|
|
* @return array |
|
163
|
|
|
*/ |
|
164
|
|
|
public function getKeys() { |
|
165
|
|
|
return array_keys($this->data); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Creates an info item |
|
170
|
|
|
* |
|
171
|
|
|
* @param string $name |
|
172
|
|
|
*/ |
|
173
|
|
|
public function createData($name) { |
|
174
|
|
|
$this->data[$name] = array( |
|
175
|
|
|
'last_read' => null, |
|
176
|
|
|
'read_count' => 0, |
|
177
|
|
|
'tags' => array() |
|
178
|
|
|
); |
|
179
|
|
|
$this->touchItem($name, 'created'); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Deletes an info item |
|
184
|
|
|
* |
|
185
|
|
|
* @param string $name |
|
186
|
|
|
*/ |
|
187
|
|
|
public function deleteData($name) { |
|
188
|
|
|
if (isset($this->data[$name])) { |
|
189
|
|
|
unset($this->data[$name]); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Appends data to an item |
|
195
|
|
|
* |
|
196
|
|
|
* @param string $name |
|
197
|
|
|
* @param array $data |
|
198
|
|
|
*/ |
|
199
|
|
|
public function appendData($name, array $data) { |
|
200
|
|
|
$this->data[$name] = array_merge($this->data[$name], $data); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Sets info data |
|
205
|
|
|
* |
|
206
|
|
|
* @param array $data |
|
207
|
|
|
*/ |
|
208
|
|
|
public function setData(array $data) { |
|
209
|
|
|
$this->data = $data; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Gets expiry information |
|
214
|
|
|
* |
|
215
|
|
|
* @param string $name Cache name |
|
216
|
|
|
* @param string $format Date format |
|
217
|
|
|
* |
|
218
|
|
|
* @return string |
|
219
|
|
|
*/ |
|
220
|
|
|
public function getExpiry($name, $format = 'U') { |
|
221
|
|
|
return isset($this->data[$name]['expiry']) ? ($this->data[$name]['expiry'] == 0 ? 0 : date($format, $this->data[$name]['expiry'])) : null; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Get cache names having the given tags |
|
226
|
|
|
* |
|
227
|
|
|
* @param array $tags |
|
228
|
|
|
* |
|
229
|
|
|
* @return array |
|
230
|
|
|
*/ |
|
231
|
|
|
public function filterByTags(array $tags) { |
|
232
|
|
|
$result = array(); |
|
233
|
|
|
foreach ($this->data as $key => $info) { |
|
234
|
|
|
if (count(array_intersect($tags, $info['tags'])) > 0) { |
|
235
|
|
|
$result[] = $key; |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
return $result; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
} |
|
242
|
|
|
|