Follow-up to #9408: Fixed ugly typo
[Packages/TYPO3.CMS.git] / t3lib / cache / backend / class.t3lib_cache_backend_db.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2008 Ingo Renner <ingo@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script 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 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 /**
27 * A caching backend which stores cache entries in files
28 *
29 * @package TYPO3
30 * @subpackage t3lib_cache
31 * @version $Id$
32 */
33 class t3lib_cache_backend_Db extends t3lib_cache_AbstractBackend {
34
35 protected $cacheTable;
36
37 /**
38 * Saves data in a cache file.
39 *
40 * @param string An identifier for this specific cache entry
41 * @param string The data to be stored
42 * @param array Tags to associate with this cache entry
43 * @param integer Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited liftime.
44 * @return void
45 * @throws t3lib_cache_Exception if the directory does not exist or is not writable, or if no cache frontend has been set.
46 * @author Ingo Renner <ingo@typo3.org>
47 */
48 public function save($entryIdentifier, $data, array $tags = array(), $lifetime = NULL) {
49 if (is_null($lifetime)) {
50 $lifetime = $this->defaultLifetime;
51 }
52
53 $this->remove($entryIdentifier);
54
55 $GLOBALS['TYPO3_DB']->exec_INSERTquery(
56 $this->cacheTable,
57 array(
58 'identifier' => $entryIdentifier,
59 'crdate' => time(),
60 'data' => $data,
61 'tags' => implode(',', $tags),
62 'lifetime' => $lifetime
63 )
64 );
65 }
66
67 /**
68 * Loads data from a cache file.
69 *
70 * @param string An identifier which describes the cache entry to load
71 * @return mixed The cache entry's content as a string or FALSE if the cache entry could not be loaded
72 * @author Ingo Renner <ingo@typo3.org>
73 */
74 public function load($entryIdentifier) {
75 $cacheEntry = false;
76
77 $caheEntries = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
78 'data',
79 $this->cacheTable,
80 'identifier = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($entryIdentifier, $this->cacheTable) . ' '
81 . 'AND (crdate + lifetime) >= ' . time()
82 );
83
84 if (count($caheEntries) == 1) {
85 $cacheEntry = $caheEntries[0]['data'];
86 }
87
88 return $cacheEntry;
89 }
90
91 /**
92 * Checks if a cache entry with the specified identifier exists.
93 *
94 * @param unknown_type
95 * @return boolean TRUE if such an entry exists, FALSE if not
96 * @author Ingo Renner <ingo@typo3.org>
97 */
98 public function has($entryIdentifier) {
99 $hasEntry = false;
100
101 $caheEntries = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
102 'data',
103 $this->cacheTable,
104 'identifier = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($entryIdentifier, $this->cacheTable) . ' '
105 . 'AND (crdate + lifetime) >= ' . time()
106 );
107
108 if (count($caheEntries) == 1) {
109 $hasEntry = true;
110 }
111
112 return $hasEntry;
113 }
114
115 /**
116 * Removes all cache entries matching the specified identifier.
117 * Usually this only affects one entry.
118 *
119 * @param string Specifies the cache entry to remove
120 * @return boolean TRUE if (at least) an entry could be removed or FALSE if no entry was found
121 * @author Ingo Renner <ingo@typo3.org>
122 */
123 public function remove($entryIdentifier) {
124 $entryRemoved = false;
125
126 $res = $GLOBALS['TYPO3_DB']->exec_DELETEquery(
127 $this->cacheTable,
128 'identifier = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($entryIdentifier, $this->cacheTable)
129 );
130
131 if($GLOBALS['TYPO3_DB']->sql_affected_rows($res) == 1) {
132 $entryRemoved = true;
133 }
134
135 return $entryRemoved;
136 }
137
138 /**
139 * Finds and returns all cache entries which are tagged by the specified tag.
140 * The asterisk ("*") is allowed as a wildcard at the beginning and the end of
141 * the tag.
142 *
143 * @param string The tag to search for, the "*" wildcard is supported
144 * @return array An array with identifiers of all matching entries. An empty array if no entries matched
145 * @author Ingo Renner <ingo@typo3.org>
146 */
147 public function findEntriesByTag($tag) {
148 $cacheEntries = array();
149
150 $cacheEntryRows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
151 'identifier',
152 $this->cacheTable,
153 $this->getListQueryForTag($tag)
154 );
155
156 foreach ($cacheEntryRows as $cacheEntryRow) {
157 $cacheEntries[$cacheEntryRow['identifier']] = $cacheEntryRow['identifier'];
158 }
159
160 return $cacheEntries;
161 }
162
163 /**
164 * Finds and returns all cache entry identifiers which are tagged by the specified tags.
165 * The asterisk ("*") is allowed as a wildcard at the beginning and the end of
166 * a tag.
167 *
168 * @param array Array of tags to search for, the "*" wildcard is supported
169 * @return array An array with identifiers of all matching entries. An empty array if no entries matched
170 * @author Ingo Renner <ingo@typo3.org>
171 */
172 public function findEntriesByTags(array $tags) {
173 $cacheEntries = array();
174 $whereClause = array();
175
176 foreach ($tags as $tag) {
177 $whereClause[] = $this->getListQueryForTag($tag);
178 }
179
180 $cacheEntryRows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
181 'identifier',
182 $this->cacheTable,
183 implode(' AND ', $whereClause)
184 );
185
186 foreach ($cacheEntryRows as $cacheEntryRow) {
187 $cacheEntries[$cacheEntryRow['identifier']] = $cacheEntryRow['identifier'];
188 }
189
190 return $cacheEntries;
191 }
192
193 /**
194 * Removes all cache entries of this cache.
195 *
196 * @return void
197 * @author Ingo Renner <ingo@typo3.org>
198 */
199 public function flush() {
200 $GLOBALS['TYPO3_DB']->sql_query('TRUNCATE ' . $this->cacheTable);
201 }
202
203 /**
204 * Removes all cache entries of this cache which are tagged by the specified tag.
205 *
206 * @param string The tag the entries must have
207 * @return void
208 * @author Ingo Renner <ingo@typo3.org>
209 */
210 public function flushByTag($tag) {
211 foreach ($this->findEntriesByTag($tag) as $entryIdentifier) {
212 $this->remove($entryIdentifier);
213 }
214 }
215
216 protected function setCacheTable($cacheTable) {
217 $this->cacheTable = $cacheTable;
218 }
219
220 /**
221 * Gets the query to be used for selecting entries by a tag. The asterisk ("*")
222 * is allowed as a wildcard at the beginning and the end of a tag.
223 *
224 * @param string The tag to search for, the "*" wildcard is supported
225 * @return string the query to be used for selecting entries
226 * @author Oliver Hader <oliver@typo3.org>
227 */
228 protected function getListQueryForTag($tag) {
229 return str_replace('*', '%', $GLOBALS['TYPO3_DB']->listQuery('tags', $tag, $this->cacheTable));
230 }
231 }
232
233
234 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/backend/class.t3lib_cache_backend_db.php']) {
235 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/backend/class.t3lib_cache_backend_db.php']);
236 }
237
238 ?>