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