[FEATURE] Add hook to t3lib_cache_frontend_VariableFrontend->set()
[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 (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/cache/frontend/class.t3lib_cache_frontend_variablefrontend.php']['set'])) {
86 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/cache/frontend/class.t3lib_cache_frontend_variablefrontend.php']['set'] as $_funcRef) {
87 $params = array(
88 'entryIdentifier' => &$entryIdentifier,
89 'variable' => &$variable,
90 'tags' => &$tags,
91 'lifetime' => &$lifetime
92 );
93 t3lib_div::callUserFunction($_funcRef, $params, $this);
94 }
95 }
96
97 if ($this->useIgBinary === TRUE) {
98 $this->backend->set($entryIdentifier, igbinary_serialize($variable), $tags, $lifetime);
99 } else {
100 $this->backend->set($entryIdentifier, serialize($variable), $tags, $lifetime);
101 }
102 }
103
104 /**
105 * Finds and returns a variable value from the cache.
106 *
107 * @param string $entryIdentifier Identifier of the cache entry to fetch
108 * @return mixed The value
109 * @throws \InvalidArgumentException if the identifier is not valid
110 * @api
111 */
112 public function get($entryIdentifier) {
113 if (!$this->isValidEntryIdentifier($entryIdentifier)) {
114 throw new \InvalidArgumentException(
115 '"' . $entryIdentifier . '" is not a valid cache entry identifier.',
116 1233058294
117 );
118 }
119
120 $rawResult = $this->backend->get($entryIdentifier);
121 if ($rawResult === FALSE) {
122 return FALSE;
123 } else {
124 return ($this->useIgBinary === TRUE) ? igbinary_unserialize($rawResult) : unserialize($rawResult);
125 }
126 }
127
128 /**
129 * Finds and returns all cache entries which are tagged by the specified tag.
130 *
131 * @param string $tag The tag to search for
132 * @return array An array with the content of all matching entries. An empty array if no entries matched
133 * @throws \InvalidArgumentException if the tag is not valid
134 * @api
135 */
136 public function getByTag($tag) {
137 if (!$this->isValidTag($tag)) {
138 throw new \InvalidArgumentException(
139 '"' . $tag . '" is not a valid tag for a cache entry.',
140 1233058312
141 );
142 }
143
144 $entries = array();
145 $identifiers = $this->backend->findIdentifiersByTag($tag);
146 foreach ($identifiers as $identifier) {
147 $rawResult = $this->backend->get($identifier);
148 if ($rawResult !== FALSE) {
149 $entries[] = ($this->useIgBinary === TRUE) ? igbinary_unserialize($rawResult) : unserialize($rawResult);
150 }
151 }
152 return $entries;
153 }
154 }
155 ?>