1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* APC store. |
4
|
|
|
* |
5
|
|
|
* @package SugiPHP.Cache |
6
|
|
|
* @author Plamen Popov <[email protected]> |
7
|
|
|
* @license http://opensource.org/licenses/mit-license.php (MIT License) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace SugiPHP\Cache; |
11
|
|
|
|
12
|
|
|
class ApcStore implements StoreInterface, IncrementorInterface |
13
|
|
|
{ |
14
|
|
|
// for some optimization reasons in APC it does not invalidate |
15
|
|
|
// data on same request. @see https://bugs.php.net/bug.php?id=58084 |
16
|
|
|
// To fix this behavior we'll use cache to store items along with timestamps |
17
|
|
|
public $ttlFix = false; |
18
|
|
|
|
19
|
|
|
protected $ttls = array(); |
20
|
|
|
|
21
|
|
|
public function __construct(array $config = array()) |
22
|
|
|
{ |
23
|
|
|
// do we need a TTL fix |
24
|
|
|
// usually need to be set to true only for unit testing |
25
|
|
|
if (!empty($config["ttl_fix"])) { |
26
|
|
|
$this->ttlFix = true; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
View Code Duplication |
public function add($key, $value, $ttl = 0) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$res = apc_add($key, $value, $ttl); |
36
|
|
|
if ($this->ttlFix) { |
37
|
|
|
unset($this->ttls[$key]); |
38
|
|
|
// fixing ttl only if it is set |
39
|
|
|
if ($res && $ttl) { |
40
|
|
|
$this->ttls[$key] = microtime(true) + $ttl; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
return $res; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function set($key, $value, $ttl = 0) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$res = apc_store($key, $value, $ttl); |
52
|
|
|
if ($this->ttlFix) { |
53
|
|
|
unset($this->ttls[$key]); |
54
|
|
|
// fixing ttl only if it is set |
55
|
|
|
if ($res && $ttl) { |
56
|
|
|
$this->ttls[$key] = microtime(true) + $ttl; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $res; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function get($key) |
67
|
|
|
{ |
68
|
|
|
$success = null; |
69
|
|
|
$result = apc_fetch($key, $success); |
70
|
|
|
|
71
|
|
|
if (!$success) { |
72
|
|
|
return null; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
if ($this->ttlFix) { |
|
|
|
|
76
|
|
|
if (isset($this->ttls[$key]) && ($this->ttls[$key] < microtime(true))) { |
77
|
|
|
unset($this->ttls[$key]); |
78
|
|
|
return null; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $result; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function has($key) |
89
|
|
|
{ |
90
|
|
|
if (!apc_exists($key)) { |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
View Code Duplication |
if ($this->ttlFix) { |
|
|
|
|
95
|
|
|
if (isset($this->ttls[$key]) && ($this->ttls[$key] < microtime(true))) { |
96
|
|
|
unset($this->ttls[$key]); |
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
public function delete($key) |
108
|
|
|
{ |
109
|
|
|
if (apc_delete($key)) { |
110
|
|
|
if ($this->ttlFix) { |
111
|
|
|
unset($this->ttls[$key]); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
public function flush() |
120
|
|
|
{ |
121
|
|
|
if (extension_loaded("apcu")) { |
122
|
|
|
apc_clear_cache(); |
123
|
|
|
} else { |
124
|
|
|
apc_clear_cache("user"); |
125
|
|
|
} |
126
|
|
|
if ($this->ttlFix) { |
127
|
|
|
unset($this->ttls); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
|
|
public function inc($key, $step = 1) |
135
|
|
|
{ |
136
|
|
|
return apc_inc($key, $step); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
|
|
public function dec($key, $step = 1) |
143
|
|
|
{ |
144
|
|
|
return apc_dec($key, $step); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Checks APC is running |
149
|
|
|
* |
150
|
|
|
* @return boolean |
151
|
|
|
*/ |
152
|
|
|
public function checkRunning() |
153
|
|
|
{ |
154
|
|
|
return (function_exists("apc_store") && ini_get("apc.enabled") |
155
|
|
|
&& ((PHP_SAPI != "cli") || ini_get("apc.enable_cli"))); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.