1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace voku\cache; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* AdapterApc: a APC-Cache adapter |
9
|
|
|
* |
10
|
|
|
* http://php.net/manual/de/book.apc.php |
11
|
|
|
* |
12
|
|
|
* @package voku\cache |
13
|
|
|
*/ |
14
|
|
|
class AdapterApc implements iAdapter |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
public $installed = false; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
public $debug = false; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* __construct() |
29
|
|
|
*/ |
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
if ( |
33
|
|
|
\function_exists('apc_store') === true |
34
|
|
|
&& |
35
|
|
|
\ini_get('apc.enabled') |
36
|
|
|
) { |
37
|
|
|
$this->installed = true; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Check if apc-cache exists. |
43
|
|
|
* |
44
|
|
|
* WARNING: use $this->exists($key) instead |
45
|
|
|
* |
46
|
|
|
* @param string $key |
47
|
|
|
* |
48
|
|
|
* @return bool |
49
|
|
|
* |
50
|
|
|
* @internal |
51
|
|
|
*/ |
52
|
|
|
public function apc_cache_exists($key): bool |
53
|
|
|
{ |
54
|
|
|
return (bool)\apc_fetch($key); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Clears the APC cache by type. |
59
|
|
|
* |
60
|
|
|
* @param string $type - If $type is "user", the user cache will be cleared; otherwise, |
61
|
|
|
* the system cache (cached files) will be cleared. |
62
|
|
|
* |
63
|
|
|
* @return bool |
64
|
|
|
* |
65
|
|
|
* @internal |
66
|
|
|
*/ |
67
|
|
|
public function cacheClear(string $type): bool |
68
|
|
|
{ |
69
|
|
|
return \apc_clear_cache($type); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Retrieves cached information from APC's data store |
74
|
|
|
* |
75
|
|
|
* @param string $type - If $type is "user", information about the user cache will be returned. |
76
|
|
|
* @param bool $limited - If $limited is TRUE, the return value will exclude the individual list of cache entries. |
77
|
|
|
* This is useful when trying to optimize calls for statistics gathering. |
78
|
|
|
* |
79
|
|
|
* @return array of cached data (and meta-data) or FALSE on failure. |
80
|
|
|
*/ |
81
|
|
|
public function cacheInfo(string $type = '', bool $limited = false): array |
82
|
|
|
{ |
83
|
|
|
return \apc_cache_info($type, $limited); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritdoc |
88
|
|
|
*/ |
89
|
|
|
public function exists(string $key): bool |
90
|
|
|
{ |
91
|
|
|
if (\function_exists('apc_exists')) { |
92
|
|
|
return \apc_exists($key); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $this->apc_cache_exists($key); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @inheritdoc |
100
|
|
|
*/ |
101
|
|
|
public function get(string $key) |
102
|
|
|
{ |
103
|
|
|
if ($this->exists($key)) { |
104
|
|
|
return \apc_fetch($key); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @inheritdoc |
112
|
|
|
*/ |
113
|
|
|
public function installed(): bool |
114
|
|
|
{ |
115
|
|
|
return $this->installed; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @inheritdoc |
120
|
|
|
*/ |
121
|
|
|
public function remove(string $key): bool |
122
|
|
|
{ |
123
|
|
|
return \apc_delete($key); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @inheritdoc |
128
|
|
|
*/ |
129
|
|
|
public function removeAll(): bool |
130
|
|
|
{ |
131
|
|
|
return $this->cacheClear('system') && $this->cacheClear('user'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @inheritdoc |
136
|
|
|
*/ |
137
|
|
|
public function set(string $key, $value): bool |
138
|
|
|
{ |
139
|
|
|
return \apc_store($key, $value); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @inheritdoc |
144
|
|
|
*/ |
145
|
|
|
public function setExpired(string $key, $data, int $ttl = 0): bool |
146
|
|
|
{ |
147
|
|
|
return \apc_store($key, $data, $ttl); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
} |
151
|
|
|
|