[TASK] Update php-cs-fixer to 2.5.0
[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 /**
18 * A caching backend which stores cache entries during one script run.
19 *
20 * This file is a backport from FLOW3
21 * @api
22 */
23 class TransientMemoryBackend extends \TYPO3\CMS\Core\Cache\Backend\AbstractBackend implements TaggableBackendInterface, TransientBackendInterface
24 {
25 /**
26 * @var array
27 */
28 protected $entries = [];
29
30 /**
31 * @var array
32 */
33 protected $tagsAndEntries = [];
34
35 /**
36 * Saves data in the cache.
37 *
38 * @param string $entryIdentifier An identifier for this specific cache entry
39 * @param string $data The data to be stored
40 * @param array $tags Tags to associate with this cache entry
41 * @param int $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited lifetime.
42 * @throws \TYPO3\CMS\Core\Cache\Exception if no cache frontend has been set.
43 * @throws \TYPO3\CMS\Core\Cache\Exception\InvalidDataException
44 * @api
45 */
46 public function set($entryIdentifier, $data, array $tags = [], $lifetime = null)
47 {
48 if (!$this->cache instanceof \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface) {
49 throw new \TYPO3\CMS\Core\Cache\Exception('No cache frontend has been set yet via setCache().', 1238244992);
50 }
51 $this->entries[$entryIdentifier] = $data;
52 foreach ($tags as $tag) {
53 $this->tagsAndEntries[$tag][$entryIdentifier] = true;
54 }
55 }
56
57 /**
58 * Loads data from the cache.
59 *
60 * @param string $entryIdentifier An identifier which describes the cache entry to load
61 * @return mixed The cache entry's content as a string or FALSE if the cache entry could not be loaded
62 * @api
63 */
64 public function get($entryIdentifier)
65 {
66 return isset($this->entries[$entryIdentifier]) ? $this->entries[$entryIdentifier] : false;
67 }
68
69 /**
70 * Checks if a cache entry with the specified identifier exists.
71 *
72 * @param string $entryIdentifier An identifier specifying the cache entry
73 * @return bool TRUE if such an entry exists, FALSE if not
74 * @api
75 */
76 public function has($entryIdentifier)
77 {
78 return isset($this->entries[$entryIdentifier]);
79 }
80
81 /**
82 * Removes all cache entries matching the specified identifier.
83 *
84 * @param string $entryIdentifier Specifies the cache entry to remove
85 * @return bool TRUE if the entry could be removed or FALSE if no entry was found
86 * @api
87 */
88 public function remove($entryIdentifier)
89 {
90 if (isset($this->entries[$entryIdentifier])) {
91 unset($this->entries[$entryIdentifier]);
92 foreach ($this->tagsAndEntries as $tag => $_) {
93 if (isset($this->tagsAndEntries[$tag][$entryIdentifier])) {
94 unset($this->tagsAndEntries[$tag][$entryIdentifier]);
95 }
96 }
97 return true;
98 }
99 return false;
100 }
101
102 /**
103 * Finds and returns all cache entry identifiers which are tagged by the
104 * specified tag.
105 *
106 * @param string $tag The tag to search for
107 * @return array An array with identifiers of all matching entries. An empty array if no entries matched
108 * @api
109 */
110 public function findIdentifiersByTag($tag)
111 {
112 if (isset($this->tagsAndEntries[$tag])) {
113 return array_keys($this->tagsAndEntries[$tag]);
114 }
115 return [];
116 }
117
118 /**
119 * Removes all cache entries of this cache.
120 *
121 * @api
122 */
123 public function flush()
124 {
125 $this->entries = [];
126 $this->tagsAndEntries = [];
127 }
128
129 /**
130 * Removes all cache entries of this cache which are tagged by the specified tag.
131 *
132 * @param string $tag The tag the entries must have
133 * @api
134 */
135 public function flushByTag($tag)
136 {
137 $identifiers = $this->findIdentifiersByTag($tag);
138 foreach ($identifiers as $identifier) {
139 $this->remove($identifier);
140 }
141 }
142
143 /**
144 * Does nothing
145 *
146 * @api
147 */
148 public function collectGarbage()
149 {
150 }
151 }