subjective-php /
psr-cache-redis
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SubjectivePHP\Psr\SimpleCache; |
||
| 4 | |||
| 5 | use SubjectivePHP\Psr\SimpleCache\Serializer\BasicSerializer; |
||
| 6 | use SubjectivePHP\Psr\SimpleCache\Serializer\SerializerInterface; |
||
| 7 | use DateInterval; |
||
| 8 | use DateTime; |
||
| 9 | use Predis\ClientInterface; |
||
| 10 | use Psr\SimpleCache\CacheInterface; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * A PSR-16 implementation which stores data in a RedisDB collection. |
||
| 14 | */ |
||
| 15 | final class RedisCache implements CacheInterface |
||
| 16 | { |
||
| 17 | use KeyValidatorTrait; |
||
| 18 | use TTLValidatorTrait; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Redis client for storing cache. |
||
| 22 | * |
||
| 23 | * @var ClientInterface |
||
| 24 | */ |
||
| 25 | private $client; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The object responsible for serializing data to and from redis values. |
||
| 29 | * |
||
| 30 | * @var SerializerInterface |
||
| 31 | */ |
||
| 32 | private $serializer; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Construct a new instance of RedisCache. |
||
| 36 | * |
||
| 37 | * @param ClientInterface $client Redis client for storing cache. |
||
| 38 | * @param SerializerInterface $serializer Object responsible for serializing data to and from redis values. |
||
| 39 | */ |
||
| 40 | public function __construct(ClientInterface $client, SerializerInterface $serializer = null) |
||
| 41 | { |
||
| 42 | $this->client = $client; |
||
| 43 | $this->serializer = $serializer ?? new BasicSerializer(); |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Fetches a value from the cache. |
||
| 48 | * |
||
| 49 | * @param string $key The unique key of this item in the cache. |
||
| 50 | * @param mixed $default Default value to return if the key does not exist. |
||
| 51 | * |
||
| 52 | * @return mixed The value of the item from the cache, or $default in case of cache miss. |
||
| 53 | * |
||
| 54 | * @throws InvalidArgumentException Thrown if the $key string is not a legal value. |
||
| 55 | */ |
||
| 56 | public function get($key, $default = null)//@codingStandardsIgnoreLine Interface does not define type-hints or return |
||
| 57 | { |
||
| 58 | $this->validateKey($key); |
||
| 59 | |||
| 60 | $cached = $this->client->get($key); |
||
| 61 | if ($cached !== null) { |
||
| 62 | return $this->serializer->unserialize($cached); |
||
| 63 | } |
||
| 64 | |||
| 65 | return $default; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Obtains multiple cache items by their unique keys. |
||
| 70 | * |
||
| 71 | * @param iterable $keys A list of keys that can obtained in a single operation. |
||
| 72 | * @param mixed $default Default value to return for keys that do not exist. |
||
| 73 | * |
||
| 74 | * @return array List of key => value pairs. Cache keys that do not exist or are stale will have $default as value. |
||
| 75 | * |
||
| 76 | * @throws InvalidArgumentException Thrown if the $key string is not a legal value. |
||
| 77 | */ |
||
| 78 | public function getMultiple($keys, $default = null)//@codingStandardsIgnoreLine Interface does not define type-hints or return |
||
| 79 | { |
||
| 80 | $result = []; |
||
| 81 | foreach ($keys as $key) { |
||
| 82 | $result[$key] = $this->get($key, $default); |
||
| 83 | } |
||
| 84 | |||
| 85 | return $result; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
||
| 90 | * |
||
| 91 | * @param string $key The key of the item to store. |
||
| 92 | * @param mixed $value The value of the item to store, must be serializable. |
||
| 93 | * @param null|integer|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
||
| 94 | * the driver supports TTL then the library may set a default value |
||
| 95 | * for it or let the driver take care of that. |
||
| 96 | * |
||
| 97 | * @return boolean True on success and false on failure. |
||
| 98 | * |
||
| 99 | * @throws InvalidArgumentException Thrown if the $key string is not a legal value. |
||
| 100 | */ |
||
| 101 | public function set($key, $value, $ttl = null)//@codingStandardsIgnoreLine Interface does not define type-hints or return |
||
| 102 | { |
||
| 103 | $this->validateKey($key); |
||
| 104 | $this->validateTTL($ttl); |
||
| 105 | if ($this->client->set($key, $this->serializer->serialize($value))->getPayload() !== 'OK') { |
||
| 106 | return false; |
||
| 107 | } |
||
| 108 | |||
| 109 | $this->setExpires($key, $ttl); |
||
| 110 | |||
| 111 | return true; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Persists a set of key => value pairs in the cache, with an optional TTL. |
||
| 116 | * |
||
| 117 | * @param iterable $values A list of key => value pairs for a multiple-set operation. |
||
| 118 | * @param null|integer|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
||
| 119 | * the driver supports TTL then the library may set a default value |
||
| 120 | * for it or let the driver take care of that. |
||
| 121 | * |
||
| 122 | * @return boolean True on success and false on failure. |
||
| 123 | * |
||
| 124 | * @throws InvalidArgumentException Thrown if $values is neither an array nor a Traversable, |
||
| 125 | * or if any of the $values are not a legal value. |
||
| 126 | */ |
||
| 127 | public function setMultiple($values, $ttl = null)//@codingStandardsIgnoreLine Interface does not define type-hints or return |
||
| 128 | { |
||
| 129 | $this->validateKeys(array_keys($values)); |
||
| 130 | $this->validateTTL($ttl); |
||
| 131 | foreach ($values as $key => $value) { |
||
| 132 | if (!$this->set($key, $value, $ttl)) { |
||
| 133 | return false; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | return true; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Delete an item from the cache by its unique key. |
||
| 142 | * |
||
| 143 | * @param string $key The unique cache key of the item to delete. |
||
| 144 | * |
||
| 145 | * @return boolean True if the item was successfully removed. False if there was an error. |
||
| 146 | * |
||
| 147 | * @throws InvalidArgumentException Thrown if the $key string is not a legal value. |
||
| 148 | */ |
||
| 149 | public function delete($key)//@codingStandardsIgnoreLine Interface does not define type-hints or return |
||
| 150 | { |
||
| 151 | return $this->deleteMultiple([$key]); |
||
|
0 ignored issues
–
show
|
|||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Deletes multiple cache items in a single operation. |
||
| 156 | * |
||
| 157 | * @param iterable $keys A list of string-based keys to be deleted. |
||
| 158 | * |
||
| 159 | * @return boolean True if the items were successfully removed. False if there was an error. |
||
| 160 | * |
||
| 161 | * @throws InvalidArgumentException Thrown if $keys is neither an array nor a Traversable, |
||
| 162 | * or if any of the $keys are not a legal value. |
||
| 163 | */ |
||
| 164 | public function deleteMultiple($keys)//@codingStandardsIgnoreLine Interface does not define type-hints |
||
| 165 | { |
||
| 166 | $this->validateKeys($keys); |
||
| 167 | return $this->client->del($keys) === count($keys); |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Wipes clean the entire cache's keys. |
||
| 172 | * |
||
| 173 | * @return boolean True on success and false on failure. |
||
| 174 | */ |
||
| 175 | public function clear()//@codingStandardsIgnoreLine Interface does not define type-hints or return |
||
| 176 | { |
||
| 177 | $this->client->flushall(); |
||
| 178 | return true; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Determines whether an item is present in the cache. |
||
| 183 | * |
||
| 184 | * NOTE: It is recommended that has() is only to be used for cache warming type purposes |
||
| 185 | * and not to be used within your live applications operations for get/set, as this method |
||
| 186 | * is subject to a race condition where your has() will return true and immediately after, |
||
| 187 | * another script can remove it making the state of your app out of date. |
||
| 188 | * |
||
| 189 | * @param string $key The cache item key. |
||
| 190 | * |
||
| 191 | * @return boolean |
||
| 192 | * |
||
| 193 | * @throws InvalidArgumentException Thrown if the $key string is not a legal value. |
||
| 194 | */ |
||
| 195 | public function has($key) //@codingStandardsIgnoreLine Interface does not define type-hints |
||
| 196 | { |
||
| 197 | $this->validateKey($key); |
||
| 198 | return (bool)$this->client->exists($key); |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Converts the given time to live value to a DataTime instance; |
||
| 203 | * |
||
| 204 | * @param string $key The cache item key. |
||
| 205 | * @param mixed $ttl The time-to-live value to validate. |
||
| 206 | * |
||
| 207 | * @return void |
||
| 208 | */ |
||
| 209 | private function setExpires(string $key, $ttl) |
||
| 210 | { |
||
| 211 | if ($ttl === null) { |
||
| 212 | return; |
||
| 213 | } |
||
| 214 | |||
| 215 | if (is_int($ttl)) { |
||
| 216 | $ttl = DateInterval::createFromDateString("{$ttl} seconds"); |
||
| 217 | } |
||
| 218 | |||
| 219 | $this->client->expireat($key, (new DateTime())->add($ttl)->getTimestamp()); |
||
| 220 | } |
||
| 221 | } |
||
| 222 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: