|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Flagsmith; |
| 4 | + |
| 5 | +use Psr\SimpleCache\CacheInterface; |
| 6 | + |
| 7 | +/** |
| 8 | + * This class is a wrapper for the PSR-16 cache interface. |
| 9 | + * |
| 10 | + * It exists as an easy way to allow use to set global Prefix and TTL. |
| 11 | + */ |
| 12 | +class Cache |
| 13 | +{ |
| 14 | + private CacheInterface $cache; |
| 15 | + private string $prefix; |
| 16 | + private ?int $ttl = null; |
| 17 | + |
| 18 | + public function __construct( |
| 19 | + CacheInterface $cache, |
| 20 | + string $prefix, |
| 21 | + ?int $ttl = null |
| 22 | + ) { |
| 23 | + $this->cache = $cache; |
| 24 | + $this->prefix = $prefix; |
| 25 | + $this->ttl = $ttl; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time. |
| 30 | + * |
| 31 | + * @param string $key The key of the item to store. |
| 32 | + * @param mixed $value The value of the item to store. Must be serializable. |
| 33 | + * @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
| 34 | + * the driver supports TTL then the library may set a default value |
| 35 | + * for it or let the driver take care of that. |
| 36 | + * |
| 37 | + * @return bool True on success and false on failure. |
| 38 | + * |
| 39 | + * @throws \Psr\SimpleCache\InvalidArgumentException |
| 40 | + * MUST be thrown if the $key string is not a legal value. |
| 41 | + */ |
| 42 | + public function set(string $key, $value): bool |
| 43 | + { |
| 44 | + return $this->cache->set( |
| 45 | + $this->getKeyWithPrefix($key), |
| 46 | + $value, |
| 47 | + $this->ttl |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Determines whether an item is present in the cache. |
| 53 | + * |
| 54 | + * NOTE: It is recommended that has() is only to be used for cache warming type purposes |
| 55 | + * and not to be used within your live applications operations for get/set, as this method |
| 56 | + * is subject to a race condition where your has() will return true and immediately after, |
| 57 | + * another script can remove it, making the state of your app out of date. |
| 58 | + * |
| 59 | + * @param string $key The cache item key. |
| 60 | + * |
| 61 | + * @return bool |
| 62 | + * |
| 63 | + * @throws \Psr\SimpleCache\InvalidArgumentException |
| 64 | + * MUST be thrown if the $key string is not a legal value. |
| 65 | + */ |
| 66 | + public function has(string $key): bool |
| 67 | + { |
| 68 | + return $this->cache->has($this->getKeyWithPrefix($key)); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Fetches a value from the cache. |
| 73 | + * |
| 74 | + * @param string $key The unique key of this item in the cache. |
| 75 | + * @param mixed $default Default value to return if the key does not exist. |
| 76 | + * |
| 77 | + * @return mixed The value of the item from the cache, or $default in case of cache miss. |
| 78 | + * |
| 79 | + * @throws \Psr\SimpleCache\InvalidArgumentException |
| 80 | + * MUST be thrown if the $key string is not a legal value. |
| 81 | + */ |
| 82 | + public function get(string $key, $default = null) |
| 83 | + { |
| 84 | + return $this->cache->get($this->getKeyWithPrefix($key), $default); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Persists a set of key => value pairs in the cache, with an optional TTL. |
| 89 | + * |
| 90 | + * @param iterable $values A list of key => value pairs for a multiple-set operation. |
| 91 | + * @param null|int|\DateInterval $ttl Optional. The TTL value of this item. If no value is sent and |
| 92 | + * the driver supports TTL then the library may set a default value |
| 93 | + * for it or let the driver take care of that. |
| 94 | + * |
| 95 | + * @return bool True on success and false on failure. |
| 96 | + * |
| 97 | + * @throws \Psr\SimpleCache\InvalidArgumentException |
| 98 | + * MUST be thrown if $values is neither an array nor a Traversable, |
| 99 | + * or if any of the $values are not a legal value. |
| 100 | + */ |
| 101 | + public function setMultiple(array $values): bool |
| 102 | + { |
| 103 | + $newValues = []; |
| 104 | + foreach ($values as $key => $value) { |
| 105 | + $newValues[$this->getKeyWithPrefix($key)] = $value; |
| 106 | + } |
| 107 | + |
| 108 | + return $this->cache->setMultiple($newValues, $this->ttl); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Obtains multiple cache items by their unique keys. |
| 113 | + * |
| 114 | + * @param iterable $keys A list of keys that can obtained in a single operation. |
| 115 | + * @param mixed $default Default value to return for keys that do not exist. |
| 116 | + * |
| 117 | + * @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value. |
| 118 | + * |
| 119 | + * @throws \Psr\SimpleCache\InvalidArgumentException |
| 120 | + * MUST be thrown if $keys is neither an array nor a Traversable, |
| 121 | + * or if any of the $keys are not a legal value. |
| 122 | + */ |
| 123 | + public function getMultiple(array $keys, $default = null) |
| 124 | + { |
| 125 | + return $this->cache->getMultiple( |
| 126 | + array_map(fn($key) => $this->getKeyWithPrefix($key), $keys), |
| 127 | + $default |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Get the full Key name including Prefix |
| 133 | + * |
| 134 | + * @param string $key |
| 135 | + * @return string |
| 136 | + */ |
| 137 | + public function getKeyWithPrefix(string $key): string |
| 138 | + { |
| 139 | + return $this->prefix . '.' . $key; |
| 140 | + } |
| 141 | +} |
0 commit comments