|
1 <?php |
|
2 |
|
3 /** |
|
4 * SimplePie |
|
5 * |
|
6 * A PHP-Based RSS and Atom Feed Framework. |
|
7 * Takes the hard work out of managing a complete RSS/Atom solution. |
|
8 * |
|
9 * Copyright (c) 2004-2022, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors |
|
10 * All rights reserved. |
|
11 * |
|
12 * Redistribution and use in source and binary forms, with or without modification, are |
|
13 * permitted provided that the following conditions are met: |
|
14 * |
|
15 * * Redistributions of source code must retain the above copyright notice, this list of |
|
16 * conditions and the following disclaimer. |
|
17 * |
|
18 * * Redistributions in binary form must reproduce the above copyright notice, this list |
|
19 * of conditions and the following disclaimer in the documentation and/or other materials |
|
20 * provided with the distribution. |
|
21 * |
|
22 * * Neither the name of the SimplePie Team nor the names of its contributors may be used |
|
23 * to endorse or promote products derived from this software without specific prior |
|
24 * written permission. |
|
25 * |
|
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS |
|
27 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
|
28 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS |
|
29 * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|
33 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
34 * POSSIBILITY OF SUCH DAMAGE. |
|
35 * |
|
36 * @package SimplePie |
|
37 * @copyright 2004-2022 Ryan Parman, Sam Sneddon, Ryan McCue |
|
38 * @author Ryan Parman |
|
39 * @author Sam Sneddon |
|
40 * @author Ryan McCue |
|
41 * @link http://simplepie.org/ SimplePie |
|
42 * @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
43 */ |
|
44 |
|
45 namespace SimplePie\Cache; |
|
46 |
|
47 use Psr\SimpleCache\CacheInterface; |
|
48 use Psr\SimpleCache\InvalidArgumentException; |
|
49 |
|
50 /** |
|
51 * Caches data into a PSR-16 cache implementation |
|
52 * |
|
53 * @package SimplePie |
|
54 * @subpackage Caching |
|
55 * @internal |
|
56 */ |
|
57 final class Psr16 implements DataCache |
|
58 { |
|
59 /** |
|
60 * PSR-16 cache implementation |
|
61 * |
|
62 * @var CacheInterface |
|
63 */ |
|
64 private $cache; |
|
65 |
|
66 /** |
|
67 * PSR-16 cache implementation |
|
68 * |
|
69 * @param CacheInterface $cache |
|
70 */ |
|
71 public function __construct(CacheInterface $cache) |
|
72 { |
|
73 $this->cache = $cache; |
|
74 } |
|
75 |
|
76 /** |
|
77 * Fetches a value from the cache. |
|
78 * |
|
79 * Equivalent to \Psr\SimpleCache\CacheInterface::get() |
|
80 * <code> |
|
81 * public function get(string $key, mixed $default = null): mixed; |
|
82 * </code> |
|
83 * |
|
84 * @param string $key The unique key of this item in the cache. |
|
85 * @param mixed $default Default value to return if the key does not exist. |
|
86 * |
|
87 * @return array|mixed The value of the item from the cache, or $default in case of cache miss. |
|
88 * |
|
89 * @throws InvalidArgumentException |
|
90 * MUST be thrown if the $key string is not a legal value. |
|
91 */ |
|
92 public function get_data(string $key, $default = null) |
|
93 { |
|
94 $data = $this->cache->get($key, $default); |
|
95 |
|
96 if (!is_array($data) || $data === $default) { |
|
97 return $default; |
|
98 } |
|
99 |
|
100 return $data; |
|
101 } |
|
102 |
|
103 /** |
|
104 * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
|
105 * |
|
106 * Equivalent to \Psr\SimpleCache\CacheInterface::set() |
|
107 * <code> |
|
108 * public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool; |
|
109 * </code> |
|
110 * |
|
111 * @param string $key The key of the item to store. |
|
112 * @param array $value The value of the item to store, must be serializable. |
|
113 * @param null|int $ttl Optional. The TTL value of this item. If no value is sent and |
|
114 * the driver supports TTL then the library may set a default value |
|
115 * for it or let the driver take care of that. |
|
116 * |
|
117 * @return bool True on success and false on failure. |
|
118 * |
|
119 * @throws InvalidArgumentException |
|
120 * MUST be thrown if the $key string is not a legal value. |
|
121 */ |
|
122 public function set_data(string $key, array $value, ?int $ttl = null): bool |
|
123 { |
|
124 return $this->cache->set($key, $value, $ttl); |
|
125 } |
|
126 |
|
127 /** |
|
128 * Delete an item from the cache by its unique key. |
|
129 * |
|
130 * Equivalent to \Psr\SimpleCache\CacheInterface::delete() |
|
131 * <code> |
|
132 * public function delete(string $key): bool; |
|
133 * </code> |
|
134 * |
|
135 * @param string $key The unique cache key of the item to delete. |
|
136 * |
|
137 * @return bool True if the item was successfully removed. False if there was an error. |
|
138 * |
|
139 * @throws InvalidArgumentException |
|
140 * MUST be thrown if the $key string is not a legal value. |
|
141 */ |
|
142 public function delete_data(string $key): bool |
|
143 { |
|
144 return $this->cache->delete($key); |
|
145 } |
|
146 } |