1
|
|
|
<?php /** MicroDbDriver */ |
2
|
|
|
|
3
|
|
|
namespace Micro\Cache\Drivers; |
4
|
|
|
|
5
|
|
|
use Micro\Base\Exception; |
6
|
|
|
use Micro\Db\ConnectionInjector; |
7
|
|
|
use Micro\Db\Drivers\IDriver; |
8
|
|
|
use Micro\Mvc\Models\Query; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class DbDriver |
12
|
|
|
* |
13
|
|
|
* @author Oleg Lunegov <[email protected]> |
14
|
|
|
* @link https://github.com/linpax/microphp-framework |
15
|
|
|
* @copyright Copyright (c) 2013 Oleg Lunegov |
16
|
|
|
* @license https://github.com/linpax/microphp-framework/blob/master/LICENSE |
17
|
|
|
* @package Micro |
18
|
|
|
* @subpackage Cache\Driver |
19
|
|
|
* @version 1.0 |
20
|
|
|
* @since 1.0 |
21
|
|
|
*/ |
22
|
|
|
class DbDriver extends CacheDriver |
23
|
|
|
{ |
24
|
|
|
/** @var IDriver $driver DB driver */ |
25
|
|
|
protected $driver; |
26
|
|
|
/** @var string $table table name */ |
27
|
|
|
protected $table; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor |
32
|
|
|
* |
33
|
|
|
* @access public |
34
|
|
|
* |
35
|
|
|
* @param array $config config array |
36
|
|
|
* |
37
|
|
|
* @result void |
38
|
|
|
* @throws Exception |
39
|
|
|
*/ |
40
|
|
|
public function __construct(array $config = []) |
41
|
|
|
{ |
42
|
|
|
parent::__construct($config); |
43
|
|
|
|
44
|
|
|
$this->table = 'cache'; |
45
|
|
|
if (!empty($config['table'])) { |
46
|
|
|
$this->table = $config['table']; |
47
|
|
|
unset($config['table']); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->driver = (new ConnectionInjector)->getDriver(); |
51
|
|
|
|
52
|
|
|
$this->driver->createTable($this->table, [ |
53
|
|
|
'`name` VARCHAR(127) NOT NULL', |
54
|
|
|
'`value` TEXT NULL', |
55
|
|
|
'`duration` INT(11) NOT NULL', |
56
|
|
|
'`date_create` INT(11) NOT NULL', |
57
|
|
|
'UNIQUE(`name`)' |
58
|
|
|
], ''); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritdoc |
63
|
|
|
*/ |
64
|
|
|
public function check() |
65
|
|
|
{ |
66
|
|
|
return ($this->driver instanceof IDriver) && $this->driver->tableExists($this->table) ?: false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritdoc |
71
|
|
|
* @throws \Micro\Base\Exception |
72
|
|
|
*/ |
73
|
|
|
public function get($name) |
74
|
|
|
{ |
75
|
|
|
/** @var array $element */ |
76
|
|
|
if ($element = $this->getElement($name)) { |
77
|
|
|
return array_key_exists('value', $element) ? $element['value'] : null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritdoc |
85
|
|
|
* @throws \Micro\Base\Exception |
86
|
|
|
* @param string $name |
87
|
|
|
*/ |
88
|
|
|
protected function getElement($name) |
89
|
|
|
{ |
90
|
|
|
$query = new Query($this->driver); |
91
|
|
|
$query->table = $this->table; |
92
|
|
|
$query->addWhere('`name`=:name'); |
93
|
|
|
$query->params = ['name' => $name]; |
94
|
|
|
$query->limit = 1; |
95
|
|
|
$query->single = true; |
96
|
|
|
|
97
|
|
|
return $query->run(\PDO::FETCH_ASSOC); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @inheritdoc |
102
|
|
|
*/ |
103
|
|
|
public function set($name, $value) |
104
|
|
|
{ |
105
|
|
|
return $this->driver->update($this->table, ['`name`="'.$value.'"'], 'name="'.$name.'"'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @inheritdoc |
110
|
|
|
*/ |
111
|
|
|
public function delete($name) |
112
|
|
|
{ |
113
|
|
|
return $this->driver->delete($this->table, 'name=:name', ['name' => $name]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @inheritdoc |
118
|
|
|
*/ |
119
|
|
|
public function clean() |
120
|
|
|
{ |
121
|
|
|
$this->driver->clearTable($this->table); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @inheritdoc |
126
|
|
|
*/ |
127
|
|
|
public function info() |
128
|
|
|
{ |
129
|
|
|
return $this->driver->count(null, $this->table); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @inheritdoc |
134
|
|
|
* @throws \Micro\Base\Exception |
135
|
|
|
*/ |
136
|
|
|
public function getMeta($id) |
137
|
|
|
{ |
138
|
|
|
return $this->getElement($id); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @inheritdoc |
143
|
|
|
*/ |
144
|
|
|
public function increment($name, $offset = 1) |
145
|
|
|
{ |
146
|
|
|
return $this->driver->update($this->table, ['value' => 'value+'.$offset], 'name="'.$name.'"'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @inheritdoc |
151
|
|
|
*/ |
152
|
|
|
public function decrement($name, $offset = 1) |
153
|
|
|
{ |
154
|
|
|
return $this->driver->update($this->table, ['value' => 'value-'.$offset], 'name="'.$name.'"'); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|