|
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 PDO; |
|
17
|
|
|
use function file_put_contents; |
|
18
|
|
|
use function rtrim; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* A cache driver class provided by SQLite database. |
|
22
|
|
|
*/ |
|
23
|
|
|
class Sqlite extends CacheProvider |
|
24
|
|
|
{ |
|
25
|
|
|
use SqlTrait; |
|
26
|
|
|
|
|
27
|
|
|
protected $type = 'sqlite'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The absolute path of the storage's directory. |
|
31
|
|
|
* It must be writable. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $storage = '/tmp/simple-cache'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param array $setting The settings. |
|
41
|
|
|
* |
|
42
|
|
|
* @throws CacheException |
|
43
|
|
|
*/ |
|
44
|
12 |
|
public function __construct(array $setting = []) |
|
45
|
|
|
{ |
|
46
|
12 |
|
if (isset($setting['storage'])) { |
|
47
|
12 |
|
$this->storage = rtrim($setting['storage'], '/'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
12 |
|
$this->assertDirectoryWritable($this->storage); |
|
51
|
|
|
|
|
52
|
12 |
|
$this->db = new PDO('sqlite:' . $this->storage . '/cache.sqlite3'); |
|
53
|
12 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Is SQLite database existed? |
|
57
|
|
|
* |
|
58
|
|
|
* @return bool |
|
59
|
|
|
*/ |
|
60
|
4 |
|
protected function isSQLiteFileExisted(): bool |
|
61
|
|
|
{ |
|
62
|
4 |
|
return file_exists($this->storage . '/cache.sqlite3'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Delete all caches by an extended Cache Driver. |
|
67
|
|
|
* |
|
68
|
|
|
* @return bool |
|
69
|
|
|
*/ |
|
70
|
4 |
|
protected function doClear(): bool |
|
71
|
|
|
{ |
|
72
|
4 |
|
$sql = 'DELETE FROM ' . $this->table; |
|
73
|
|
|
|
|
74
|
4 |
|
$query = $this->db->prepare($sql); |
|
75
|
4 |
|
$result = $query->execute(); |
|
76
|
|
|
|
|
77
|
4 |
|
return $result; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Rebuild the cache storage. |
|
82
|
|
|
* |
|
83
|
|
|
* @return bool |
|
84
|
|
|
*/ |
|
85
|
4 |
|
public function rebuild(): bool |
|
86
|
|
|
{ |
|
87
|
4 |
|
if (!$this->isSQLiteFileExisted()) { |
|
88
|
|
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
try { |
|
92
|
4 |
|
$sql = "CREATE TABLE IF NOT EXISTS {$this->table} ( |
|
93
|
|
|
cache_key VARCHAR(40) PRIMARY KEY, |
|
94
|
|
|
cache_value LONGTEXT |
|
95
|
|
|
);"; |
|
96
|
|
|
|
|
97
|
4 |
|
$this->db->query($sql); |
|
98
|
|
|
|
|
99
|
|
|
// @codeCoverageIgnoreStart |
|
100
|
|
|
} catch (Exception $e) { |
|
|
|
|
|
|
101
|
|
|
file_put_contents('php://stderr', $e->getMessage()); |
|
102
|
|
|
return false; |
|
103
|
|
|
} |
|
104
|
|
|
// @codeCoverageIgnoreEnd |
|
105
|
|
|
|
|
106
|
4 |
|
return true; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|