Added feature 9097: Introduce a new caching framework (backported from FLOW3)
[Packages/TYPO3.CMS.git] / t3lib / cache / class.t3lib_cache_abstractbackend.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 * An abstract caching backend
27 *
28 * This file is a backport from FLOW3
29 *
30 * @author Ingo Renner <ingo@typo3.org>
31 * @package TYPO3
32 * @subpackage t3lib_cache
33 * @version $Id$
34 */
35 abstract class t3lib_cache_AbstractBackend {
36
37 /**
38 * Pattern an entry identifer must match.
39 */
40 const PATTERN_ENTRYIDENTIFIER = '/^[a-zA-Z0-9_%]{1,250}$/';
41
42 /**
43 * Pattern a tag identifer must match.
44 */
45 const PATTERN_TAG = '/^[a-zA-Z0-9_%]{1,250}$/';
46
47 /**
48 * @var t3lib_cache_AbstractCache Reference to the cache which uses this backend
49 */
50 protected $cache;
51
52 /**
53 * @var integer Default lifetime of a cache entry in seconds
54 */
55 protected $defaultLifetime = 3600;
56
57
58 /**
59 * Constructs this backend
60 *
61 * @param mixed Configuration options - depends on the actual backend
62 */
63 public function __construct(array $options = array()) {
64 if (is_array($options) || $options instanceof ArrayAccess) {
65 foreach ($options as $optionKey => $optionValue) {
66 $methodName = 'set' . ucfirst($optionKey);
67 if (method_exists($this, $methodName)) {
68 $this->$methodName($optionValue);
69 }
70 }
71 }
72 }
73
74 /**
75 * Sets a reference to the cache which uses this backend
76 *
77 * @param t3lib_cache_AbstractCache The frontend for this backend
78 * @return void
79 */
80 public function setCache(t3lib_cache_AbstractCache $cache) {
81 $this->cache = $cache;
82 }
83
84 /**
85 * Saves data in the cache.
86 *
87 * @param string An identifier for this specific cache entry
88 * @param string The data to be stored
89 * @param array Tags to associate with this cache entry
90 * @param integer Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited liftime.
91 * @return void
92 * @throws t3lib_cache_Exception if no cache frontend has been set.
93 * @throws InvalidArgumentException if the identifier is not valid
94 * @throws t3lib_cache_Exception_InvalidData if the data is not a string
95 */
96 abstract public function save($entryIdentifier, $data, array $tags = array(), $lifetime = NULL);
97
98 /**
99 * Loads data from the cache.
100 *
101 * @param string An identifier which describes the cache entry to load
102 * @return mixed The cache entry's content as a string or FALSE if the cache entry could not be loaded
103 */
104 abstract public function load($entryIdentifier);
105
106 /**
107 * Checks if a cache entry with the specified identifier exists.
108 *
109 * @param string An identifier specifying the cache entry
110 * @return boolean TRUE if such an entry exists, FALSE if not
111 */
112 abstract public function has($entryIdentifier);
113
114 /**
115 * Removes all cache entries matching the specified identifier.
116 * Usually this only affects one entry but if - for what reason ever -
117 * old entries for the identifier still exist, they are removed as well.
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 */
122 abstract public function remove($entryIdentifier);
123
124 /**
125 * Removes all cache entries of this cache.
126 *
127 * @return void
128 */
129 abstract public function flush();
130
131 /**
132 * Removes all cache entries of this cache which are tagged by the specified tag.
133 *
134 * @param string The tag the entries must have
135 * @return void
136 */
137 abstract public function flushByTag($tag);
138
139 /**
140 * Finds and returns all cache entry identifiers which are tagged by the specified tag.
141 * The asterisk ("*") is allowed as a wildcard at the beginning and the end of
142 * the tag.
143 *
144 * @param string The tag to search for, the "*" wildcard is supported
145 * @return array An array with identifiers of all matching entries. An empty array if no entries matched
146 */
147 abstract public function findEntriesByTag($tag);
148
149 /**
150 * Finds and returns all cache entry identifiers which are tagged by the specified tags.
151 * The asterisk ("*") is allowed as a wildcard at the beginning and the end of
152 * a tag.
153 *
154 * @param array Array of tags to search for, the "*" wildcard is supported
155 * @return array An array with identifiers of all matching entries. An empty array if no entries matched
156 */
157 abstract public function findEntriesByTags(array $tags);
158
159 /**
160 * Checks the validity of an entry identifier. Returns true if it's valid.
161 *
162 * @param string An identifier to be checked for validity
163 * @return boolean
164 * @author Christian Jul Jensen <julle@typo3.org>
165 */
166 static public function isValidEntryIdentifier($identifier) {
167 return preg_match(self::PATTERN_ENTRYIDENTIFIER, $identifier) === 1;
168 }
169
170 /**
171 * Checks the validity of a tag. Returns true if it's valid.
172 *
173 * @param string An identifier to be checked for validity
174 * @return boolean
175 * @author Robert Lemke <robert@typo3.org>
176 */
177 static public function isValidTag($tag) {
178 return preg_match(self::PATTERN_TAG, $tag) === 1;
179 }
180
181 }
182
183
184 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/class.t3lib_cache_abstractbackend.php']) {
185 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/class.t3lib_cache_abstractbackend.php']);
186 }
187
188 ?>