fcf713266a85151887599874603d5ed45f9e35d0
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Cache / Backend / PdoBackend.php
1 <?php
2 namespace TYPO3\CMS\Core\Cache\Backend;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2011 Christian Kuhn <lolli@schwarzbu.ch>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * A PDO database cache backend
28 *
29 * @author Christian Kuhn <lolli@schwarzbu.ch>
30 * @author Karsten Dambekalns <karsten@typo3.org>
31 * @api
32 */
33 class PdoBackend extends \TYPO3\CMS\Core\Cache\Backend\AbstractBackend implements \TYPO3\CMS\Core\Cache\Backend\TaggableBackendInterface {
34
35 /**
36 * @var string
37 */
38 protected $dataSourceName;
39
40 /**
41 * @var string
42 */
43 protected $username;
44
45 /**
46 * @var string
47 */
48 protected $password;
49
50 /**
51 * @var \PDO
52 */
53 protected $databaseHandle;
54
55 /**
56 * @var string
57 */
58 protected $pdoDriver;
59
60 /**
61 * Sets the DSN to use
62 *
63 * @param string $DSN The DSN to use for connecting to the DB
64 * @return void
65 * @api
66 */
67 public function setDataSourceName($DSN) {
68 $this->dataSourceName = $DSN;
69 }
70
71 /**
72 * Sets the username to use
73 *
74 * @param string $username The username to use for connecting to the DB
75 * @return void
76 * @api
77 */
78 public function setUsername($username) {
79 $this->username = $username;
80 }
81
82 /**
83 * Sets the password to use
84 *
85 * @param string $password The password to use for connecting to the DB
86 * @return void
87 * @api
88 */
89 public function setPassword($password) {
90 $this->password = $password;
91 }
92
93 /**
94 * Initialize the cache backend.
95 *
96 * @return void
97 */
98 public function initializeObject() {
99 $this->connect();
100 }
101
102 /**
103 * Saves data in the cache.
104 *
105 * @param string $entryIdentifier An identifier for this specific cache entry
106 * @param string $data The data to be stored
107 * @param array $tags Tags to associate with this cache entry
108 * @param integer $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited liftime.
109 * @return void
110 * @throws \TYPO3\CMS\Core\Cache\Exception if no cache frontend has been set.
111 * @throws \InvalidArgumentException if the identifier is not valid
112 * @throws \TYPO3\CMS\Core\Cache\Exception\InvalidDataException if $data is not a string
113 * @api
114 */
115 public function set($entryIdentifier, $data, array $tags = array(), $lifetime = NULL) {
116 if (!$this->cache instanceof \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface) {
117 throw new \TYPO3\CMS\Core\Cache\Exception('No cache frontend has been set yet via setCache().', 1259515600);
118 }
119 if (!is_string($data)) {
120 throw new \TYPO3\CMS\Core\Cache\Exception\InvalidDataException('The specified data is of type "' . gettype($data) . '" but a string is expected.', 1259515601);
121 }
122 $this->remove($entryIdentifier);
123 $lifetime = $lifetime === NULL ? $this->defaultLifetime : $lifetime;
124 $statementHandle = $this->databaseHandle->prepare('INSERT INTO "cache" ("identifier", "context", "cache", "created", "lifetime", "content") VALUES (?, ?, ?, ?, ?, ?)');
125 $result = $statementHandle->execute(array($entryIdentifier, $this->context, $this->cacheIdentifier, $GLOBALS['EXEC_TIME'], $lifetime, $data));
126 if ($result === FALSE) {
127 throw new \TYPO3\CMS\Core\Cache\Exception('The cache entry "' . $entryIdentifier . '" could not be written.', 1259530791);
128 }
129 $statementHandle = $this->databaseHandle->prepare('INSERT INTO "tags" ("identifier", "context", "cache", "tag") VALUES (?, ?, ?, ?)');
130 foreach ($tags as $tag) {
131 $result = $statementHandle->execute(array($entryIdentifier, $this->context, $this->cacheIdentifier, $tag));
132 if ($result === FALSE) {
133 throw new \TYPO3\CMS\Core\Cache\Exception('The tag "' . $tag . ' for cache entry "' . $entryIdentifier . '" could not be written.', 1259530751);
134 }
135 }
136 }
137
138 /**
139 * Loads data from the cache.
140 *
141 * @param string $entryIdentifier An identifier which describes the cache entry to load
142 * @return mixed The cache entry's content as a string or FALSE if the cache entry could not be loaded
143 * @api
144 */
145 public function get($entryIdentifier) {
146 $statementHandle = $this->databaseHandle->prepare('SELECT "content" FROM "cache" WHERE "identifier"=? AND "context"=? AND "cache"=?' . $this->getNotExpiredStatement());
147 $statementHandle->execute(array($entryIdentifier, $this->context, $this->cacheIdentifier));
148 return $statementHandle->fetchColumn();
149 }
150
151 /**
152 * Checks if a cache entry with the specified identifier exists.
153 *
154 * @param string $entryIdentifier An identifier specifying the cache entry
155 * @return boolean TRUE if such an entry exists, FALSE if not
156 * @api
157 */
158 public function has($entryIdentifier) {
159 $statementHandle = $this->databaseHandle->prepare('SELECT COUNT("identifier") FROM "cache" WHERE "identifier"=? AND "context"=? AND "cache"=?' . $this->getNotExpiredStatement());
160 $statementHandle->execute(array($entryIdentifier, $this->context, $this->cacheIdentifier));
161 return $statementHandle->fetchColumn() > 0;
162 }
163
164 /**
165 * Removes all cache entries matching the specified identifier.
166 * Usually this only affects one entry but if - for what reason ever -
167 * old entries for the identifier still exist, they are removed as well.
168 *
169 * @param string $entryIdentifier Specifies the cache entry to remove
170 * @return boolean TRUE if (at least) an entry could be removed or FALSE if no entry was found
171 * @api
172 */
173 public function remove($entryIdentifier) {
174 $statementHandle = $this->databaseHandle->prepare('DELETE FROM "tags" WHERE "identifier"=? AND "context"=? AND "cache"=?');
175 $statementHandle->execute(array($entryIdentifier, $this->context, $this->cacheIdentifier));
176 $statementHandle = $this->databaseHandle->prepare('DELETE FROM "cache" WHERE "identifier"=? AND "context"=? AND "cache"=?');
177 $statementHandle->execute(array($entryIdentifier, $this->context, $this->cacheIdentifier));
178 return $statementHandle->rowCount() > 0;
179 }
180
181 /**
182 * Removes all cache entries of this cache.
183 *
184 * @return void
185 * @api
186 */
187 public function flush() {
188 $statementHandle = $this->databaseHandle->prepare('DELETE FROM "tags" WHERE "context"=? AND "cache"=?');
189 $statementHandle->execute(array($this->context, $this->cacheIdentifier));
190 $statementHandle = $this->databaseHandle->prepare('DELETE FROM "cache" WHERE "context"=? AND "cache"=?');
191 $statementHandle->execute(array($this->context, $this->cacheIdentifier));
192 }
193
194 /**
195 * Removes all cache entries of this cache which are tagged by the specified tag.
196 *
197 * @param string $tag The tag the entries must have
198 * @return void
199 * @api
200 */
201 public function flushByTag($tag) {
202 $statementHandle = $this->databaseHandle->prepare('DELETE FROM "cache" WHERE "context"=? AND "cache"=? AND "identifier" IN (SELECT "identifier" FROM "tags" WHERE "context"=? AND "cache"=? AND "tag"=?)');
203 $statementHandle->execute(array($this->context, $this->cacheIdentifier, $this->context, $this->cacheIdentifier, $tag));
204 $statementHandle = $this->databaseHandle->prepare('DELETE FROM "tags" WHERE "context"=? AND "cache"=? AND "tag"=?');
205 $statementHandle->execute(array($this->context, $this->cacheIdentifier, $tag));
206 }
207
208 /**
209 * Finds and returns all cache entry identifiers which are tagged by the
210 * specified tag.
211 *
212 * @param string $tag The tag to search for
213 * @return array An array with identifiers of all matching entries. An empty array if no entries matched
214 * @api
215 */
216 public function findIdentifiersByTag($tag) {
217 $statementHandle = $this->databaseHandle->prepare('SELECT "identifier" FROM "tags" WHERE "context"=? AND "cache"=? AND "tag"=?');
218 $statementHandle->execute(array($this->context, $this->cacheIdentifier, $tag));
219 return $statementHandle->fetchAll(\PDO::FETCH_COLUMN);
220 }
221
222 /**
223 * Does garbage collection
224 *
225 * @return void
226 * @api
227 */
228 public function collectGarbage() {
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));
233 }
234
235 /**
236 * Returns an SQL statement that evaluates to TRUE if the entry is not expired.
237 *
238 * @return string
239 */
240 protected function getNotExpiredStatement() {
241 return ' AND ("lifetime" = 0 OR "created" + "lifetime" >= ' . $GLOBALS['EXEC_TIME'] . ')';
242 }
243
244 /**
245 * Connect to the database
246 *
247 * @return void
248 * @throws \RuntimeException if something goes wrong
249 */
250 protected function connect() {
251 try {
252 $splitdsn = explode(':', $this->dataSourceName, 2);
253 $this->pdoDriver = $splitdsn[0];
254 if ($this->pdoDriver === 'sqlite' && !file_exists($splitdsn[1])) {
255 $this->databaseHandle = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('PDO', $this->dataSourceName, $this->username, $this->password);
256 $this->createCacheTables();
257 } else {
258 $this->databaseHandle = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('PDO', $this->dataSourceName, $this->username, $this->password);
259 }
260 $this->databaseHandle->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
261 if ($this->pdoDriver === 'mysql') {
262 $this->databaseHandle->exec('SET SESSION sql_mode=\'ANSI\';');
263 }
264 } catch (\PDOException $e) {
265 throw new \RuntimeException('Could not connect to cache table with DSN "' . $this->dataSourceName . '". PDO error: ' . $e->getMessage(), 1334736164);
266 }
267 }
268
269 /**
270 * Creates the tables needed for the cache backend.
271 *
272 * @return void
273 * @throws \RuntimeException if something goes wrong
274 */
275 protected function createCacheTables() {
276 try {
277 \TYPO3\CMS\Core\Database\PdoHelper::importSql($this->databaseHandle, $this->pdoDriver, PATH_t3lib . 'cache/backend/resources/ddl.sql');
278 } catch (\PDOException $e) {
279 throw new \RuntimeException('Could not create cache tables with DSN "' . $this->dataSourceName . '". PDO error: ' . $e->getMessage(), 1259576985);
280 }
281 }
282
283 }
284
285
286 ?>