89a3ee615695d5743e7e786f95cbe87bffdbb583
[Packages/TYPO3.CMS.git] / t3lib / cache / frontend / class.t3lib_cache_frontend_stringfrontend.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2010 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 /**
27 * A cache frontend for strings. Nothing else.
28 *
29 * This file is a backport from FLOW3
30 *
31 * @package TYPO3
32 * @subpackage t3lib_cache
33 * @api
34 * @version $Id$
35 */
36 class t3lib_cache_frontend_StringFrontend extends t3lib_cache_frontend_AbstractFrontend {
37
38 /**
39 * Saves the value of a PHP variable in the cache. Note that the variable
40 * will be serialized if necessary.
41 *
42 * @param string An identifier used for this cache entry
43 * @param string The variable to cache
44 * @param array Tags to associate with this cache entry
45 * @param integer Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited liftime.
46 * @return void
47 * @author Karsten Dambekalns <karsten@typo3.org>
48 */
49 public function set($entryIdentifier, $string, $tags = array(), $lifetime = NULL) {
50 if (!$this->isValidEntryIdentifier($entryIdentifier)) {
51 throw new InvalidArgumentException(
52 '"' . $entryIdentifier . '" is not a valid cache entry identifier.',
53 1233057566
54 );
55 }
56
57 if (!is_string($string)) {
58 throw new t3lib_cache_exception_InvalidData(
59 'Only strings can be digested by the StringCache. Thanks.',
60 1222808333
61 );
62 }
63
64 foreach ($tags as $tag) {
65 if (!$this->isValidTag($tag)) {
66 throw new InvalidArgumentException(
67 '"' . $tag . '" is not a valid tag for a cache entry.',
68 1233057512
69 );
70 }
71 }
72
73 $this->backend->set($entryIdentifier, $string, $tags, $lifetime);
74 }
75
76 /**
77 * Loads a variable value from the cache.
78 *
79 * @param string Identifier of the cache entry to fetch
80 * @return string The value
81 * @author Karsten Dambekalns <karsten@typo3.org>
82 */
83 public function get($entryIdentifier) {
84 if (!$this->isValidEntryIdentifier($entryIdentifier)) {
85 throw new InvalidArgumentException(
86 '"' . $entryIdentifier . '" is not a valid cache entry identifier.',
87 1233057752
88 );
89 }
90
91 return $this->backend->get($entryIdentifier);
92 }
93
94 /**
95 * Finds and returns all cache entries which are tagged by the specified tag.
96 *
97 * @param string $tag The tag to search for
98 * @return array An array with the content of all matching entries. An empty array if no entries matched
99 * @author Karsten Dambekalns <karsten@typo3.org>
100 */
101 public function getByTag($tag) {
102 if (!$this->isValidTag($tag)) {
103 throw new InvalidArgumentException(
104 '"' . $tag . '" is not a valid tag for a cache entry.',
105 1233057772
106 );
107 }
108
109 $entries = array();
110 $identifiers = $this->backend->findIdentifiersByTag($tag);
111
112 foreach ($identifiers as $identifier) {
113 $entries[] = $this->backend->get($identifier);
114 }
115
116 return $entries;
117 }
118
119 }
120
121
122 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/class.t3lib_cache_stringcache.php']) {
123 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/class.t3lib_cache_stringcache.php']);
124 }
125
126 ?>