/
Typo3DatabaseBackend.php
539 lines (503 loc) · 21 KB
/
Typo3DatabaseBackend.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
<?php
namespace TYPO3\CMS\Core\Cache\Backend;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use Doctrine\DBAL\FetchMode;
use TYPO3\CMS\Core\Cache\Exception;
use TYPO3\CMS\Core\Cache\Exception\InvalidDataException;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* A caching backend which stores cache entries in database tables
*/
class Typo3DatabaseBackend extends AbstractBackend implements TaggableBackendInterface
{
/**
* @var int Timestamp of 2038-01-01)
*/
const FAKED_UNLIMITED_EXPIRE = 2145909600;
/**
* @var string Name of the cache data table
*/
protected $cacheTable;
/**
* @var string Name of the cache tags table
*/
protected $tagsTable;
/**
* @var bool Indicates whether data is compressed or not (requires php zlib)
*/
protected $compression = false;
/**
* @var int -1 to 9, indicates zlib compression level: -1 = default level 6, 0 = no compression, 9 maximum compression
*/
protected $compressionLevel = -1;
/**
* @var int Maximum lifetime to stay with expire field below FAKED_UNLIMITED_LIFETIME
*/
protected $maximumLifetime;
/**
* Set cache frontend instance and calculate data and tags table name
*
* @param FrontendInterface $cache The frontend for this backend
*/
public function setCache(FrontendInterface $cache)
{
parent::setCache($cache);
$this->cacheTable = 'cache_' . $this->cacheIdentifier;
$this->tagsTable = 'cache_' . $this->cacheIdentifier . '_tags';
$this->maximumLifetime = self::FAKED_UNLIMITED_EXPIRE - $GLOBALS['EXEC_TIME'];
}
/**
* Saves data in a cache file.
*
* @param string $entryIdentifier An identifier for this specific cache entry
* @param string $data The data to be stored
* @param array $tags Tags to associate with this cache entry
* @param int $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited lifetime.
* @throws Exception if no cache frontend has been set.
* @throws InvalidDataException if the data to be stored is not a string.
*/
public function set($entryIdentifier, $data, array $tags = [], $lifetime = null)
{
$this->throwExceptionIfFrontendDoesNotExist();
if (!is_string($data)) {
throw new InvalidDataException(
'The specified data is of type "' . gettype($data) . '" but a string is expected.',
1236518298
);
}
if ($lifetime === null) {
$lifetime = $this->defaultLifetime;
}
if ($lifetime === 0 || $lifetime > $this->maximumLifetime) {
$lifetime = $this->maximumLifetime;
}
$expires = $GLOBALS['EXEC_TIME'] + $lifetime;
$this->remove($entryIdentifier);
if ($this->compression) {
$data = gzcompress($data, $this->compressionLevel);
}
GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable($this->cacheTable)
->insert(
$this->cacheTable,
[
'identifier' => $entryIdentifier,
'expires' => $expires,
'content' => $data,
],
[
'content' => Connection::PARAM_LOB,
]
);
if (!empty($tags)) {
$tagRows = [];
foreach ($tags as $tag) {
$tagRows[] = [$entryIdentifier, $tag];
}
GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable($this->tagsTable)
->bulkInsert($this->tagsTable, $tagRows, ['identifier', 'tag']);
}
}
/**
* Loads data from a cache file.
*
* @param string $entryIdentifier An identifier which describes the cache entry to load
* @return mixed The cache entry's data as a string or FALSE if the cache entry could not be loaded
*/
public function get($entryIdentifier)
{
$this->throwExceptionIfFrontendDoesNotExist();
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable($this->cacheTable);
$cacheRow = $queryBuilder->select('content')
->from($this->cacheTable)
->where(
$queryBuilder->expr()->eq(
'identifier',
$queryBuilder->createNamedParameter($entryIdentifier, \PDO::PARAM_STR)
),
$queryBuilder->expr()->gte(
'expires',
$queryBuilder->createNamedParameter($GLOBALS['EXEC_TIME'], \PDO::PARAM_INT)
)
)
->execute()
->fetch();
$content = '';
if (!empty($cacheRow)) {
$content = $cacheRow['content'];
}
if ($this->compression && (string)$content !== '') {
$content = gzuncompress($content);
}
return !empty($cacheRow) ? $content : false;
}
/**
* Checks if a cache entry with the specified identifier exists.
*
* @param string $entryIdentifier Specifies the identifier to check for existence
* @return bool TRUE if such an entry exists, FALSE if not
*/
public function has($entryIdentifier)
{
$this->throwExceptionIfFrontendDoesNotExist();
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable($this->cacheTable);
$count = $queryBuilder->count('*')
->from($this->cacheTable)
->where(
$queryBuilder->expr()->eq(
'identifier',
$queryBuilder->createNamedParameter($entryIdentifier, \PDO::PARAM_STR)
),
$queryBuilder->expr()->gte(
'expires',
$queryBuilder->createNamedParameter($GLOBALS['EXEC_TIME'], \PDO::PARAM_INT)
)
)
->execute()
->fetchColumn(0);
return (bool)$count;
}
/**
* Removes all cache entries matching the specified identifier.
* Usually this only affects one entry.
*
* @param string $entryIdentifier Specifies the cache entry to remove
* @return bool TRUE if (at least) an entry could be removed or FALSE if no entry was found
*/
public function remove($entryIdentifier)
{
$this->throwExceptionIfFrontendDoesNotExist();
$numberOfRowsRemoved = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable($this->cacheTable)
->delete(
$this->cacheTable,
['identifier' => $entryIdentifier]
);
GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable($this->tagsTable)
->delete(
$this->tagsTable,
['identifier' => $entryIdentifier]
);
return (bool)$numberOfRowsRemoved;
}
/**
* Finds and returns all cache entries which are tagged by the specified tag.
*
* @param string $tag The tag to search for
* @return array An array with identifiers of all matching entries. An empty array if no entries matched
*/
public function findIdentifiersByTag($tag)
{
$this->throwExceptionIfFrontendDoesNotExist();
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable($this->tagsTable);
$result = $queryBuilder->select($this->cacheTable . '.identifier')
->from($this->cacheTable)
->from($this->tagsTable)
->where(
$queryBuilder->expr()->eq($this->cacheTable . '.identifier', $queryBuilder->quoteIdentifier($this->tagsTable . '.identifier')),
$queryBuilder->expr()->eq(
$this->tagsTable . '.tag',
$queryBuilder->createNamedParameter($tag, \PDO::PARAM_STR)
),
$queryBuilder->expr()->gte(
$this->cacheTable . '.expires',
$queryBuilder->createNamedParameter($GLOBALS['EXEC_TIME'], \PDO::PARAM_INT)
)
)
->groupBy($this->cacheTable . '.identifier')
->execute();
$identifiers = $result->fetchAll(FetchMode::COLUMN, 0);
return array_combine($identifiers, $identifiers);
}
/**
* Removes all cache entries of this cache.
*/
public function flush()
{
$this->throwExceptionIfFrontendDoesNotExist();
GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable($this->cacheTable)
->truncate($this->cacheTable);
GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable($this->tagsTable)
->truncate($this->tagsTable);
}
/**
* Removes all entries tagged by any of the specified tags. Performs the SQL
* operation as a bulk query for better performance.
*
* @param string[] $tags
*/
public function flushByTags(array $tags)
{
$this->throwExceptionIfFrontendDoesNotExist();
if (empty($tags)) {
return;
}
/** @var Connection $connection */
$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->cacheTable);
// A large set of tags was detected. Process it in chunks to guard against exceeding
// maximum SQL query limits.
if (count($tags) > 100) {
array_walk(array_chunk($tags, 100), [$this, 'flushByTags']);
return;
}
// VERY simple quoting of tags is sufficient here for performance. Tags are already
// validated to not contain any bad characters, e.g. they are automatically generated
// inside this class and suffixed with a pure integer enforced by DB.
$quotedTagList = array_map(function ($value) {
return '\'' . $value . '\'';
}, $tags);
if ($this->isConnectionMysql($connection)) {
// Use an optimized query on mysql ... don't use on your own
// * ansi sql does not know about multi table delete
// * doctrine query builder does not support join on delete()
$connection->executeQuery(
'DELETE tags2, cache1'
. ' FROM ' . $this->tagsTable . ' AS tags1'
. ' JOIN ' . $this->tagsTable . ' AS tags2 ON tags1.identifier = tags2.identifier'
. ' JOIN ' . $this->cacheTable . ' AS cache1 ON tags1.identifier = cache1.identifier'
. ' WHERE tags1.tag IN (' . implode(',', $quotedTagList) . ')'
);
} else {
$queryBuilder = $connection->createQueryBuilder();
$result = $queryBuilder->select('identifier')
->from($this->tagsTable)
->where('tag IN (' . implode(',', $quotedTagList) . ')')
// group by is like DISTINCT and used here to suppress possible duplicate identifiers
->groupBy('identifier')
->execute();
$cacheEntryIdentifiers = $result->fetchAll(FetchMode::COLUMN, 0);
$quotedIdentifiers = $queryBuilder->createNamedParameter($cacheEntryIdentifiers, Connection::PARAM_STR_ARRAY);
$queryBuilder->delete($this->cacheTable)
->where($queryBuilder->expr()->in('identifier', $quotedIdentifiers))
->execute();
$queryBuilder->delete($this->tagsTable)
->where($queryBuilder->expr()->in('identifier', $quotedIdentifiers))
->execute();
}
}
/**
* Removes all cache entries of this cache which are tagged by the specified tag.
*
* @param string $tag The tag the entries must have
*/
public function flushByTag($tag)
{
$this->throwExceptionIfFrontendDoesNotExist();
if (empty($tag)) {
return;
}
/** @var Connection $connection */
$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->cacheTable);
$quotedTag = '\'' . $tag . '\'';
if ($this->isConnectionMysql($connection)) {
// Use an optimized query on mysql ... don't use on your own
// * ansi sql does not know about multi table delete
// * doctrine query builder does not support join on delete()
$connection->executeQuery(
'DELETE tags2, cache1'
. ' FROM ' . $this->tagsTable . ' AS tags1'
. ' JOIN ' . $this->tagsTable . ' AS tags2 ON tags1.identifier = tags2.identifier'
. ' JOIN ' . $this->cacheTable . ' AS cache1 ON tags1.identifier = cache1.identifier'
. ' WHERE tags1.tag = ' . $quotedTag
);
} else {
$queryBuilder = $connection->createQueryBuilder();
$result = $queryBuilder->select('identifier')
->from($this->tagsTable)
->where('tag = ' . $quotedTag)
// group by is like DISTINCT and used here to suppress possible duplicate identifiers
->groupBy('identifier')
->execute();
$cacheEntryIdentifiers = $result->fetchAll(FetchMode::COLUMN, 0);
$quotedIdentifiers = $queryBuilder->createNamedParameter($cacheEntryIdentifiers, Connection::PARAM_STR_ARRAY);
$queryBuilder->delete($this->cacheTable)
->where($queryBuilder->expr()->in('identifier', $quotedIdentifiers))
->execute();
$queryBuilder->delete($this->tagsTable)
->where($queryBuilder->expr()->in('identifier', $quotedIdentifiers))
->execute();
}
}
/**
* Does garbage collection
*/
public function collectGarbage()
{
$this->throwExceptionIfFrontendDoesNotExist();
$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->cacheTable);
if ($this->isConnectionMysql($connection)) {
// Use an optimized query on mysql ... don't use on your own
// * ansi sql does not know about multi table delete
// * doctrine query builder does not support join on delete()
// First delete all expired rows from cache table and their connected tag rows
$connection->executeQuery(
'DELETE cache, tags'
. ' FROM ' . $this->cacheTable . ' AS cache'
. ' LEFT OUTER JOIN ' . $this->tagsTable . ' AS tags ON cache.identifier = tags.identifier'
. ' WHERE cache.expires < ?',
[(int)$GLOBALS['EXEC_TIME']]
);
// Then delete possible "orphaned" rows from tags table - tags that have no cache row for whatever reason
$connection->executeQuery(
'DELETE tags'
. ' FROM ' . $this->tagsTable . ' AS tags'
. ' LEFT OUTER JOIN ' . $this->cacheTable . ' as cache ON tags.identifier = cache.identifier'
. ' WHERE cache.identifier IS NULL'
);
} else {
$queryBuilder = $connection->createQueryBuilder();
$result = $queryBuilder->select('identifier')
->from($this->cacheTable)
->where($queryBuilder->expr()->lt(
'expires',
$queryBuilder->createNamedParameter($GLOBALS['EXEC_TIME'], \PDO::PARAM_INT)
))
// group by is like DISTINCT and used here to suppress possible duplicate identifiers
->groupBy('identifier')
->execute();
// Get identifiers of expired cache entries
$cacheEntryIdentifiers = $result->fetchAll(FetchMode::COLUMN, 0);
if (!empty($cacheEntryIdentifiers)) {
// Delete tag rows connected to expired cache entries
$quotedIdentifiers = $queryBuilder->createNamedParameter($cacheEntryIdentifiers, Connection::PARAM_STR_ARRAY);
$queryBuilder->delete($this->tagsTable)
->where($queryBuilder->expr()->in('identifier', $quotedIdentifiers))
->execute();
}
$queryBuilder->delete($this->cacheTable)
->where($queryBuilder->expr()->lt(
'expires',
$queryBuilder->createNamedParameter($GLOBALS['EXEC_TIME'], \PDO::PARAM_INT)
))
->execute();
// Find out which "orphaned" tags rows exists that have no cache row and delete those, too.
$queryBuilder = $connection->createQueryBuilder();
$result = $queryBuilder->select('tags.identifier')
->from($this->tagsTable, 'tags')
->leftJoin(
'tags',
$this->cacheTable,
'cache',
$queryBuilder->expr()->eq('tags.identifier', $queryBuilder->quoteIdentifier('cache.identifier'))
)
->where($queryBuilder->expr()->isNull('cache.identifier'))
->groupBy('tags.identifier')
->execute();
$tagsEntryIdentifiers = $result->fetchAll(FetchMode::COLUMN, 0);
if (!empty($tagsEntryIdentifiers)) {
$quotedIdentifiers = $queryBuilder->createNamedParameter($tagsEntryIdentifiers, Connection::PARAM_STR_ARRAY);
$queryBuilder->delete($this->tagsTable)
->where($queryBuilder->expr()->in('identifier', $quotedIdentifiers))
->execute();
}
}
}
/**
* Returns the table where the cache entries are stored.
*
* @return string The cache table.
*/
public function getCacheTable()
{
$this->throwExceptionIfFrontendDoesNotExist();
return $this->cacheTable;
}
/**
* Gets the table where cache tags are stored.
*
* @return string Name of the table storing tags
*/
public function getTagsTable()
{
$this->throwExceptionIfFrontendDoesNotExist();
return $this->tagsTable;
}
/**
* Enable data compression
*
* @param bool $compression TRUE to enable compression
*/
public function setCompression($compression)
{
$this->compression = $compression;
}
/**
* Set data compression level.
* If compression is enabled and this is not set,
* gzcompress default level will be used
*
* @param int $compressionLevel -1 to 9: Compression level
*/
public function setCompressionLevel($compressionLevel)
{
if ($compressionLevel >= -1 && $compressionLevel <= 9) {
$this->compressionLevel = $compressionLevel;
}
}
/**
* This database backend uses some optimized queries for mysql
* to get maximum performance.
*
* @param Connection $connection
* @return bool
*/
protected function isConnectionMysql(Connection $connection): bool
{
$serverVersion = $connection->getServerVersion();
return (bool)(strpos($serverVersion, 'MySQL') === 0);
}
/**
* Check if required frontend instance exists
*
* @throws Exception If there is no frontend instance in $this->cache
*/
protected function throwExceptionIfFrontendDoesNotExist()
{
if (!$this->cache instanceof FrontendInterface) {
throw new Exception('No cache frontend has been set via setCache() yet.', 1236518288);
}
}
/**
* Calculate needed table definitions for this cache.
* This helper method is used by install tool and extension manager
* and is not part of the public API!
*
* @return string SQL of table definitions
*/
public function getTableDefinitions()
{
$cacheTableSql = file_get_contents(
ExtensionManagementUtility::extPath('core') .
'Resources/Private/Sql/Cache/Backend/Typo3DatabaseBackendCache.sql'
);
$requiredTableStructures = str_replace('###CACHE_TABLE###', $this->cacheTable, $cacheTableSql) . LF . LF;
$tagsTableSql = file_get_contents(
ExtensionManagementUtility::extPath('core') .
'Resources/Private/Sql/Cache/Backend/Typo3DatabaseBackendTags.sql'
);
$requiredTableStructures .= str_replace('###TAGS_TABLE###', $this->tagsTable, $tagsTableSql) . LF;
return $requiredTableStructures;
}
}