|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Spiral Framework. |
|
4
|
|
|
* |
|
5
|
|
|
* @license MIT |
|
6
|
|
|
* @author Anton Titov (Wolfy-J) |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Spiral\Cache\Stores; |
|
10
|
|
|
|
|
11
|
|
|
use Spiral\Cache\Prototypes\CacheStore; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Talks to apc and apcu driver. |
|
15
|
|
|
*/ |
|
16
|
|
|
class APCStore extends CacheStore |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Cache driver types. |
|
20
|
|
|
*/ |
|
21
|
|
|
const APC_DRIVER = 0; |
|
22
|
|
|
const APCU_DRIVER = 1; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* {@inheritdoc} |
|
26
|
|
|
*/ |
|
27
|
|
|
private $prefix; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Cache driver type. |
|
31
|
|
|
* |
|
32
|
|
|
* @var int |
|
33
|
|
|
*/ |
|
34
|
|
|
private $driverType; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $prefix |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(string $prefix = 'spiral:') |
|
40
|
|
|
{ |
|
41
|
|
|
$this->prefix = $prefix; |
|
42
|
|
|
|
|
43
|
|
|
$this->driverType = function_exists('apcu_store') ? self::APCU_DRIVER : self::APC_DRIVER; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get APC cache type (APC or APCU). |
|
48
|
|
|
* |
|
49
|
|
|
* @see APCStore::APC_DRIVER |
|
50
|
|
|
* @see APCStore::APCU_DRIVER |
|
51
|
|
|
* @return int |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getDriver(): int |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->driverType; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public function isAvailable(): bool |
|
62
|
|
|
{ |
|
63
|
|
|
return function_exists('apcu_store') || function_exists('apc_store'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function has(string $name): bool |
|
70
|
|
|
{ |
|
71
|
|
|
if ($this->driverType == self::APCU_DRIVER) { |
|
72
|
|
|
return apcu_exists($this->prefix . $name); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return apc_exists($this->prefix . $name); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritdoc} |
|
80
|
|
|
*/ |
|
81
|
|
|
public function get(string $name) |
|
82
|
|
|
{ |
|
83
|
|
|
if ($this->driverType == self::APCU_DRIVER) { |
|
84
|
|
|
return apcu_fetch($this->prefix . $name); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return apc_fetch($this->prefix . $name); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
|
|
public function set(string $name, $data, $ttl = null) |
|
94
|
|
|
{ |
|
95
|
|
|
if ($this->driverType == self::APCU_DRIVER) { |
|
96
|
|
|
return apcu_store($this->prefix . $name, $data, $this->lifetime($ttl, 0)); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return apc_store($this->prefix . $name, $data, $this->lifetime($ttl, 0)); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* {@inheritdoc} |
|
104
|
|
|
*/ |
|
105
|
|
|
public function delete(string $name) |
|
106
|
|
|
{ |
|
107
|
|
|
if ($this->driverType == self::APCU_DRIVER) { |
|
108
|
|
|
apcu_delete($this->prefix . $name); |
|
109
|
|
|
|
|
110
|
|
|
return; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
apc_delete($this->prefix . $name); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritdoc} |
|
118
|
|
|
*/ |
|
119
|
|
View Code Duplication |
public function inc(string $name, int $delta = 1): int |
|
|
|
|
|
|
120
|
|
|
{ |
|
121
|
|
|
if ($this->driverType == self::APCU_DRIVER) { |
|
122
|
|
|
return apcu_inc($this->prefix . $name, $delta); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return apc_inc($this->prefix . $name, $delta); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* {@inheritdoc} |
|
130
|
|
|
*/ |
|
131
|
|
View Code Duplication |
public function dec(string $name, int $delta = 1): int |
|
|
|
|
|
|
132
|
|
|
{ |
|
133
|
|
|
if ($this->driverType == self::APCU_DRIVER) { |
|
134
|
|
|
return apcu_dec($this->prefix . $name, $delta); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return apc_dec($this->prefix . $name, $delta); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* {@inheritdoc} |
|
142
|
|
|
*/ |
|
143
|
|
|
public function clear() |
|
144
|
|
|
{ |
|
145
|
|
|
if ($this->driverType == self::APCU_DRIVER) { |
|
146
|
|
|
apcu_clear_cache(); |
|
147
|
|
|
|
|
148
|
|
|
return; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
apc_clear_cache('user'); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
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.