|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* User: jg |
|
4
|
|
|
* Date: 13/02/17 |
|
5
|
|
|
* Time: 16:29 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace ByJG\AnyDataset\Store; |
|
9
|
|
|
|
|
10
|
|
|
use ByJG\AnyDataset\DbDriverInterface; |
|
11
|
|
|
use ByJG\AnyDataset\Dataset\ArrayDataset; |
|
12
|
|
|
use ByJG\Util\Uri; |
|
13
|
|
|
use DateInterval; |
|
14
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
15
|
|
|
|
|
16
|
|
|
class DbCached implements DbDriverInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var \ByJG\AnyDataset\DbDriverInterface|null |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $dbDriver = null; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var CacheItemPoolInterface; |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $cacheEngine = null; |
|
27
|
|
|
|
|
28
|
|
|
protected $timeToCache = 30; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* DbCached constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param \ByJG\AnyDataset\DbDriverInterface|null $dbDriver |
|
34
|
|
|
* @param \Psr\Cache\CacheItemPoolInterface $cacheEngine |
|
35
|
|
|
* @param int $timeToCache |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(DbDriverInterface $dbDriver, CacheItemPoolInterface $cacheEngine, $timeToCache = 30) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->dbDriver = $dbDriver; |
|
40
|
|
|
$this->cacheEngine = $cacheEngine; |
|
41
|
|
|
$this->timeToCache = $timeToCache; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
public function getIterator($sql, $params = null) |
|
46
|
|
|
{ |
|
47
|
|
|
// Otherwise try to get from cache |
|
48
|
|
|
$key = $this->getQueryKey($sql, $params); |
|
49
|
|
|
|
|
50
|
|
|
// Get the CACHE |
|
51
|
|
|
$cacheItem = $this->cacheEngine->getItem($key); |
|
52
|
|
|
if (!$cacheItem->isHit()) { |
|
53
|
|
|
$iterator = $this->dbDriver->getIterator($sql, $params); |
|
54
|
|
|
|
|
55
|
|
|
$cacheItem->set($iterator->toArray()); |
|
56
|
|
|
$cacheItem->expiresAfter(DateInterval::createFromDateString($this->timeToCache . " seconds")); |
|
57
|
|
|
|
|
58
|
|
|
$this->cacheEngine->save($cacheItem); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$arrayDS = new ArrayDataset($cacheItem->get()); |
|
62
|
|
|
return $arrayDS->getIterator(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function getQueryKey($sql, $array) |
|
66
|
|
|
{ |
|
67
|
|
|
$key1 = md5($sql); |
|
68
|
|
|
|
|
69
|
|
|
// Check which parameter exists in the SQL |
|
70
|
|
|
$arKey2 = array(); |
|
71
|
|
|
if (is_array($array)) { |
|
72
|
|
|
foreach ($array as $key => $value) { |
|
73
|
|
|
if (preg_match("/\[\[$key\]\]/", $sql)) { |
|
74
|
|
|
$arKey2[$key] = $value; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// Define the query key |
|
80
|
|
|
if (is_array($arKey2) && count($arKey2) > 0) { |
|
81
|
|
|
$key2 = ":" . md5(json_encode($arKey2)); |
|
82
|
|
|
} else { |
|
83
|
|
|
$key2 = ""; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return "qry:" . $key1 . $key2; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function getScalar($sql, $array = null) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->dbDriver->getScalar($sql, $array); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function getAllFields($tablename) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->dbDriver->getAllFields($tablename); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function execute($sql, $array = null) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->dbDriver->execute($sql, $array); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function beginTransaction() |
|
105
|
|
|
{ |
|
106
|
|
|
$this->dbDriver->beginTransaction(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function commitTransaction() |
|
110
|
|
|
{ |
|
111
|
|
|
$this->dbDriver->commitTransaction(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function rollbackTransaction() |
|
115
|
|
|
{ |
|
116
|
|
|
$this->dbDriver->rollbackTransaction(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function getDbConnection() |
|
120
|
|
|
{ |
|
121
|
|
|
$this->dbDriver->getDbConnection(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function setAttribute($name, $value) |
|
125
|
|
|
{ |
|
126
|
|
|
$this->dbDriver->setAttribute($name, $value); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function getAttribute($name) |
|
130
|
|
|
{ |
|
131
|
|
|
$this->dbDriver->getAttribute($name); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function executeAndGetId($sql, $array = null) |
|
135
|
|
|
{ |
|
136
|
|
|
$this->dbDriver->executeAndGetId($sql, $array); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function getDbHelper() |
|
140
|
|
|
{ |
|
141
|
|
|
$this->dbDriver->getDbHelper(); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @return Uri |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getUri() |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->dbDriver->getUri(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function isSupportMultRowset() |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->dbDriver->isSupportMultRowset(); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public function setSupportMultRowset($multipleRowSet) |
|
158
|
|
|
{ |
|
159
|
|
|
$this->dbDriver->setSupportMultRowset($multipleRowSet); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|