2 namespace TYPO3\CMS\Core\Cache\Backend;
57 $this->dataSourceName = $DSN;
107 public function set($entryIdentifier, $data, array $tags = array(), $lifetime = null)
109 if (!$this->cache instanceof \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface) {
110 throw new \TYPO3\CMS\Core\Cache\Exception(
'No cache frontend has been set yet via setCache().', 1259515600);
112 if (!is_string($data)) {
113 throw new \TYPO3\CMS\Core\Cache\Exception\InvalidDataException(
'The specified data is of type "' . gettype($data) .
'" but a string is expected.', 1259515601);
115 $this->
remove($entryIdentifier);
116 $lifetime = $lifetime === null ? $this->defaultLifetime : $lifetime;
117 $statementHandle = $this->databaseHandle->prepare(
'INSERT INTO "cache" ("identifier", "context", "cache", "created", "lifetime", "content") VALUES (?, ?, ?, ?, ?, ?)');
118 $result = $statementHandle->execute(array($entryIdentifier, $this->context, $this->cacheIdentifier,
$GLOBALS[
'EXEC_TIME'], $lifetime, $data));
119 if ($result ===
false) {
120 throw new \TYPO3\CMS\Core\Cache\Exception(
'The cache entry "' . $entryIdentifier .
'" could not be written.', 1259530791);
122 $statementHandle = $this->databaseHandle->prepare(
'INSERT INTO "tags" ("identifier", "context", "cache", "tag") VALUES (?, ?, ?, ?)');
123 foreach ($tags as $tag) {
124 $result = $statementHandle->execute(array($entryIdentifier, $this->context, $this->cacheIdentifier, $tag));
125 if ($result ===
false) {
126 throw new \TYPO3\CMS\Core\Cache\Exception(
'The tag "' . $tag .
' for cache entry "' . $entryIdentifier .
'" could not be written.', 1259530751);
138 public function get($entryIdentifier)
140 $statementHandle = $this->databaseHandle->prepare(
'SELECT "content" FROM "cache" WHERE "identifier"=? AND "context"=? AND "cache"=?' . $this->
getNotExpiredStatement());
141 $statementHandle->execute(array($entryIdentifier, $this->context, $this->cacheIdentifier));
142 return $statementHandle->fetchColumn();
152 public function has($entryIdentifier)
154 $statementHandle = $this->databaseHandle->prepare(
'SELECT COUNT("identifier") FROM "cache" WHERE "identifier"=? AND "context"=? AND "cache"=?' . $this->
getNotExpiredStatement());
155 $statementHandle->execute(array($entryIdentifier, $this->context, $this->cacheIdentifier));
156 return $statementHandle->fetchColumn() > 0;
168 public function remove($entryIdentifier)
170 $statementHandle = $this->databaseHandle->prepare(
'DELETE FROM "tags" WHERE "identifier"=? AND "context"=? AND "cache"=?');
171 $statementHandle->execute(array($entryIdentifier, $this->context, $this->cacheIdentifier));
172 $statementHandle = $this->databaseHandle->prepare(
'DELETE FROM "cache" WHERE "identifier"=? AND "context"=? AND "cache"=?');
173 $statementHandle->execute(array($entryIdentifier, $this->context, $this->cacheIdentifier));
174 return $statementHandle->rowCount() > 0;
185 $statementHandle = $this->databaseHandle->prepare(
'DELETE FROM "tags" WHERE "context"=? AND "cache"=?');
186 $statementHandle->execute(array($this->context, $this->cacheIdentifier));
187 $statementHandle = $this->databaseHandle->prepare(
'DELETE FROM "cache" WHERE "context"=? AND "cache"=?');
188 $statementHandle->execute(array($this->context, $this->cacheIdentifier));
200 $statementHandle = $this->databaseHandle->prepare(
'DELETE FROM "cache" WHERE "context"=? AND "cache"=? AND "identifier" IN (SELECT "identifier" FROM "tags" WHERE "context"=? AND "cache"=? AND "tag"=?)');
201 $statementHandle->execute(array($this->context, $this->cacheIdentifier, $this->context, $this->cacheIdentifier, $tag));
202 $statementHandle = $this->databaseHandle->prepare(
'DELETE FROM "tags" WHERE "context"=? AND "cache"=? AND "tag"=?');
203 $statementHandle->execute(array($this->context, $this->cacheIdentifier, $tag));
216 $statementHandle = $this->databaseHandle->prepare(
'SELECT "identifier" FROM "tags" WHERE "context"=? AND "cache"=? AND "tag"=?');
217 $statementHandle->execute(array($this->context, $this->cacheIdentifier, $tag));
218 return $statementHandle->fetchAll(\PDO::FETCH_COLUMN);
229 $statementHandle = $this->databaseHandle->prepare(
'DELETE FROM "tags" WHERE "context"=? AND "cache"=? AND "identifier" IN ' .
'(SELECT "identifier" FROM "cache" WHERE "context"=? AND "cache"=? AND "lifetime" > 0 AND "created" + "lifetime" < ' .
$GLOBALS[
'EXEC_TIME'] .
')');
230 $statementHandle->execute(array($this->context, $this->cacheIdentifier, $this->context, $this->cacheIdentifier));
231 $statementHandle = $this->databaseHandle->prepare(
'DELETE FROM "cache" WHERE "context"=? AND "cache"=? AND "lifetime" > 0 AND "created" + "lifetime" < ' .
$GLOBALS[
'EXEC_TIME']);
232 $statementHandle->execute(array($this->context, $this->cacheIdentifier));
242 return ' AND ("lifetime" = 0 OR "created" + "lifetime" >= ' .
$GLOBALS[
'EXEC_TIME'] .
')';
254 $splitdsn = explode(
':', $this->dataSourceName, 2);
255 $this->pdoDriver = $splitdsn[0];
256 if ($this->pdoDriver ===
'sqlite' && !file_exists($splitdsn[1])) {
257 $this->databaseHandle = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\PDO::class, $this->dataSourceName, $this->username, $this->password);
260 $this->databaseHandle = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\PDO::class, $this->dataSourceName, $this->username, $this->password);
262 $this->databaseHandle->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
263 if (substr($this->pdoDriver, 0, 5) ===
'mysql') {
264 $this->databaseHandle->exec(
'SET SESSION sql_mode=\'ANSI\';');
266 }
catch (\PDOException $e) {
267 throw new \RuntimeException(
'Could not connect to cache table with DSN "' . $this->dataSourceName .
'". PDO error: ' . $e->getMessage(), 1334736164);
280 \TYPO3\CMS\Core\Database\PdoHelper::importSql(
281 $this->databaseHandle,
283 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath(
'core') .
284 'Resources/Private/Sql/Cache/Backend/PdoBackendCacheAndTags.sql'
286 }
catch (\PDOException $e) {
287 throw new \RuntimeException(
'Could not create cache tables with DSN "' . $this->dataSourceName .
'". PDO error: ' . $e->getMessage(), 1259576985);