Added feature 9097: Introduce a new caching framework (backported from FLOW3)
[Packages/TYPO3.CMS.git] / t3lib / cache / class.t3lib_cache_abstractcache.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 * An abstract cache
28 *
29 * This file is a backport from FLOW3
30 *
31 * @package TYPO3
32 * @subpackage t3lib_cache
33 * @version $Id$
34 */
35 abstract class t3lib_cache_AbstractCache {
36
37 const PATTERN_IDENTIFIER = '/^[a-zA-Z0-9_%]{1,250}$/';
38
39 /**
40 * @var string Identifies this cache
41 */
42 protected $identifier;
43
44 /**
45 * @var t3lib_cache_AbstractBackend
46 */
47 protected $backend;
48
49 /**
50 * Constructs the cache
51 *
52 * @param string A identifier which describes this cache
53 * @param t3lib_cache_AbstractBackend Backend to be used for this cache
54 * @author Robert Lemke <robert@typo3.org>
55 * @throws InvalidArgumentException if the identifier doesn't match PATTERN_IDENTIFIER
56 */
57 public function __construct($identifier, t3lib_cache_AbstractBackend $backend) {
58 if (!preg_match(self::PATTERN_IDENTIFIER, $identifier)) {
59 throw new InvalidArgumentException('"' . $identifier . '" is not a valid cache identifier.', 1203584729);
60 }
61
62 $this->identifier = $identifier;
63 $this->backend = $backend;
64 $this->backend->setCache($this);
65 }
66
67 /**
68 * Returns this cache's identifier
69 *
70 * @return string The identifier for this cache
71 * @author Robert Lemke <robert@typo3.org>
72 */
73 public function getIdentifier() {
74 return $this->identifier;
75 }
76
77 /**
78 * Returns the backend used by this cache
79 *
80 * @return t3lib_cache_AbstractBackend The backend used by this cache
81 * @author Robert Lemke <robert@typo3.org>
82 */
83 public function getBackend() {
84 return $this->backend;
85 }
86
87 /**
88 * Saves data in the cache.
89 *
90 * @param string Something which identifies the data - depends on concrete cache
91 * @param mixed The data to cache - also depends on the concrete cache implementation
92 * @param array Tags to associate with this cache entry
93 * @return void
94 * @author Robert Lemke <robert@typo3.org>
95 */
96 abstract public function save($entryIdentifier, $data, array $tags = array());
97
98 /**
99 * Loads data from the cache.
100 *
101 * @param string Something which identifies the cache entry - depends on concrete cache
102 * @return mixed
103 * @author Robert Lemke <robert@typo3.org>
104 */
105 abstract public function load($entryIdentifier);
106
107 /**
108 * Finds, loads, and returns all cache entries which are tagged by the specified tags.
109 * The asterisk ("*") is allowed as a wildcard at the beginning and the end of
110 * the tags.
111 *
112 * @param array An array of tags to search for, the "*" wildcard is supported
113 * @return array An array with all matching entries. An empty array if no entries matched
114 * @author Ingo Renner <ingo@typo3.org>
115 */
116 public function loadByTag(array $tags) {
117 $loadedEntries = array();
118 $foundEntries = $this->findEntriesByTag($tags);
119
120 foreach($foundEntries as $foundEntryIdentifier) {
121 $loadedEntries[$foundEntryIdentifier] = $this->load($foundEntryIdentifier);
122 }
123
124 return $loadedEntries;
125 }
126
127 /**
128 * Finds and returns all cache entry identifiers which are tagged by the specified tags.
129 * The asterisk ("*") is allowed as a wildcard at the beginning and the end of
130 * a tag.
131 *
132 * @param array Array of tags to search for, the "*" wildcard is supported
133 * @return array An array with identifiers of all matching entries. An empty array if no entries matched
134 */
135 public function findEntriesByTag(array $tags) {
136 return $this->backend->findEntriesByTags($tags);
137 }
138
139 /**
140 * Checks if a cache entry with the specified identifier exists.
141 *
142 * @param string An identifier specifying the cache entry
143 * @return boolean TRUE if such an entry exists, FALSE if not
144 * @author Robert Lemke <robert@typo3.org>
145 */
146 abstract public function has($entryIdentifier);
147
148 /**
149 * Removes the given cache entry from the cache.
150 *
151 * @param string An identifier specifying the cache entry
152 * @return boolean TRUE if such an entry exists, FALSE if not
153 * @author Sebastian Kurfuerst <sebastian@typo3.org>
154 */
155 abstract public function remove($entryIdentifier);
156
157 /**
158 * Removes all cache entries of this cache.
159 *
160 * @return void
161 * @author Robert Lemke <robert@typo3.org>
162 */
163 public function flush() {
164 $this->backend->flush();
165 }
166
167 /**
168 * Removes all cache entries of this cache which are tagged by the specified tag.
169 *
170 * @param string $tag The tag the entries must have
171 * @return void
172 * @author Robert Lemke <robert@typo3.org>
173 */
174 public function flushByTag($tag) {
175 $this->backend->flushByTag($tag);
176 }
177
178 }
179
180
181 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/class.t3lib_cache_abstractcache.php']) {
182 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/class.t3lib_cache_abstractcache.php']);
183 }
184
185 ?>