1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Shieldon Simple Cache package. |
4
|
|
|
* |
5
|
|
|
* (c) Terry L. <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Shieldon\SimpleCache\Driver; |
14
|
|
|
|
15
|
|
|
use Shieldon\SimpleCache\CacheProvider; |
16
|
|
|
use Shieldon\SimpleCache\Exception\CacheException; |
17
|
|
|
use function unserialize; |
18
|
|
|
use function serialize; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Provided by APC extension. |
22
|
|
|
*/ |
23
|
|
|
use function apc_clear_cache; |
24
|
|
|
use function apc_delete; |
25
|
|
|
use function apc_exists; |
26
|
|
|
use function apc_fetch; |
27
|
|
|
use function apc_store; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* A cache driver class provided by APC (Alternative PHP Cache). |
31
|
|
|
* |
32
|
|
|
* @see https://www.php.net/manual/en/book.apc.php |
33
|
|
|
*/ |
34
|
|
|
class Apc extends CacheProvider |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* Constructor. |
38
|
|
|
* |
39
|
|
|
* @param array $setting The settings. |
40
|
|
|
* |
41
|
|
|
* @throws CacheException |
42
|
|
|
*/ |
43
|
8 |
|
public function __construct(array $setting = []) |
44
|
|
|
{ |
45
|
8 |
|
if (!function_exists('apc_fetch')) { |
46
|
|
|
// @codeCoverageIgnoreStart |
47
|
|
|
throw new CacheException( |
48
|
|
|
'APC extension is not enable.' |
49
|
|
|
); |
50
|
|
|
// @codeCoverageIgnoreEnd |
51
|
|
|
} |
52
|
|
|
|
53
|
8 |
|
unset($setting); |
54
|
8 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Fetch a cache by an extended Cache Driver. |
58
|
|
|
* |
59
|
|
|
* @param string $key The key of a cache. |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
6 |
|
protected function doGet(string $key): array |
64
|
|
|
{ |
65
|
6 |
|
$success = false; |
66
|
6 |
|
$content = apc_fetch($key, $success); |
67
|
|
|
|
68
|
6 |
|
if (empty($content) || !$success) { |
69
|
4 |
|
return []; |
70
|
|
|
} |
71
|
6 |
|
$data = unserialize($content); |
72
|
|
|
|
73
|
6 |
|
return $data; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set a cache by an extended Cache Driver. |
78
|
|
|
* |
79
|
|
|
* @param string $key The key of a cache. |
80
|
|
|
* @param mixed $value The value of a cache. (serialized) |
81
|
|
|
* @param int $ttl The time to live for a cache. |
82
|
|
|
* @param int $timestamp The time to store a cache. |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
6 |
|
protected function doSet(string $key, $value, int $ttl, int $timestamp): bool |
87
|
|
|
{ |
88
|
|
|
$contents = [ |
89
|
6 |
|
'timestamp' => $timestamp, |
90
|
6 |
|
'ttl' => $ttl, |
91
|
6 |
|
'value' => $value |
92
|
|
|
]; |
93
|
|
|
|
94
|
6 |
|
$result = apc_store( |
95
|
6 |
|
$key, |
96
|
6 |
|
serialize($contents), |
97
|
6 |
|
$ttl |
98
|
|
|
); |
99
|
|
|
|
100
|
6 |
|
return $result; |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Delete a cache by an extended Cache Driver. |
105
|
|
|
* |
106
|
|
|
* @param string $key The key of a cache. |
107
|
|
|
* |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
4 |
|
protected function doDelete(string $key): bool |
111
|
|
|
{ |
112
|
4 |
|
return apc_delete($key); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Delete all caches by an extended Cache Driver. |
117
|
|
|
* |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
2 |
|
protected function doClear(): bool |
121
|
|
|
{ |
122
|
2 |
|
return apc_clear_cache('user'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Check if the cache exists or not. |
127
|
|
|
* |
128
|
|
|
* @param string $key The key of a cache. |
129
|
|
|
* |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
4 |
|
protected function doHas(string $key): bool |
133
|
|
|
{ |
134
|
4 |
|
return apc_exists($key); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|