[TASK] Re-work/simplify copyright header in PHP files - Part 2
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Cache / Frontend / StringFrontend.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 * A cache frontend for strings. Nothing else.
18 *
19 * This file is a backport from FLOW3
20 *
21 * @author Karsten Dambekalns <karsten@typo3.org>
22 * @api
23 */
24 class StringFrontend extends \TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend {
25
26 /**
27 * Saves the value of a PHP variable in the cache.
28 *
29 * @param string $entryIdentifier An identifier used for this cache entry
30 * @param string $string The variable to cache
31 * @param array $tags Tags to associate with this cache entry
32 * @param integer $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited liftime.
33 * @return void
34 * @throws \InvalidArgumentException if the identifier or tag is not valid
35 * @throws \TYPO3\CMS\Core\Cache\Exception\InvalidDataException if the variable to cache is not of type string
36 * @api
37 */
38 public function set($entryIdentifier, $string, array $tags = array(), $lifetime = NULL) {
39 if (!$this->isValidEntryIdentifier($entryIdentifier)) {
40 throw new \InvalidArgumentException('"' . $entryIdentifier . '" is not a valid cache entry identifier.', 1233057566);
41 }
42 if (!is_string($string)) {
43 throw new \TYPO3\CMS\Core\Cache\Exception\InvalidDataException('Given data is of type "' . gettype($string) . '", but a string is expected for string cache.', 1222808333);
44 }
45 foreach ($tags as $tag) {
46 if (!$this->isValidTag($tag)) {
47 throw new \InvalidArgumentException('"' . $tag . '" is not a valid tag for a cache entry.', 1233057512);
48 }
49 }
50 $this->backend->set($entryIdentifier, $string, $tags, $lifetime);
51 }
52
53 /**
54 * Finds and returns a variable value from the cache.
55 *
56 * @param string $entryIdentifier Identifier of the cache entry to fetch
57 * @return string The value
58 * @throws \InvalidArgumentException if the cache identifier is not valid
59 * @api
60 */
61 public function get($entryIdentifier) {
62 if (!$this->isValidEntryIdentifier($entryIdentifier)) {
63 throw new \InvalidArgumentException('"' . $entryIdentifier . '" is not a valid cache entry identifier.', 1233057752);
64 }
65 return $this->backend->get($entryIdentifier);
66 }
67
68 /**
69 * Finds and returns all cache entries which are tagged by the specified tag.
70 *
71 * @param string $tag The tag to search for
72 * @return array An array with the content of all matching entries. An empty array if no entries matched
73 * @throws \InvalidArgumentException if the tag is not valid
74 * @api
75 */
76 public function getByTag($tag) {
77 if (!$this->isValidTag($tag)) {
78 throw new \InvalidArgumentException('"' . $tag . '" is not a valid tag for a cache entry.', 1233057772);
79 }
80 $entries = array();
81 $identifiers = $this->backend->findIdentifiersByTag($tag);
82 foreach ($identifiers as $identifier) {
83 $entries[] = $this->backend->get($identifier);
84 }
85 return $entries;
86 }
87
88 }