[TASK] Re-work/simplify copyright header in PHP files - Part 2
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Cache / Backend / TransientMemoryBackend.php
1 <?php
2 namespace TYPO3\CMS\Core\Cache\Backend;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 /**
17 * A caching backend which stores cache entries during one script run.
18 *
19 * This file is a backport from FLOW3
20 *
21 * @author Robert Lemke <robert@typo3.org>
22 * @api
23 */
24 class TransientMemoryBackend extends \TYPO3\CMS\Core\Cache\Backend\AbstractBackend implements \TYPO3\CMS\Core\Cache\Backend\TaggableBackendInterface {
25
26 /**
27 * @var array
28 */
29 protected $entries = array();
30
31 /**
32 * @var array
33 */
34 protected $tagsAndEntries = array();
35
36 /**
37 * Saves data in the cache.
38 *
39 * @param string $entryIdentifier An identifier for this specific cache entry
40 * @param string $data The data to be stored
41 * @param array $tags Tags to associate with this cache entry
42 * @param integer $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited liftime.
43 * @return void
44 * @throws \TYPO3\CMS\Core\Cache\Exception if no cache frontend has been set.
45 * @throws \TYPO3\CMS\Core\Cache\Exception\InvalidDataException
46 * @api
47 */
48 public function set($entryIdentifier, $data, array $tags = array(), $lifetime = NULL) {
49 if (!$this->cache instanceof \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface) {
50 throw new \TYPO3\CMS\Core\Cache\Exception('No cache frontend has been set yet via setCache().', 1238244992);
51 }
52 if (!is_string($data)) {
53 throw new \TYPO3\CMS\Core\Cache\Exception\InvalidDataException('The specified data is of type "' . gettype($data) . '" but a string is expected.', 1238244993);
54 }
55 $this->entries[$entryIdentifier] = $data;
56 foreach ($tags as $tag) {
57 $this->tagsAndEntries[$tag][$entryIdentifier] = TRUE;
58 }
59 }
60
61 /**
62 * Loads data from the cache.
63 *
64 * @param string $entryIdentifier An identifier which describes the cache entry to load
65 * @return mixed The cache entry's content as a string or FALSE if the cache entry could not be loaded
66 * @api
67 */
68 public function get($entryIdentifier) {
69 return isset($this->entries[$entryIdentifier]) ? $this->entries[$entryIdentifier] : FALSE;
70 }
71
72 /**
73 * Checks if a cache entry with the specified identifier exists.
74 *
75 * @param string $entryIdentifier An identifier specifying the cache entry
76 * @return boolean TRUE if such an entry exists, FALSE if not
77 * @api
78 */
79 public function has($entryIdentifier) {
80 return isset($this->entries[$entryIdentifier]);
81 }
82
83 /**
84 * Removes all cache entries matching the specified identifier.
85 *
86 * @param string $entryIdentifier Specifies the cache entry to remove
87 * @return boolean TRUE if the entry could be removed or FALSE if no entry was found
88 * @api
89 */
90 public function remove($entryIdentifier) {
91 if (isset($this->entries[$entryIdentifier])) {
92 unset($this->entries[$entryIdentifier]);
93 foreach (array_keys($this->tagsAndEntries) as $tag) {
94 if (isset($this->tagsAndEntries[$tag][$entryIdentifier])) {
95 unset($this->tagsAndEntries[$tag][$entryIdentifier]);
96 }
97 }
98 return TRUE;
99 } else {
100 return FALSE;
101 }
102 }
103
104 /**
105 * Finds and returns all cache entry identifiers which are tagged by the
106 * specified tag.
107 *
108 * @param string $tag The tag to search for
109 * @return array An array with identifiers of all matching entries. An empty array if no entries matched
110 * @api
111 */
112 public function findIdentifiersByTag($tag) {
113 if (isset($this->tagsAndEntries[$tag])) {
114 return array_keys($this->tagsAndEntries[$tag]);
115 } else {
116 return array();
117 }
118 }
119
120 /**
121 * Removes all cache entries of this cache.
122 *
123 * @return void
124 * @api
125 */
126 public function flush() {
127 $this->entries = array();
128 $this->tagsAndEntries = array();
129 }
130
131 /**
132 * Removes all cache entries of this cache which are tagged by the specified tag.
133 *
134 * @param string $tag The tag the entries must have
135 * @return void
136 * @api
137 */
138 public function flushByTag($tag) {
139 $identifiers = $this->findIdentifiersByTag($tag);
140 foreach ($identifiers as $identifier) {
141 $this->remove($identifier);
142 }
143 }
144
145 /**
146 * Does nothing
147 *
148 * @return void
149 * @api
150 */
151 public function collectGarbage() {
152
153 }
154
155 }