7db554764aeefc6dd68c5625f55c202bc54de255
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Cache / Frontend / VariableFrontend.php
1 <?php
2 namespace TYPO3\CMS\Core\Cache\Frontend;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2009-2013 Ingo Renner <ingo@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * A cache frontend for any kinds of PHP variables
28 *
29 * This file is a backport from FLOW3
30 *
31 * @author Robert Lemke <robert@typo3.org>
32 * @author Karsten Dambekalns <karsten@typo3.org>
33 * @api
34 */
35 class VariableFrontend extends \TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend {
36
37 /**
38 * If the extension "igbinary" is installed, use it for increased performance.
39 * Caching the result of extension_loaded() here is faster than calling extension_loaded() multiple times.
40 *
41 * @var boolean
42 */
43 protected $useIgBinary = FALSE;
44
45 /**
46 * Initializes this cache frontend
47 *
48 * @return void
49 */
50 public function initializeObject() {
51 $this->useIgBinary = extension_loaded('igbinary');
52 }
53
54 /**
55 * Saves the value of a PHP variable in the cache. Note that the variable
56 * will be serialized if necessary.
57 *
58 * @param string $entryIdentifier An identifier used for this cache entry
59 * @param mixed $variable The variable to cache
60 * @param array $tags Tags to associate with this cache entry
61 * @param integer $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited liftime.
62 * @return void
63 * @throws \InvalidArgumentException if the identifier or tag is not valid
64 * @api
65 */
66 public function set($entryIdentifier, $variable, array $tags = array(), $lifetime = NULL) {
67 if (!$this->isValidEntryIdentifier($entryIdentifier)) {
68 throw new \InvalidArgumentException('"' . $entryIdentifier . '" is not a valid cache entry identifier.', 1233058264);
69 }
70 foreach ($tags as $tag) {
71 if (!$this->isValidTag($tag)) {
72 throw new \InvalidArgumentException('"' . $tag . '" is not a valid tag for a cache entry.', 1233058269);
73 }
74 }
75 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/cache/frontend/class.t3lib_Cache\Frontend\VariableFrontend.php']['set'])) {
76 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/cache/frontend/class.t3lib_Cache\Frontend\VariableFrontend.php']['set'] as $_funcRef) {
77 $params = array(
78 'entryIdentifier' => &$entryIdentifier,
79 'variable' => &$variable,
80 'tags' => &$tags,
81 'lifetime' => &$lifetime
82 );
83 \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($_funcRef, $params, $this);
84 }
85 }
86 if ($this->useIgBinary === TRUE) {
87 $this->backend->set($entryIdentifier, igbinary_serialize($variable), $tags, $lifetime);
88 } else {
89 $this->backend->set($entryIdentifier, serialize($variable), $tags, $lifetime);
90 }
91 }
92
93 /**
94 * Finds and returns a variable value from the cache.
95 *
96 * @param string $entryIdentifier Identifier of the cache entry to fetch
97 * @return mixed The value
98 * @throws \InvalidArgumentException if the identifier is not valid
99 * @api
100 */
101 public function get($entryIdentifier) {
102 if (!$this->isValidEntryIdentifier($entryIdentifier)) {
103 throw new \InvalidArgumentException('"' . $entryIdentifier . '" is not a valid cache entry identifier.', 1233058294);
104 }
105 $rawResult = $this->backend->get($entryIdentifier);
106 if ($rawResult === FALSE) {
107 return FALSE;
108 } else {
109 return $this->useIgBinary === TRUE ? igbinary_unserialize($rawResult) : unserialize($rawResult);
110 }
111 }
112
113 /**
114 * Finds and returns all cache entries which are tagged by the specified tag.
115 *
116 * @param string $tag The tag to search for
117 * @return array An array with the content of all matching entries. An empty array if no entries matched
118 * @throws \InvalidArgumentException if the tag is not valid
119 * @api
120 */
121 public function getByTag($tag) {
122 if (!$this->isValidTag($tag)) {
123 throw new \InvalidArgumentException('"' . $tag . '" is not a valid tag for a cache entry.', 1233058312);
124 }
125 $entries = array();
126 $identifiers = $this->backend->findIdentifiersByTag($tag);
127 foreach ($identifiers as $identifier) {
128 $rawResult = $this->backend->get($identifier);
129 if ($rawResult !== FALSE) {
130 $entries[] = $this->useIgBinary === TRUE ? igbinary_unserialize($rawResult) : unserialize($rawResult);
131 }
132 }
133 return $entries;
134 }
135
136 }