[TASK] Use class imports in Cache Frontend classes
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Cache / Frontend / VariableFrontend.php
1 <?php
2 namespace TYPO3\CMS\Core\Cache\Frontend;
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 use TYPO3\CMS\Core\Utility\GeneralUtility;
17
18 /**
19 * A cache frontend for any kinds of PHP variables
20 *
21 * This file is a backport from FLOW3
22 *
23 * @author Robert Lemke <robert@typo3.org>
24 * @author Karsten Dambekalns <karsten@typo3.org>
25 * @api
26 */
27 class VariableFrontend extends AbstractFrontend {
28
29 /**
30 * If the extension "igbinary" is installed, use it for increased performance.
31 * Caching the result of extension_loaded() here is faster than calling extension_loaded() multiple times.
32 *
33 * @var bool
34 */
35 protected $useIgBinary = FALSE;
36
37 /**
38 * Initializes this cache frontend
39 *
40 * @return void
41 */
42 public function initializeObject() {
43 $this->useIgBinary = extension_loaded('igbinary');
44 }
45
46 /**
47 * Saves the value of a PHP variable in the cache. Note that the variable
48 * will be serialized if necessary.
49 *
50 * @param string $entryIdentifier An identifier used for this cache entry
51 * @param mixed $variable The variable to cache
52 * @param array $tags Tags to associate with this cache entry
53 * @param int $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited liftime.
54 * @return void
55 * @throws \InvalidArgumentException if the identifier or tag is not valid
56 * @api
57 */
58 public function set($entryIdentifier, $variable, array $tags = array(), $lifetime = NULL) {
59 if (!$this->isValidEntryIdentifier($entryIdentifier)) {
60 throw new \InvalidArgumentException('"' . $entryIdentifier . '" is not a valid cache entry identifier.', 1233058264);
61 }
62 foreach ($tags as $tag) {
63 if (!$this->isValidTag($tag)) {
64 throw new \InvalidArgumentException('"' . $tag . '" is not a valid tag for a cache entry.', 1233058269);
65 }
66 }
67 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/cache/frontend/class.t3lib_cache_frontend_variablefrontend.php']['set'])) {
68 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/cache/frontend/class.t3lib_cache_frontend_variablefrontend.php']['set'] as $_funcRef) {
69 $params = array(
70 'entryIdentifier' => &$entryIdentifier,
71 'variable' => &$variable,
72 'tags' => &$tags,
73 'lifetime' => &$lifetime
74 );
75 GeneralUtility::callUserFunction($_funcRef, $params, $this);
76 }
77 }
78 if ($this->useIgBinary === TRUE) {
79 $this->backend->set($entryIdentifier, igbinary_serialize($variable), $tags, $lifetime);
80 } else {
81 $this->backend->set($entryIdentifier, serialize($variable), $tags, $lifetime);
82 }
83 }
84
85 /**
86 * Finds and returns a variable value from the cache.
87 *
88 * @param string $entryIdentifier Identifier of the cache entry to fetch
89 * @return mixed The value
90 * @throws \InvalidArgumentException if the identifier is not valid
91 * @api
92 */
93 public function get($entryIdentifier) {
94 if (!$this->isValidEntryIdentifier($entryIdentifier)) {
95 throw new \InvalidArgumentException('"' . $entryIdentifier . '" is not a valid cache entry identifier.', 1233058294);
96 }
97 $rawResult = $this->backend->get($entryIdentifier);
98 if ($rawResult === FALSE) {
99 return FALSE;
100 } else {
101 return $this->useIgBinary === TRUE ? igbinary_unserialize($rawResult) : unserialize($rawResult);
102 }
103 }
104
105 /**
106 * Finds and returns all cache entries which are tagged by the specified tag.
107 *
108 * @param string $tag The tag to search for
109 * @return array An array with the content of all matching entries. An empty array if no entries matched
110 * @throws \InvalidArgumentException if the tag is not valid
111 * @api
112 */
113 public function getByTag($tag) {
114 if (!$this->isValidTag($tag)) {
115 throw new \InvalidArgumentException('"' . $tag . '" is not a valid tag for a cache entry.', 1233058312);
116 }
117 $entries = array();
118 $identifiers = $this->backend->findIdentifiersByTag($tag);
119 foreach ($identifiers as $identifier) {
120 $rawResult = $this->backend->get($identifier);
121 if ($rawResult !== FALSE) {
122 $entries[] = $this->useIgBinary === TRUE ? igbinary_unserialize($rawResult) : unserialize($rawResult);
123 }
124 }
125 return $entries;
126 }
127
128 }