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 PDO; |
16
|
|
|
use function serialize; |
17
|
|
|
use function unserialize; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* A trait implements SQL-action methods. |
21
|
|
|
*/ |
22
|
|
|
Trait SqlTrait |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* The PDO instance. |
26
|
|
|
* |
27
|
|
|
* @var \PDO |
28
|
|
|
*/ |
29
|
|
|
protected $db; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Table name. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $table = 'cache_data'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Rebuid a database table schema for SQL-like Cache driver. |
40
|
|
|
* |
41
|
|
|
* @return bool True for success, overwise. |
42
|
|
|
*/ |
43
|
|
|
abstract public function rebuild(): bool; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Fetch a cache by an extended Cache Driver. |
47
|
|
|
* |
48
|
|
|
* @param string $key The key of a cache. |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
20 |
|
protected function doGet(string $key): array |
53
|
|
|
{ |
54
|
20 |
|
$sql = 'SELECT * FROM ' . $this->table . ' |
55
|
|
|
WHERE cache_key = :cache_key'; |
56
|
|
|
|
57
|
20 |
|
$query = $this->db->prepare($sql); |
58
|
20 |
|
$query->bindValue(':cache_key', $key, PDO::PARAM_STR); |
59
|
20 |
|
$query->execute(); |
60
|
20 |
|
$resultData = $query->fetch($this->db::FETCH_ASSOC); |
61
|
|
|
|
62
|
20 |
|
if (empty($resultData['cache_value'])) { |
63
|
20 |
|
return []; |
64
|
|
|
} |
65
|
|
|
|
66
|
16 |
|
$data = unserialize($resultData['cache_value']); |
67
|
|
|
|
68
|
16 |
|
return $data; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Set a cache by an extended Cache Driver. |
73
|
|
|
* |
74
|
|
|
* @param string $key The key of a cache. |
75
|
|
|
* @param mixed $value The value of a cache. (serialized) |
76
|
|
|
* @param int $ttl The time to live for a cache. |
77
|
|
|
* @param int $timestamp The time to store a cache. |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
20 |
|
protected function doSet(string $key, $value, int $ttl, int $timestamp): bool |
82
|
|
|
{ |
83
|
20 |
|
$cacheData = $this->get($key); |
|
|
|
|
84
|
|
|
|
85
|
20 |
|
$sql = 'INSERT INTO ' . $this->table . ' (cache_key, cache_value) |
86
|
|
|
VALUES (:cache_key, :cache_value)'; |
87
|
|
|
|
88
|
20 |
|
if (!empty($cacheData)) { |
89
|
4 |
|
$sql = 'UPDATE ' . $this->table . ' |
90
|
|
|
SET cache_value = :cache_value |
91
|
|
|
WHERE cache_key = :cache_key'; |
92
|
|
|
} |
93
|
|
|
|
94
|
20 |
|
$query = $this->db->prepare($sql); |
95
|
|
|
|
96
|
|
|
$data = [ |
97
|
20 |
|
'cache_key' => $key, |
98
|
20 |
|
'cache_value' => serialize([ |
99
|
20 |
|
'timestamp' => $timestamp, |
100
|
20 |
|
'ttl' => $ttl, |
101
|
20 |
|
'value' => $value, |
102
|
|
|
]), |
103
|
|
|
]; |
104
|
|
|
|
105
|
20 |
|
return $query->execute($data); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Delete a cache by an extended Cache Driver. |
110
|
|
|
* |
111
|
|
|
* @param string $key The key of a cache. |
112
|
|
|
* |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
12 |
|
protected function doDelete(string $key): bool |
116
|
|
|
{ |
117
|
12 |
|
$sql = 'DELETE FROM ' . $this->table . ' |
118
|
|
|
WHERE cache_key = ?'; |
119
|
|
|
|
120
|
12 |
|
$query = $this->db->prepare($sql); |
121
|
12 |
|
$result = $query->execute([$key]); |
122
|
|
|
|
123
|
12 |
|
return $result; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Delete all caches by an extended Cache Driver. |
128
|
|
|
* |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
6 |
|
protected function doClear(): bool |
132
|
|
|
{ |
133
|
6 |
|
$sql = 'TRUNCATE TABLE ' . $this->table; |
134
|
|
|
|
135
|
6 |
|
$query = $this->db->prepare($sql); |
136
|
6 |
|
$result = $query->execute(); |
137
|
|
|
|
138
|
6 |
|
return $result; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Check if the cache exists or not. |
143
|
|
|
* |
144
|
|
|
* @param string $key The key of a cache. |
145
|
|
|
* |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
8 |
|
protected function doHas(string $key): bool |
149
|
|
|
{ |
150
|
8 |
|
$sql = 'SELECT COUNT(*) FROM ' . $this->table . ' |
151
|
|
|
WHERE cache_key = :cache_key'; |
152
|
|
|
|
153
|
8 |
|
$query = $this->db->prepare($sql); |
154
|
8 |
|
$query->bindValue(':cache_key', $key, PDO::PARAM_STR); |
155
|
8 |
|
$query->execute(); |
156
|
8 |
|
$count =$query->fetchColumn(); |
157
|
|
|
|
158
|
8 |
|
return $count > 0; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Fetch all cache items. |
163
|
|
|
* |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
8 |
|
protected function getAll(): array |
167
|
|
|
{ |
168
|
8 |
|
$list = []; |
169
|
|
|
|
170
|
8 |
|
$sql = 'SELECT * FROM ' . $this->table; |
171
|
|
|
|
172
|
8 |
|
$query = $this->db->prepare($sql); |
173
|
8 |
|
$query->execute(); |
174
|
8 |
|
$results = $query->fetchAll($this->db::FETCH_ASSOC); |
175
|
|
|
|
176
|
8 |
|
foreach ($results as $row) { |
177
|
8 |
|
$key = $row['cache_key']; |
178
|
8 |
|
$value = $row['cache_value']; |
179
|
|
|
|
180
|
8 |
|
$list[$key] = unserialize($value); |
181
|
|
|
} |
182
|
8 |
|
return $list; |
183
|
|
|
} |
184
|
|
|
} |