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\CacheArgumentException; |
17
|
|
|
use Shieldon\SimpleCache\Exception\CacheException; |
18
|
|
|
use PDO; |
19
|
|
|
use PDOException; |
20
|
|
|
use Exception; |
21
|
|
|
use function file_put_contents; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* A cache driver class provided by MySQL database. |
25
|
|
|
*/ |
26
|
|
|
class Mysql extends CacheProvider |
27
|
|
|
{ |
28
|
|
|
use SqlTrait; |
29
|
|
|
|
30
|
|
|
protected $type = 'mysql'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Constructor. |
34
|
|
|
* |
35
|
|
|
* @param array $setting The settings. |
36
|
|
|
* |
37
|
|
|
* @throws CacheArgumentException |
38
|
|
|
*/ |
39
|
16 |
|
public function __construct($setting) |
40
|
|
|
{ |
41
|
|
|
$config = [ |
42
|
16 |
|
'host' => '127.0.0.1', |
43
|
|
|
'port' => 3306, |
44
|
|
|
'user' => null, |
45
|
|
|
'pass' => null, |
46
|
|
|
'dbname' => null, |
47
|
|
|
'table' => 'cache_data', |
48
|
|
|
'charset' => 'utf8', |
49
|
|
|
]; |
50
|
|
|
|
51
|
16 |
|
foreach (array_keys($config) as $key) { |
52
|
16 |
|
if (isset($setting[$key])) { |
53
|
16 |
|
$config[$key] = $setting[$key]; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
16 |
|
$this->assertSettingFields($config); |
58
|
14 |
|
$this->connect($config); |
59
|
14 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Connect to MySQL server. |
63
|
|
|
* |
64
|
|
|
* @param array $config The settings. |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
* |
68
|
|
|
* @throws CacheException |
69
|
|
|
*/ |
70
|
14 |
|
protected function connect(array $config): void |
71
|
|
|
{ |
72
|
|
|
$host = 'mysql' . |
73
|
14 |
|
':host=' . $config['host'] . |
74
|
14 |
|
';port=' . $config['port'] . |
75
|
14 |
|
';dbname=' . $config['dbname'] . |
76
|
14 |
|
';charset='. $config['charset']; |
77
|
|
|
|
78
|
14 |
|
$user = $config['user']; |
79
|
14 |
|
$pass = $config['pass']; |
80
|
|
|
|
81
|
|
|
try { |
82
|
14 |
|
$this->db = new PDO($host, $user, $pass); |
83
|
|
|
|
84
|
|
|
// @codeCoverageIgnoreStart |
85
|
|
|
} catch (PDOException $e) { |
86
|
|
|
throw new CacheException($e->getMessage()); |
87
|
|
|
} |
88
|
|
|
// @codeCoverageIgnoreEnd |
89
|
14 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Rebuild the cache storage. |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
4 |
|
public function rebuild(): bool |
97
|
|
|
{ |
98
|
|
|
try { |
99
|
4 |
|
$sql = "CREATE TABLE IF NOT EXISTS `{$this->table}` ( |
100
|
|
|
`cache_key` varchar(40) NOT NULL, |
101
|
|
|
`cache_value` longtext, |
102
|
|
|
PRIMARY KEY (`cache_key`) |
103
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;"; |
104
|
|
|
|
105
|
4 |
|
$this->db->query($sql); |
106
|
|
|
|
107
|
|
|
// @codeCoverageIgnoreStart |
108
|
|
|
} catch (Exception $e) { |
109
|
|
|
file_put_contents('php://stderr', $e->getMessage()); |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
// @codeCoverageIgnoreEnd |
113
|
|
|
|
114
|
4 |
|
return true; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|