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