92c8ee76d52fd2de2a67c798ba3476e4caff051e
[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
17 use TYPO3\CMS\Core\Cache\Backend\TransientBackendInterface;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * A cache frontend for any kinds of PHP variables
22 */
23 class VariableFrontend extends AbstractFrontend
24 {
25 /**
26 * Saves the value of a PHP variable in the cache. Note that the variable
27 * will be serialized if necessary.
28 *
29 * @param string $entryIdentifier An identifier used for this cache entry
30 * @param mixed $variable The variable to cache
31 * @param array $tags Tags to associate with this cache entry
32 * @param int $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited lifetime.
33 * @throws \InvalidArgumentException if the identifier or tag is not valid
34 * @api
35 */
36 public function set($entryIdentifier, $variable, array $tags = [], $lifetime = null)
37 {
38 if (!$this->isValidEntryIdentifier($entryIdentifier)) {
39 throw new \InvalidArgumentException(
40 '"' . $entryIdentifier . '" is not a valid cache entry identifier.',
41 1233058264
42 );
43 }
44 foreach ($tags as $tag) {
45 if (!$this->isValidTag($tag)) {
46 throw new \InvalidArgumentException('"' . $tag . '" is not a valid tag for a cache entry.', 1233058269);
47 }
48 }
49 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/cache/frontend/class.t3lib_cache_frontend_variablefrontend.php']['set'])) {
50 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/cache/frontend/class.t3lib_cache_frontend_variablefrontend.php']['set'] as $_funcRef) {
51 $params = [
52 'entryIdentifier' => &$entryIdentifier,
53 'variable' => &$variable,
54 'tags' => &$tags,
55 'lifetime' => &$lifetime
56 ];
57 GeneralUtility::callUserFunction($_funcRef, $params, $this);
58 }
59 }
60 if (!$this->backend instanceof TransientBackendInterface) {
61 $variable = serialize($variable);
62 }
63 $this->backend->set($entryIdentifier, $variable, $tags, $lifetime);
64 }
65
66 /**
67 * Finds and returns a variable value from the cache.
68 *
69 * @param string $entryIdentifier Identifier of the cache entry to fetch
70 *
71 * @return mixed The value
72 * @throws \InvalidArgumentException if the identifier is not valid
73 * @api
74 */
75 public function get($entryIdentifier)
76 {
77 if (!$this->isValidEntryIdentifier($entryIdentifier)) {
78 throw new \InvalidArgumentException(
79 '"' . $entryIdentifier . '" is not a valid cache entry identifier.',
80 1233058294
81 );
82 }
83 $rawResult = $this->backend->get($entryIdentifier);
84 if ($rawResult === false) {
85 return false;
86 } else {
87 return $this->backend instanceof TransientBackendInterface ? $rawResult : unserialize($rawResult);
88 }
89 }
90
91 /**
92 * Finds and returns all cache entries which are tagged by the specified tag.
93 *
94 * @param string $tag The tag to search for
95 *
96 * @return array An array with the content of all matching entries. An empty array if no entries matched
97 * @throws \InvalidArgumentException if the tag is not valid
98 * @api
99 */
100 public function getByTag($tag)
101 {
102 if (!$this->isValidTag($tag)) {
103 throw new \InvalidArgumentException('"' . $tag . '" is not a valid tag for a cache entry.', 1233058312);
104 }
105 $entries = [];
106 $identifiers = $this->backend->findIdentifiersByTag($tag);
107 foreach ($identifiers as $identifier) {
108 $rawResult = $this->backend->get($identifier);
109 if ($rawResult !== false) {
110 $entries[] = $this->backend instanceof TransientBackendInterface ? $rawResult : unserialize($rawResult);
111 }
112 }
113 return $entries;
114 }
115 }