Lines 66.66% 6 / 9
Methods 66.66% 6 / 9
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 store 100.00% 1 / 1 100.00% 1 / 1 1
 get 100.00% 1 / 1 100.00% 1 / 1 1
 set 100.00% 1 / 1 100.00% 1 / 1 1
 delete 100.00% 1 / 1 100.00% 1 / 1 1
 clear 100.00% 1 / 1 100.00% 1 / 1 1
 getMultiple 0.00% 0 / 1 0.00% 0 / 1 2
 setMultiple 0.00% 0 / 1 0.00% 0 / 1 2
 deleteMultiple 0.00% 0 / 1 0.00% 0 / 1 2
 has 100.00% 1 / 1 100.00% 1 / 1 1
18class Cache
19{
20    /**
21     * Get the application's cache store.
22     *
23     * @return CacheInterface The cache store
24     */
25    public static function store(): CacheInterface
26    {
27        return Application::getInstance()->cache();
28    }
29
30    /**
31     * Fetch a value from the cache.
32     *
33     * @param string $key The cache key
34     * @param mixed $default Default value to return on a miss
35     * @return mixed The cached value, or $default on a miss
36     */
37    public static function get(string $key, mixed $default = null): mixed
38    {
39        return self::store()->get($key, $default);
40    }
41
42    /**
43     * Persist a value in the cache.
44     *
45     * @param string $key The cache key
46     * @param mixed $value The value to store
47     * @param null|int|DateInterval $ttl Optional TTL
48     * @return bool True on success, false on failure
49     */
50    public static function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
51    {
52        return self::store()->set($key, $value, $ttl);
53    }
54
55    /**
56     * Delete an item from the cache.
57     *
58     * @param string $key The cache key
59     * @return bool True on success, false on failure
60     */
61    public static function delete(string $key): bool
62    {
63        return self::store()->delete($key);
64    }
65
66    /**
67     * Wipe the entire cache.
68     *
69     * @return bool True on success, false on failure
70     */
71    public static function clear(): bool
72    {
73        return self::store()->clear();
74    }
75
76    /**
77     * Fetch multiple values from the cache.
78     *
79     * @param iterable<string> $keys The cache keys
80     * @param mixed $default Default value for keys that are missing
81     * @return iterable<string, mixed> Key => value pairs
82     */
83    public static function getMultiple(iterable $keys, mixed $default = null): iterable
84    {
85        return self::store()->getMultiple($keys, $default);
86    }
87
88    /**
89     * Persist multiple values in the cache.
90     *
91     * @param iterable $values Key => value pairs to store
92     * @param null|int|DateInterval $ttl Optional TTL
93     * @return bool True on success, false on failure
94     */
95    public static function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
96    {
97        return self::store()->setMultiple($values, $ttl);
98    }
99
100    /**
101     * Delete multiple items from the cache.
102     *
103     * @param iterable<string> $keys The cache keys
104     * @return bool True on success, false on failure
105     */
106    public static function deleteMultiple(iterable $keys): bool
107    {
108        return self::store()->deleteMultiple($keys);
109    }
110
111    /**
112     * Determine whether an item is present in the cache.
113     *
114     * @param string $key The cache key
115     * @return bool True if the item exists and is not expired
116     */
117    public static function has(string $key): bool
118    {
119        return self::store()->has($key);
120    }
121}