1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Query\Extension; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Cache\CacheInterface; |
6
|
|
|
use Bdf\Prime\Cache\CacheKey; |
7
|
|
|
use Bdf\Prime\Connection\ConnectionInterface; |
|
|
|
|
8
|
|
|
use Bdf\Prime\Query\Contract\Cachable; |
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Provides result cache on queries |
12
|
|
|
* |
13
|
|
|
* @see Cachable |
14
|
|
|
* |
15
|
|
|
* @property ConnectionInterface $connection |
16
|
|
|
* |
17
|
|
|
* @todo Cache statement instead of assoc array result ? |
|
|
|
|
18
|
|
|
*/ |
19
|
|
|
trait CachableTrait |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var null|CacheInterface |
23
|
|
|
*/ |
24
|
|
|
protected $cache; |
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var bool |
|
|
|
|
28
|
|
|
*/ |
29
|
|
|
protected $disableCache = true; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var CacheKey |
33
|
|
|
*/ |
34
|
|
|
protected $cacheKey = null; |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @see Cachable::cache() |
39
|
|
|
*/ |
40
|
4 |
|
public function cache() |
|
|
|
|
41
|
|
|
{ |
42
|
4 |
|
return $this->disableCache ? null : $this->cache; |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
|
|
|
|
46
|
|
|
* @see Cachable::setCache() |
47
|
|
|
*/ |
48
|
482 |
|
public function setCache(CacheInterface $cache = null) |
49
|
|
|
{ |
50
|
482 |
|
$this->disableCache = $cache === null; |
|
|
|
|
51
|
482 |
|
$this->cache = $cache; |
52
|
|
|
|
53
|
482 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @see Cachable::disableCache() |
58
|
|
|
*/ |
59
|
7 |
|
public function disableCache() |
60
|
|
|
{ |
61
|
7 |
|
$this->disableCache = true; |
62
|
|
|
|
63
|
7 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Define the cache lifetime |
68
|
|
|
* |
69
|
|
|
* @param int $lifetime The cache lifetime in seconds |
|
|
|
|
70
|
|
|
* |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
1 |
|
public function setCacheLifetime(int $lifetime) |
74
|
|
|
{ |
75
|
1 |
|
$this->getCacheKey()->setLifetime($lifetime); |
76
|
|
|
|
77
|
1 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Define the cache key |
82
|
|
|
* |
83
|
|
|
* @param string $cacheKey |
84
|
|
|
* |
85
|
|
|
* @return $this |
86
|
|
|
*/ |
87
|
1 |
|
public function setCacheKey(string $cacheKey) |
88
|
|
|
{ |
89
|
1 |
|
$this->getCacheKey()->setKey($cacheKey); |
90
|
|
|
|
91
|
1 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Define the cache namespace |
96
|
|
|
* |
97
|
|
|
* @param string $namespace |
98
|
|
|
* |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
1 |
|
public function setCacheNamespace(string $namespace) |
102
|
|
|
{ |
103
|
1 |
|
$this->getCacheKey()->setNamespace($namespace); |
104
|
|
|
|
105
|
1 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the cache key |
110
|
|
|
* |
111
|
|
|
* @return CacheKey |
112
|
|
|
*/ |
113
|
560 |
|
public function getCacheKey(): CacheKey |
114
|
|
|
{ |
115
|
560 |
|
if ($this->cacheKey === null) { |
116
|
546 |
|
return $this->cacheKey = new CacheKey( |
|
|
|
|
117
|
|
|
function () { return $this->cacheNamespace(); }, |
|
|
|
|
118
|
|
|
function () { return $this->cacheKey(); } |
|
|
|
|
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
139 |
|
return $this->cacheKey; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Retrieve data from cache, or execute the query and save into cache |
127
|
|
|
* |
128
|
|
|
* @return mixed |
129
|
|
|
*/ |
130
|
559 |
|
protected function executeCached() |
131
|
|
|
{ |
132
|
559 |
|
$key = $this->getCacheKey(); |
133
|
|
|
|
134
|
559 |
|
if ($this->disableCache || !$key->valid()) { |
135
|
543 |
|
return $this->connection->execute($this)->all(); |
|
|
|
|
136
|
|
|
} |
137
|
|
|
|
138
|
23 |
|
$data = $this->cache->get($key); |
|
|
|
|
139
|
|
|
|
140
|
23 |
|
if ($data !== null) { |
|
|
|
|
141
|
5 |
|
return $data; |
142
|
|
|
} |
143
|
|
|
|
144
|
23 |
|
$data = $this->connection->execute($this)->all(); |
145
|
|
|
|
146
|
23 |
|
$this->cache->set($key, $data); |
147
|
|
|
|
148
|
23 |
|
return $data; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Clear the cache when a write operation is performed |
153
|
|
|
*/ |
154
|
565 |
|
protected function clearCacheOnWrite() |
155
|
|
|
{ |
156
|
565 |
|
if ($this->cache) { |
157
|
10 |
|
$this->cache->flush($this->getCacheKey()->namespace()); |
158
|
|
|
} |
159
|
565 |
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get the cache key |
163
|
|
|
* The cache key is generated from the query string |
|
|
|
|
164
|
|
|
* |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
protected function cacheKey() |
168
|
|
|
{ |
169
|
|
|
return null; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get cache namespace |
174
|
|
|
* The namespace is in form : [connection name] ":" [table name] |
|
|
|
|
175
|
|
|
* |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
16 |
|
protected function cacheNamespace() |
179
|
|
|
{ |
180
|
16 |
|
return $this->connection->getName().':'.(isset($this->statements['tables'][0]['table']) ? $this->statements['tables'][0]['table'] : ''); |
|
|
|
|
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|