|
1
|
|
|
<?php |
|
2
|
|
|
namespace micro\cache\system; |
|
3
|
|
|
|
|
4
|
|
|
use micro\utils\StrUtils; |
|
5
|
|
|
use micro\controllers\admin\popo\CacheFile; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* This class is responsible for storing values in apcu cache. |
|
9
|
|
|
*/ |
|
10
|
|
|
class ApcuCache extends AbstractDataCache{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Initializes the apcu cache-provider |
|
13
|
|
|
*/ |
|
14
|
|
|
public function __construct($root,$postfix="") { |
|
15
|
|
|
parent::__construct($root,$postfix); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Check if annotation-data for the key has been stored. |
|
20
|
|
|
* @param string $key cache key |
|
21
|
|
|
* @return boolean true if data with the given key has been stored; otherwise false |
|
22
|
|
|
*/ |
|
23
|
|
|
public function exists($key) { |
|
24
|
|
|
return \apcu_exists($this->getRealKey($key)); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function store($key, $code, $tag,$php=true) { |
|
28
|
|
|
$this->storeContent($key, $code,$tag); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Caches the given data with the given key. |
|
33
|
|
|
* @param string $key cache key |
|
34
|
|
|
* @param string $content the source-code to be cached |
|
35
|
|
|
* @param string $tag not used |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function storeContent($key,$content,$tag) { |
|
38
|
|
|
\apcu_store($this->getRealKey($key), $content); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
protected function getRealKey($key){ |
|
42
|
|
|
return $key; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Fetches data stored for the given key. |
|
47
|
|
|
* @param string $key cache key |
|
48
|
|
|
* @return mixed the cached data |
|
49
|
|
|
*/ |
|
50
|
|
|
public function fetch($key) { |
|
51
|
|
|
$result=\apcu_fetch($this->getRealKey($key)); |
|
52
|
|
|
return eval($result); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* return data stored for the given key. |
|
57
|
|
|
* @param string $key cache key |
|
58
|
|
|
* @return mixed the cached data |
|
59
|
|
|
*/ |
|
60
|
|
|
public function file_get_contents($key) { |
|
61
|
|
|
return \apcu_fetch($this->getRealKey($key)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Returns the timestamp of the last cache update for the given key. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $key cache key |
|
68
|
|
|
* @return int unix timestamp |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getTimestamp($key) { |
|
71
|
|
|
$key=$this->getRealKey($key); |
|
72
|
|
|
$cache = \apcu_cache_info(); |
|
73
|
|
|
if (empty($cache['cache_list'])) { |
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
foreach ($cache['cache_list'] as $entry) { |
|
77
|
|
|
if ($entry['info'] != $key) { |
|
78
|
|
|
continue; |
|
79
|
|
|
} |
|
80
|
|
|
$creationTime = $entry['creation_time']; |
|
81
|
|
|
return $creationTime; |
|
82
|
|
|
} |
|
83
|
|
|
return \time(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function remove($key) { |
|
87
|
|
|
\apcu_delete($this->getRealKey($key)); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function clear() { |
|
91
|
|
|
\apcu_clear_cache(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
protected function getCacheEntries($type){ |
|
95
|
|
|
$entries=$this->getAllEntries(); |
|
96
|
|
|
return \array_filter($entries,function($v) use ($type){return StrUtils::startswith($v['info'], $type);}); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
protected function getAllEntries(){ |
|
100
|
|
|
$entries=[]; |
|
101
|
|
|
$cache = \apcu_cache_info(); |
|
102
|
|
|
if (!empty($cache['cache_list'])) { |
|
103
|
|
|
$entries=$cache['cache_list']; |
|
104
|
|
|
} |
|
105
|
|
|
return $entries; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function getCacheFiles($type){ |
|
109
|
|
|
$result=[]; |
|
110
|
|
|
$entries=$this->getCacheEntries($type); |
|
111
|
|
|
foreach ($entries as $entry) { |
|
112
|
|
|
$key=$entry['info']; |
|
113
|
|
|
if(StrUtils::startswith($key, $type)){ |
|
114
|
|
|
$result[]=new CacheFile(\ucfirst($type),$key,$entry['creation_time'],$entry['mem_size'],$key); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
View Code Duplication |
if(\sizeof($result)===0) |
|
|
|
|
|
|
118
|
|
|
$result[]=new CacheFile(\ucfirst($type),"","",""); |
|
119
|
|
|
return $result; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function clearCache($type){ |
|
123
|
|
|
$entries=$this->getCacheEntries($type); |
|
124
|
|
|
foreach ($entries as $entry){ |
|
125
|
|
|
$this->remove($entry['info']); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
On one hand,
evalmight be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM,evalprevents some optimization that they perform.