|
1
|
|
|
<?php |
|
2
|
|
|
namespace micro\cache\system; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* This class is responsible for storing Arrays in PHP files. |
|
6
|
|
|
*/ |
|
7
|
|
|
class ApcuCache extends AbstractDataCache{ |
|
8
|
|
|
/** |
|
9
|
|
|
* Initializes the apcu cache-provider |
|
10
|
|
|
*/ |
|
11
|
|
|
public function __construct($root,$postfix="") { |
|
12
|
|
|
parent::__construct($root,$postfix); |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Check if annotation-data for the key has been stored. |
|
17
|
|
|
* @param string $key cache key |
|
18
|
|
|
* @return boolean true if data with the given key has been stored; otherwise false |
|
19
|
|
|
*/ |
|
20
|
|
|
public function exists($key) { |
|
21
|
|
|
return \apcu_exists($this->getRealKey($key)); |
|
|
|
|
|
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function store($key, $code, $php=true) { |
|
25
|
|
|
$this->storeContent($key, $code); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Caches the given data with the given key. |
|
30
|
|
|
* @param string $key cache key |
|
31
|
|
|
* @param string $content the source-code to be cached |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function storeContent($key,$content) { |
|
34
|
|
|
\apcu_store($this->getRealKey($key), $content); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected function getRealKey($key){ |
|
38
|
|
|
return \md5($key); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Fetches data stored for the given key. |
|
43
|
|
|
* @param string $key cache key |
|
44
|
|
|
* @return mixed the cached data |
|
45
|
|
|
*/ |
|
46
|
|
|
public function fetch($key) { |
|
47
|
|
|
$result=\apcu_fetch($this->getRealKey($key)); |
|
48
|
|
|
return eval($result); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* return data stored for the given key. |
|
53
|
|
|
* @param string $key cache key |
|
54
|
|
|
* @return mixed the cached data |
|
55
|
|
|
*/ |
|
56
|
|
|
public function file_get_contents($key) { |
|
57
|
|
|
return \apcu_fetch($this->getRealKey($key)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Returns the timestamp of the last cache update for the given key. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $key cache key |
|
64
|
|
|
* @return int unix timestamp |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getTimestamp($key) { |
|
67
|
|
|
$key=$this->getRealKey($key); |
|
68
|
|
|
$cache = \apc_cache_info(); |
|
69
|
|
|
if (empty($cache['cache_list'])) { |
|
70
|
|
|
return false; |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
foreach ($cache['cache_list'] as $entry) { |
|
73
|
|
|
if ($entry['info'] != $key) { |
|
74
|
|
|
continue; |
|
75
|
|
|
} |
|
76
|
|
|
$creationTime = $entry['creation_time']; |
|
77
|
|
|
return $creationTime; |
|
78
|
|
|
} |
|
79
|
|
|
return \time(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function remove($key) { |
|
83
|
|
|
\apcu_delete($this->getRealKey($key)); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function clear() { |
|
87
|
|
|
\apcu_clear_cache(); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|