1
|
|
|
<?php |
2
|
|
|
require_once(dirname(__FILE__) . '/cache.php'); |
3
|
|
|
require_once(FWOLFLIB . 'func/filesystem.php'); |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Key - value cache system, data store in file. |
8
|
|
|
* |
9
|
|
|
* @deprecated Use Fwlib\Cache\CacheFile |
10
|
|
|
* @package fwolflib |
11
|
|
|
* @subpackage class.cache |
12
|
|
|
* @copyright Copyright 2010-2012, Fwolf |
13
|
|
|
* @author Fwolf <[email protected]> |
14
|
|
|
* @since 2010-01-07 |
15
|
|
|
*/ |
16
|
|
|
class CacheFile extends Cache { |
|
|
|
|
17
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Constructor |
21
|
|
|
* |
22
|
|
|
* @param array $ar_cfg |
23
|
|
|
*/ |
24
|
|
|
public function __construct ($ar_cfg = array()) { |
25
|
|
|
parent::__construct($ar_cfg); |
26
|
|
|
|
27
|
|
|
// Unset for auto new |
28
|
|
|
} // end of func __construct |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Check if cache is ready for use. |
33
|
|
|
* |
34
|
|
|
* @return boolean |
35
|
|
|
*/ |
36
|
|
|
public function ChkCfg () { |
37
|
|
|
$b_pass = true; |
38
|
|
|
|
39
|
|
View Code Duplication |
if (empty($this->aCfg['cache-file-dir'])) |
|
|
|
|
40
|
|
|
$b_pass = false; |
41
|
|
|
else { |
42
|
|
|
$s = $this->ChkCfgFileDir($this->aCfg['cache-file-dir']); |
43
|
|
|
if (!empty($s)) { |
44
|
|
|
$this->Log('Cache file cfg dir error: ' . $s, 4); |
45
|
|
|
$b_pass = false; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
View Code Duplication |
if (empty($this->aCfg['cache-file-rule'])) |
|
|
|
|
50
|
|
|
$b_pass = false; |
51
|
|
|
else { |
52
|
|
|
$s = $this->ChkCfgFileRule($this->aCfg['cache-file-rule']); |
53
|
|
|
if (!empty($s)) { |
54
|
|
|
$this->Log('Cache file cfg rule error: ' . $s, 4); |
55
|
|
|
$b_pass = false; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $b_pass; |
60
|
|
|
} // end of func ChkCfg |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Check config/cache store dir valid and writable |
65
|
|
|
* If error, return error msg, else return empty str. |
66
|
|
|
* |
67
|
|
|
* @param string $dir |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function ChkCfgFileDir ($dir) { |
71
|
|
|
$s = ''; |
72
|
|
|
|
73
|
|
|
if (empty($dir)) |
74
|
|
|
$s = "Cache dir {$dir} is not defined."; |
75
|
|
|
|
76
|
|
View Code Duplication |
if (!file_exists($dir)) { |
|
|
|
|
77
|
|
|
if (false == mkdir($dir, 0755, true)) |
|
|
|
|
78
|
|
|
$s = "Fail to create cache dir {$dir}."; |
79
|
|
|
} |
80
|
|
|
else { |
81
|
|
|
if (!is_writable($dir)) |
82
|
|
|
$s = "Cache dir {$dir} is not writable."; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $s; |
86
|
|
|
} // end of func ChkCfgFileDir |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Check cache rule exist and valid |
91
|
|
|
* If error, return error msg, else return empty str. |
92
|
|
|
* |
93
|
|
|
* @param string $rule |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
public function ChkCfgFileRule($rule) { |
|
|
|
|
97
|
|
|
if (2 > strlen($rule)) |
98
|
|
|
return("Cache rule is not defined or too short."); |
99
|
|
|
|
100
|
|
|
if (0 != (strlen($rule) % 2)) |
101
|
|
|
return("Cache rule {$this->aCfg['cache-file-rule']} may not right."); |
102
|
|
|
|
103
|
|
|
return ''; |
104
|
|
|
} // end of func ChkCfgFileRule |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Delete cache data |
109
|
|
|
* @param string $key |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
|
|
public function Del ($key) { |
113
|
|
|
$s_file = $this->FilePath($key); |
114
|
|
|
|
115
|
|
|
if (file_exists($s_file)) { |
116
|
|
|
unlink($s_file); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} // end of func Del |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Is cache data expire ? |
125
|
|
|
* |
126
|
|
|
* File cache does not keep lifetime in cache, |
127
|
|
|
* so it need a lifetime from outside, |
128
|
|
|
* or use default lifetime config. |
129
|
|
|
* |
130
|
|
|
* @param string $key |
131
|
|
|
* @param int $lifetime Cache lifetime, in second. |
132
|
|
|
* @return boolean True means it IS expired. |
133
|
|
|
*/ |
134
|
|
|
public function Expire ($key, $lifetime = NULL) { |
135
|
|
|
$s_file = $this->FilePath($key); |
136
|
|
|
|
137
|
|
|
// File doesn't exist |
138
|
|
|
if (!file_exists($s_file)) |
139
|
|
|
return true; |
140
|
|
|
|
141
|
|
|
if (0 == $lifetime) |
|
|
|
|
142
|
|
|
return false; |
143
|
|
|
|
144
|
|
|
// Check file expire time |
145
|
|
|
$t_expire = $this->ExpireTime($lifetime, filemtime($s_file)); |
|
|
|
|
146
|
|
|
if (time() > $t_expire) |
147
|
|
|
return true; |
148
|
|
|
else |
149
|
|
|
return false; |
150
|
|
|
} // end of func Expire |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Compute path of a key's data file |
155
|
|
|
* |
156
|
|
|
* @param string $key |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
View Code Duplication |
public function FilePath ($key) { |
|
|
|
|
160
|
|
|
$s_path = $this->aCfg['cache-file-dir']; |
161
|
|
|
|
162
|
|
|
$ar_rule = str_split($this->aCfg['cache-file-rule'], 2); |
163
|
|
|
if (empty($ar_rule)) |
164
|
|
|
return $s_path; |
165
|
|
|
|
166
|
|
|
foreach ($ar_rule as $rule) |
167
|
|
|
$s_path .= $this->FilePathSec($rule, $key) . '/'; |
168
|
|
|
|
169
|
|
|
// Filename |
170
|
|
|
$s_path .= $this->FilePathFilename($key); |
171
|
|
|
|
172
|
|
|
return $s_path; |
173
|
|
|
} // end of func FilePath |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Compute name of a key's data file |
178
|
|
|
* |
179
|
|
|
* @param string $key |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
protected function FilePathFilename($key) { |
183
|
|
|
return substr(md5($key), 0, 8); |
184
|
|
|
} // end of func FilePathFilename |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Compute path of a key by a single rule section |
189
|
|
|
* |
190
|
|
|
* @param string $rule |
191
|
|
|
* @param string $key |
192
|
|
|
* @return string |
193
|
|
|
* @see $sCacheRule |
194
|
|
|
*/ |
195
|
|
View Code Duplication |
protected function FilePathSec($rule, $key) { |
|
|
|
|
196
|
|
|
$i_len = 2; |
197
|
|
|
|
198
|
|
|
if ($i_len > strlen($rule)) |
199
|
|
|
return ''; |
200
|
|
|
|
201
|
|
|
$i = intval($rule{1}); |
202
|
|
|
if (1 == $rule{0}) { |
203
|
|
|
// md5 from start |
204
|
|
|
$i_start = $i_len * $i; |
205
|
|
|
$s_seed = md5($key); |
206
|
|
|
} elseif (2 == $rule{0}) { |
207
|
|
|
// md5 from end |
208
|
|
|
$i_start = -1 * $i_len * ($i + 1); |
209
|
|
|
$s_seed = md5($key); |
210
|
|
|
} elseif (3 == $rule{0}) { |
211
|
|
|
// raw from start |
212
|
|
|
$i_start = $i_len * $i; |
213
|
|
|
$s_seed = $key; |
214
|
|
|
} elseif (4 == $rule{0}) { |
215
|
|
|
// raw from end |
216
|
|
|
$i_start = -1 * $i_len * ($i + 1); |
217
|
|
|
$s_seed = $key; |
218
|
|
|
} elseif (5 == $rule{0}) { |
219
|
|
|
// crc32 |
220
|
|
|
if (3 < $i) |
221
|
|
|
$i = $i % 3; |
222
|
|
|
$i_start = $i_len * $i; |
223
|
|
|
$s_seed = hash('crc32', $key); |
224
|
|
|
} |
225
|
|
|
return(substr($s_seed, $i_start, 2)); |
|
|
|
|
226
|
|
|
} // end of func FilePathSec |
227
|
|
|
|
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Read cache and return value |
231
|
|
|
* |
232
|
|
|
* File cache should check lifetime when get, |
233
|
|
|
* return NULL when fail. |
234
|
|
|
* |
235
|
|
|
* @param string $key |
236
|
|
|
* @param int $lifetime Cache lifetime, 0/no check, NULL/cfg |
237
|
|
|
* @return mixed |
238
|
|
|
*/ |
239
|
|
|
public function Get ($key, $lifetime = NULL) { |
240
|
|
|
if ($this->Expire($key, $lifetime)) { |
241
|
|
|
parent::$aLogGet[] = array( |
242
|
|
|
'key' => $key, |
243
|
|
|
'success' => false, |
244
|
|
|
); |
245
|
|
|
return NULL; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
// Read from file and parse it. |
249
|
|
|
$s_file = $this->FilePath($key); |
250
|
|
|
$s_cache = file_get_contents($s_file); |
251
|
|
|
parent::$aLogGet[] = array( |
252
|
|
|
'key' => $key, |
253
|
|
|
'success' => !(false === $s_cache), |
254
|
|
|
); |
255
|
|
|
|
256
|
|
|
return $this->ValDecode($s_cache); |
|
|
|
|
257
|
|
|
} // end of func Get |
258
|
|
|
|
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Init treatment |
262
|
|
|
* |
263
|
|
|
* @param array $ar_cfg |
|
|
|
|
264
|
|
|
* @return object |
265
|
|
|
*/ |
266
|
|
|
public function Init () { |
267
|
|
|
parent::Init(); |
268
|
|
|
|
269
|
|
|
$this->ChkCfg(); |
270
|
|
|
|
271
|
|
|
return $this; |
272
|
|
|
} // end of func Init |
273
|
|
|
|
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Write data to cache |
277
|
|
|
* |
278
|
|
|
* Lifetime check when get. |
279
|
|
|
* |
280
|
|
|
* @param string $key |
281
|
|
|
* @param mixed $val |
282
|
|
|
* @param int $lifetime |
283
|
|
|
* @return $this |
284
|
|
|
*/ |
285
|
|
View Code Duplication |
public function Set ($key, $val, $lifetime = NULL) { |
|
|
|
|
286
|
|
|
$s_file = $this->FilePath($key); |
287
|
|
|
$s_cache = $this->ValEncode($val); |
|
|
|
|
288
|
|
|
|
289
|
|
|
// Create each level dir if not exists |
290
|
|
|
$s_dir = DirName1($s_file); |
|
|
|
|
291
|
|
|
if (!file_exists($s_dir)) |
292
|
|
|
mkdir($s_dir, 0755, true); |
293
|
|
|
|
294
|
|
|
// Finally write file |
295
|
|
|
file_put_contents($s_file, $s_cache, LOCK_EX); |
296
|
|
|
|
297
|
|
|
return $this; |
298
|
|
|
} // end of func Set |
299
|
|
|
|
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Set default config |
303
|
|
|
* |
304
|
|
|
* @return this |
305
|
|
|
*/ |
306
|
|
View Code Duplication |
protected function SetCfgDefault () { |
|
|
|
|
307
|
|
|
parent::SetCfgDefault(); |
308
|
|
|
|
309
|
|
|
// Cache type: file |
310
|
|
|
$this->aCfg['cache-type'] = 'file'; |
311
|
|
|
|
312
|
|
|
|
313
|
|
|
// Dir where data file store |
314
|
|
|
$this->aCfg['cache-file-dir'] = '/tmp/cache/'; |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Cache file store rule |
318
|
|
|
* |
319
|
|
|
* Group by every 2-chars, their means: |
320
|
|
|
* 10 first 2 char of md5 hash, 16 * 16 = 256 |
321
|
|
|
* 11 3-4 char of md5 hash |
322
|
|
|
* 20 last 2 char of md5 hash |
323
|
|
|
* 30 first 2 char of key |
324
|
|
|
* 40 last 2 char of key |
325
|
|
|
* 5n crc32, n=0..3, 16 * 16 = 256 |
326
|
|
|
* Join these str with '/', got full path of cache file. |
327
|
|
|
*/ |
328
|
|
|
$this->aCfg['cache-file-rule'] = '10'; |
329
|
|
|
|
330
|
|
|
|
331
|
|
|
return $this; |
332
|
|
|
} // end of func SetCfgDefault |
333
|
|
|
|
334
|
|
|
|
335
|
|
|
} // end of class CacheFile |
336
|
|
|
?> |
|
|
|
|
337
|
|
|
|