1 | <?php |
||
21 | class DB |
||
22 | extends \Aimeos\MW\Cache\Base |
||
|
|||
23 | implements \Aimeos\MW\Cache\Iface |
||
24 | { |
||
25 | private $sql; |
||
26 | private $dbm; |
||
27 | private $dbname; |
||
28 | private $siteid; |
||
29 | private $searchConfig; |
||
30 | |||
31 | |||
32 | /** |
||
33 | * Initializes the object instance. |
||
34 | * |
||
35 | * The config['search] array must contain these key/array pairs suitable for \Aimeos\MW\Criteria\Attribute\Standard: |
||
36 | * [cache.id] => Array containing the codes/types/labels for the unique ID |
||
37 | * [cache.siteid] => Array containing the codes/types/labels for the site ID |
||
38 | * [cache.value] => Array containing the codes/types/labels for the cached value |
||
39 | * [cache.expire] => Array containing the codes/types/labels for the expiration date |
||
40 | * [cache.tag.name] => Array containing the codes/types/labels for the tag name |
||
41 | * |
||
42 | * The config['sql] array must contain these statement: |
||
43 | * [delete] => |
||
44 | * DELETE FROM cachetable WHERE siteid = ? AND :cond |
||
45 | * [deletebytag] => |
||
46 | * DELETE FROM cachetable WHERE siteid = ? AND id IN ( |
||
47 | * SELECT tid FROM cachetagtable WHERE tsiteid = ? AND :cond |
||
48 | * ) |
||
49 | * [get] => |
||
50 | * SELECT id, value, expire FROM cachetable WHERE siteid = ? AND :cond |
||
51 | * [getbytag] => |
||
52 | * SELECT id, value, expire FROM cachetable |
||
53 | * JOIN cachetagtable ON tid = id |
||
54 | * WHERE siteid = ? AND tsiteid = ? AND :cond |
||
55 | * [set] => |
||
56 | * INSERT INTO cachetable ( id, siteid, expire, value ) VALUES ( ?, ?, ?, ? ) |
||
57 | * [settag] => |
||
58 | * INSERT INTO cachetagtable ( tid, tsiteid, tname ) VALUES ( ?, ?, ? ) |
||
59 | * |
||
60 | * For using a different database connection, the name of the database connection |
||
61 | * can be also given in the "config" parameter. In this case, use e.g. |
||
62 | * config['dbname'] = 'db-cache' |
||
63 | * |
||
64 | * If a site ID is given, the cache is partitioned for different |
||
65 | * sites. This also includes access control so cached values can be only |
||
66 | * retrieved from the same site. Specify a site ID with |
||
67 | * config['siteid'] = 123 |
||
68 | * |
||
69 | * @param array $config Associative list with SQL statements, search attribute definitions and database name |
||
70 | * @param \Aimeos\MW\DB\Manager\Iface $dbm Database manager |
||
71 | */ |
||
72 | public function __construct( array $config, \Aimeos\MW\DB\Manager\Iface $dbm ) |
||
91 | |||
92 | |||
93 | /** |
||
94 | * Removes all expired cache entries. |
||
95 | * |
||
96 | * @throws \Aimeos\MW\Cache\Exception If the cache server doesn't respond |
||
97 | */ |
||
98 | public function cleanup() |
||
124 | |||
125 | |||
126 | /** |
||
127 | * Removes the cache entries identified by the given keys. |
||
128 | * |
||
129 | * @param iterable $keys List of key strings that identify the cache entries |
||
130 | * that should be removed |
||
131 | * @throws \Aimeos\MW\Cache\Exception If the cache server doesn't respond |
||
132 | */ |
||
133 | public function deleteMultiple( $keys ) |
||
134 | { |
||
135 | $conn = $this->dbm->acquire( $this->dbname ); |
||
136 | |||
137 | try |
||
138 | { |
||
139 | $search = new \Aimeos\MW\Criteria\SQL( $conn ); |
||
140 | $search->setConditions( $search->compare( '==', $this->searchConfig['cache.id']['code'], $keys ) ); |
||
141 | |||
142 | $types = $this->getSearchTypes( $this->searchConfig ); |
||
143 | $translations = $this->getSearchTranslations( $this->searchConfig ); |
||
144 | $conditions = $search->getConditionString( $types, $translations ); |
||
145 | |||
146 | $stmt = $conn->create( str_replace( ':cond', $conditions, $this->sql['delete'] ) ); |
||
147 | $stmt->bind( 1, $this->siteid, \Aimeos\MW\DB\Statement\Base::PARAM_INT ); |
||
148 | $stmt->execute()->finish(); |
||
149 | |||
150 | $this->dbm->release( $conn, $this->dbname ); |
||
151 | } |
||
152 | catch( \Exception $e ) |
||
153 | { |
||
154 | $this->dbm->release( $conn, $this->dbname ); |
||
155 | throw $e; |
||
156 | } |
||
157 | } |
||
158 | |||
159 | |||
160 | /** |
||
161 | * Removes the cache entries identified by the given tags. |
||
162 | * |
||
163 | * @param string[] $tags List of tag strings that are associated to one or more |
||
164 | * cache entries that should be removed |
||
165 | * @throws \Aimeos\MW\Cache\Exception If the cache server doesn't respond |
||
166 | */ |
||
167 | public function deleteByTags( array $tags ) |
||
193 | |||
194 | |||
195 | /** |
||
196 | * Removes all entries of the site from the cache. |
||
197 | * |
||
198 | * @throws \Aimeos\MW\Cache\Exception If the cache server doesn't respond |
||
199 | */ |
||
200 | public function clear() |
||
218 | |||
219 | |||
220 | /** |
||
221 | * Returns the cached values for the given cache keys if available. |
||
222 | * |
||
223 | * @param iterable $keys List of key strings for the requested cache entries |
||
224 | * @param mixed $default Default value to return for keys that do not exist |
||
225 | * @return array Associative list of key/value pairs for the requested cache |
||
226 | * entries. If a cache entry doesn't exist, neither its key nor a value |
||
227 | * will be in the result list |
||
228 | * @throws \Aimeos\MW\Cache\Exception If the cache server doesn't respond |
||
229 | */ |
||
230 | public function getMultiple( $keys, $default = null ) |
||
277 | |||
278 | |||
279 | /** |
||
280 | * Returns the cached keys and values associated to the given tags if available. |
||
281 | * |
||
282 | * @param string[] $tags List of tag strings associated to the requested cache entries |
||
283 | * @return array Associative list of key/value pairs for the requested cache |
||
284 | * entries. If a tag isn't associated to any cache entry, nothing is returned |
||
285 | * for that tag |
||
286 | * @throws \Aimeos\MW\Cache\Exception If the cache server doesn't respond |
||
287 | */ |
||
288 | public function getMultipleByTags( array $tags ) |
||
329 | |||
330 | |||
331 | /** |
||
332 | * Adds or overwrites the given key/value pairs in the cache, which is much |
||
333 | * more efficient than setting them one by one using the set() method. |
||
334 | * |
||
335 | * @param iterable $pairs Associative list of key/value pairs. Both must be |
||
336 | * a string |
||
337 | * @param int|string|array $expires Associative list of keys and datetime |
||
338 | * string or integer TTL pairs. |
||
339 | * @param array $tags Associative list of key/tag or key/tags pairs that |
||
340 | * should be associated to the values identified by their key. The value |
||
341 | * associated to the key can either be a tag string or an array of tag strings |
||
342 | * @return null |
||
343 | * @throws \Aimeos\MW\Cache\Exception If the cache server doesn't respond |
||
344 | */ |
||
345 | public function setMultiple( $pairs, $expires = null, array $tags = array() ) |
||
391 | |||
392 | |||
393 | /** |
||
394 | * Checks if all required search configurations are available. |
||
395 | * |
||
396 | * @param array $config Associative list of search configurations |
||
397 | * @throws \Aimeos\MW\Tree\Exception If one ore more search configurations are missing |
||
398 | */ |
||
399 | protected function checkSearchConfig( array $config ) |
||
416 | |||
417 | |||
418 | /** |
||
419 | * Checks if all required SQL statements are available. |
||
420 | * |
||
421 | * @param array $config Associative list of SQL statements |
||
422 | * @throws \Aimeos\MW\Tree\Exception If one ore more SQL statements are missing |
||
423 | */ |
||
424 | protected function checkSqlConfig( array $config ) |
||
441 | } |
||
442 |