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
|
8 |
|
throw new CacheException( |
47
|
8 |
|
'APC extension is not enable.' |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
unset($setting); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Fetch a cache by an extended Cache Driver. |
56
|
|
|
* |
57
|
|
|
* @param string $key The key of a cache. |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
protected function doGet(string $key): array |
62
|
|
|
{ |
63
|
|
|
$success = false; |
64
|
|
|
$content = apc_fetch($key, $success); |
65
|
|
|
|
66
|
|
|
if (empty($content) || !$success) { |
67
|
|
|
return []; |
68
|
|
|
} |
69
|
|
|
$data = unserialize($content); |
70
|
|
|
|
71
|
|
|
return $data; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set a cache by an extended Cache Driver. |
76
|
|
|
* |
77
|
|
|
* @param string $key The key of a cache. |
78
|
|
|
* @param mixed $value The value of a cache. (serialized) |
79
|
|
|
* @param int $ttl The time to live for a cache. |
80
|
|
|
* @param int $timestamp The time to store a cache. |
81
|
|
|
* |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
protected function doSet(string $key, $value, int $ttl, int $timestamp): bool |
85
|
|
|
{ |
86
|
|
|
$contents = [ |
87
|
|
|
'timestamp' => $timestamp, |
88
|
|
|
'ttl' => $ttl, |
89
|
|
|
'value' => $value |
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
$result = apc_store( |
93
|
|
|
$key, |
94
|
|
|
serialize($contents), |
95
|
|
|
$ttl |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
return $result; |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Delete a cache by an extended Cache Driver. |
103
|
|
|
* |
104
|
|
|
* @param string $key The key of a cache. |
105
|
|
|
* |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
|
|
protected function doDelete(string $key): bool |
109
|
|
|
{ |
110
|
|
|
return apc_delete($key); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Delete all caches by an extended Cache Driver. |
115
|
|
|
* |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
protected function doClear(): bool |
119
|
|
|
{ |
120
|
|
|
return apc_clear_cache('user'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Check if the cache exists or not. |
125
|
|
|
* |
126
|
|
|
* @param string $key The key of a cache. |
127
|
|
|
* |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
|
|
protected function doHas(string $key): bool |
131
|
|
|
{ |
132
|
|
|
return apc_exists($key); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|