|
1
|
|
|
<?php |
|
2
|
|
|
defined('ROOT_PATH') || exit('Access denied'); |
|
3
|
|
|
/** |
|
4
|
|
|
* TNH Framework |
|
5
|
|
|
* |
|
6
|
|
|
* A simple PHP framework using HMVC architecture |
|
7
|
|
|
* |
|
8
|
|
|
* This content is released under the GNU GPL License (GPL) |
|
9
|
|
|
* |
|
10
|
|
|
* Copyright (C) 2017 Tony NGUEREZA |
|
11
|
|
|
* |
|
12
|
|
|
* This program is free software; you can redistribute it and/or |
|
13
|
|
|
* modify it under the terms of the GNU General Public License |
|
14
|
|
|
* as published by the Free Software Foundation; either version 3 |
|
15
|
|
|
* of the License, or (at your option) any later version. |
|
16
|
|
|
* |
|
17
|
|
|
* This program is distributed in the hope that it will be useful, |
|
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20
|
|
|
* GNU General Public License for more details. |
|
21
|
|
|
* |
|
22
|
|
|
* You should have received a copy of the GNU General Public License |
|
23
|
|
|
* along with this program; if not, write to the Free Software |
|
24
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
25
|
|
|
*/ |
|
26
|
|
|
class DatabaseCache extends BaseClass { |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The cache time to live in second. 0 means no need to use the cache feature |
|
30
|
|
|
* @var int |
|
31
|
|
|
*/ |
|
32
|
|
|
private $cacheTtl = 0; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The cache instance |
|
36
|
|
|
* @var object |
|
37
|
|
|
*/ |
|
38
|
|
|
private $cacheInstance = null; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* The SQL query statment to get or save the result into cache |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
private $query = null; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* If the current query is the SELECT query |
|
48
|
|
|
* @var boolean |
|
49
|
|
|
*/ |
|
50
|
|
|
private $isSqlSELECTQuery = false; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* The status of the database cache |
|
54
|
|
|
* @var boolean |
|
55
|
|
|
*/ |
|
56
|
|
|
private $dbCacheStatus = false; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Indicate if we need return result as list (boolean) |
|
60
|
|
|
* or the data used to replace the placeholder (array) |
|
61
|
|
|
* @var array|boolean |
|
62
|
|
|
*/ |
|
63
|
|
|
private $returnAsList = true; |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Indicate if we need return result as array or not |
|
68
|
|
|
* @var boolean |
|
69
|
|
|
*/ |
|
70
|
|
|
private $returnAsArray = true; |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Construct new instance |
|
75
|
|
|
*/ |
|
76
|
|
|
public function __construct() { |
|
77
|
|
|
parent::__construct(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Return the current query SQL string |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getQuery() { |
|
85
|
|
|
return $this->query; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Set the query SQL string |
|
90
|
|
|
* @param string $query the SQL query to set |
|
91
|
|
|
* @return object DatabaseQueryRunner |
|
92
|
|
|
*/ |
|
93
|
|
|
public function setQuery($query) { |
|
94
|
|
|
$this->query = $query; |
|
95
|
|
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Set database cache time to live |
|
100
|
|
|
* @param integer $ttl the cache time to live in second |
|
101
|
|
|
* @return object the current Database instance |
|
102
|
|
|
*/ |
|
103
|
|
|
public function setCacheTtl($ttl = 0) { |
|
104
|
|
|
$this->cacheTtl = $ttl; |
|
105
|
|
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Return the cache instance |
|
110
|
|
|
* @return object CacheInterface |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getCacheInstance() { |
|
113
|
|
|
return $this->cacheInstance; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Set the cache instance |
|
118
|
|
|
* @param object CacheInterface $cache the cache object |
|
119
|
|
|
* @return object Database |
|
120
|
|
|
*/ |
|
121
|
|
|
public function setCacheInstance($cache) { |
|
122
|
|
|
$this->cacheInstance = $cache; |
|
123
|
|
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Set the query return type as list or not |
|
129
|
|
|
* @param boolean $returnType |
|
130
|
|
|
* @return object the current instance |
|
131
|
|
|
*/ |
|
132
|
|
|
public function setReturnType($returnType) { |
|
133
|
|
|
$this->returnAsList = $returnType; |
|
134
|
|
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Set the return as array or not |
|
139
|
|
|
* @param boolean $status the status if true will return as array |
|
140
|
|
|
* @return object the current instance |
|
141
|
|
|
*/ |
|
142
|
|
|
public function setReturnAsArray($status = true) { |
|
143
|
|
|
$this->returnAsArray = $status; |
|
144
|
|
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Get the cache content for this query |
|
150
|
|
|
* @see Database::query |
|
151
|
|
|
* |
|
152
|
|
|
* @return mixed |
|
153
|
|
|
*/ |
|
154
|
|
|
public function getCacheContent() { |
|
155
|
|
|
//set some attributes values |
|
156
|
|
|
$this->setPropertiesValues(); |
|
157
|
|
|
if(! $this->isSqlSELECTQuery || ! $this->dbCacheStatus){ |
|
158
|
|
|
$this->logger->info('The cache is not enabled for this query or is not a SELECT query'); |
|
159
|
|
|
return null; |
|
160
|
|
|
} |
|
161
|
|
|
$this->logger->info('The cache is enabled for this query, try to get result from cache'); |
|
162
|
|
|
$cacheKey = $this->getCacheKey(); |
|
163
|
|
|
if (!is_object($this->cacheInstance)) { |
|
164
|
|
|
//can not call method with reference in argument |
|
165
|
|
|
//like $this->setCacheInstance(& get_instance()->cache); |
|
166
|
|
|
//use temporary variable |
|
167
|
|
|
$instance = & get_instance()->cache; |
|
168
|
|
|
$this->cacheInstance = $instance; |
|
169
|
|
|
} |
|
170
|
|
|
return $this->cacheInstance->get($cacheKey); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Save the result of query into cache |
|
175
|
|
|
* @param string $key the cache key |
|
176
|
|
|
* @param mixed $result the query result to save |
|
177
|
|
|
* @param int $expire the cache TTL |
|
178
|
|
|
* |
|
179
|
|
|
* @return boolean the status of the operation |
|
180
|
|
|
*/ |
|
181
|
|
|
public function saveCacheContent($result) { |
|
182
|
|
|
//set some attributes values |
|
183
|
|
|
$this->setPropertiesValues(); |
|
184
|
|
|
if(! $this->isSqlSELECTQuery || ! $this->dbCacheStatus){ |
|
185
|
|
|
//just return true |
|
186
|
|
|
return true; |
|
187
|
|
|
} |
|
188
|
|
|
$cacheKey = $this->getCacheKey(); |
|
189
|
|
|
$this->logger->info('Save the result for query [' . $this->query . '] into cache for future use'); |
|
190
|
|
|
if (!is_object($this->cacheInstance)) { |
|
191
|
|
|
//can not call method with reference in argument |
|
192
|
|
|
//like $this->setCacheInstance(& get_instance()->cache); |
|
193
|
|
|
//use temporary variable |
|
194
|
|
|
$instance = & get_instance()->cache; |
|
195
|
|
|
$this->cacheInstance = $instance; |
|
196
|
|
|
} |
|
197
|
|
|
return $this->cacheInstance->set($cacheKey, $result, $this->cacheTtl); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Set some properties values to use later |
|
202
|
|
|
*/ |
|
203
|
|
|
protected function setPropertiesValues() { |
|
204
|
|
|
//If is the SELECT query |
|
205
|
|
|
$this->isSqlSELECTQuery = stristr($this->query, 'SELECT') !== false; |
|
206
|
|
|
|
|
207
|
|
|
//if can use cache feature for this query |
|
208
|
|
|
$this->dbCacheStatus = get_config('cache_enable', false) && $this->cacheTtl > 0; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Return the cache key for the current query |
|
213
|
|
|
* @see Database::query |
|
214
|
|
|
* |
|
215
|
|
|
* @return string |
|
216
|
|
|
*/ |
|
217
|
|
|
protected function getCacheKey() { |
|
218
|
|
|
return md5($this->query . $this->returnAsList . $this->returnAsArray); |
|
|
|
|
|
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
} |
|
222
|
|
|
|