|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Classes; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\Cache as CoreCache; |
| 6 | + |
| 7 | +class Cache extends CoreCache |
| 8 | +{ |
| 9 | + protected static function applyPrefix( $key ) |
| 10 | + { |
| 11 | + return Hook::filter( 'ns-cache-prefix', $key ); |
| 12 | + } |
| 13 | + |
| 14 | + /** |
| 15 | + * Check if a key exists in the cache. |
| 16 | + * |
| 17 | + * @param string $key |
| 18 | + * @return bool |
| 19 | + */ |
| 20 | + public static function has( $key ) |
| 21 | + { |
| 22 | + $key = self::applyPrefix( $key ); |
| 23 | + |
| 24 | + return parent::has( $key ); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Check if a key is missing from the cache. |
| 29 | + * |
| 30 | + * @param string $key |
| 31 | + * @return bool |
| 32 | + */ |
| 33 | + public static function missing( $key ) |
| 34 | + { |
| 35 | + $key = self::applyPrefix( $key ); |
| 36 | + |
| 37 | + return parent::missing( $key ); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Retrieve a value from the cache. |
| 42 | + * |
| 43 | + * @param string $key |
| 44 | + * @param mixed $default |
| 45 | + * @return mixed |
| 46 | + */ |
| 47 | + public static function get( $key, $default = null ) |
| 48 | + { |
| 49 | + $key = self::applyPrefix( $key ); |
| 50 | + |
| 51 | + return parent::get( $key, $default ); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Retrieve multiple values from the cache. |
| 56 | + * |
| 57 | + * @param array $keys |
| 58 | + * @param mixed $default |
| 59 | + * @return array |
| 60 | + */ |
| 61 | + public static function getMultiple( $keys, $default = null ) |
| 62 | + { |
| 63 | + $keys = array_map( [self::class, 'applyPrefix'], $keys ); |
| 64 | + |
| 65 | + return parent::getMultiple( $keys, $default ); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Retrieve and delete a value from the cache. |
| 70 | + * |
| 71 | + * @param string $key |
| 72 | + * @param mixed $default |
| 73 | + * @return mixed |
| 74 | + */ |
| 75 | + public static function pull( $key, $default = null ) |
| 76 | + { |
| 77 | + $key = self::applyPrefix( $key ); |
| 78 | + |
| 79 | + return parent::pull( $key, $default ); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Store a value in the cache for a given time-to-live (TTL). |
| 84 | + * |
| 85 | + * @param string $key |
| 86 | + * @param mixed $value |
| 87 | + * @param int|null $ttl |
| 88 | + * @return bool |
| 89 | + */ |
| 90 | + public static function put( $key, $value, $ttl = null ) |
| 91 | + { |
| 92 | + $key = self::applyPrefix( $key ); |
| 93 | + |
| 94 | + return parent::put ( $key, $value, $ttl ); |
| 95 | + } |
| 96 | + |
| 97 | + public static function set( $key, $value, $ttl = null ) |
| 98 | + { |
| 99 | + $key = self::applyPrefix( $key ); |
| 100 | + |
| 101 | + return parent::set( $key, $value, $ttl ); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Store multiple key-value pairs in the cache for a given time-to-live (TTL). |
| 106 | + * |
| 107 | + * @param array $values |
| 108 | + * @param int|null $ttl |
| 109 | + * @return bool |
| 110 | + */ |
| 111 | + public static function putMany( array $values, $ttl = null ) |
| 112 | + { |
| 113 | + $prefixedValues = []; |
| 114 | + foreach ( $values as $key => $value ) { |
| 115 | + $prefixedValues[self::applyPrefix( $key )] = $value; |
| 116 | + } |
| 117 | + |
| 118 | + return parent::putMany( $prefixedValues, $ttl ); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Store multiple key-value pairs in the cache indefinitely. |
| 123 | + * |
| 124 | + * @param array $values |
| 125 | + * @param int|null $ttl |
| 126 | + * @return bool |
| 127 | + */ |
| 128 | + public static function setMultiple( $values, $ttl = null ) |
| 129 | + { |
| 130 | + $prefixedValues = []; |
| 131 | + foreach ( $values as $key => $value ) { |
| 132 | + $prefixedValues[self::applyPrefix( $key )] = $value; |
| 133 | + } |
| 134 | + |
| 135 | + return parent::setMultiple( $prefixedValues, $ttl ); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Add a value to the cache if it does not already exist. |
| 140 | + * |
| 141 | + * @param string $key |
| 142 | + * @param mixed $value |
| 143 | + * @param int|null $ttl |
| 144 | + * @return bool |
| 145 | + */ |
| 146 | + public static function add( $key, $value, $ttl = null ) |
| 147 | + { |
| 148 | + $key = self::applyPrefix( $key ); |
| 149 | + |
| 150 | + return parent::add( $key, $value, $ttl ); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Increment a value in the cache. |
| 155 | + * |
| 156 | + * @param string $key |
| 157 | + * @param int $value |
| 158 | + * @return int|bool |
| 159 | + */ |
| 160 | + public static function increment( $key, $value = 1 ) |
| 161 | + { |
| 162 | + $key = self::applyPrefix( $key ); |
| 163 | + |
| 164 | + return parent::increment( $key, $value ); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Decrement a value in the cache. |
| 169 | + * |
| 170 | + * @param string $key |
| 171 | + * @param int $value |
| 172 | + * @return int|bool |
| 173 | + */ |
| 174 | + public static function decrement( $key, $value = 1 ) |
| 175 | + { |
| 176 | + $key = self::applyPrefix( $key ); |
| 177 | + |
| 178 | + return parent::decrement( $key, $value ); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Store a value in the cache indefinitely. |
| 183 | + * |
| 184 | + * @param string $key |
| 185 | + * @param mixed $value |
| 186 | + * @return bool |
| 187 | + */ |
| 188 | + public static function forever( $key, $value ) |
| 189 | + { |
| 190 | + $key = self::applyPrefix( $key ); |
| 191 | + |
| 192 | + return parent::forever( $key, $value ); |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Remember a value in the cache for a given time-to-live (TTL). |
| 197 | + * |
| 198 | + * @param string $key |
| 199 | + * @param int $ttl |
| 200 | + * @param callable $callback |
| 201 | + * @return mixed |
| 202 | + */ |
| 203 | + public static function remember( $key, $ttl, $callback ) |
| 204 | + { |
| 205 | + $key = self::applyPrefix( $key ); |
| 206 | + |
| 207 | + return parent::remember( $key, $ttl, $callback ); |
| 208 | + } |
| 209 | + |
| 210 | + /** |
| 211 | + * Remember a value in the cache indefinitely. |
| 212 | + * |
| 213 | + * @param string $key |
| 214 | + * @param callable $callback |
| 215 | + * @return mixed |
| 216 | + */ |
| 217 | + public static function rememberForever( $key, $callback ) |
| 218 | + { |
| 219 | + $key = self::applyPrefix( $key ); |
| 220 | + |
| 221 | + return parent::rememberForever( $key, $callback ); |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * Forget a value in the cache. |
| 226 | + * |
| 227 | + * @param string $key |
| 228 | + * @return bool |
| 229 | + */ |
| 230 | + public static function forget( $key ) |
| 231 | + { |
| 232 | + $key = self::applyPrefix( $key ); |
| 233 | + |
| 234 | + return parent::forget( $key ); |
| 235 | + } |
| 236 | + |
| 237 | + /** |
| 238 | + * Delete a value from the cache. |
| 239 | + * |
| 240 | + * @param string $key |
| 241 | + * @return bool |
| 242 | + */ |
| 243 | + public static function delete( $key ) |
| 244 | + { |
| 245 | + $key = self::applyPrefix( $key ); |
| 246 | + |
| 247 | + return parent::delete( $key ); |
| 248 | + } |
| 249 | + |
| 250 | + /** |
| 251 | + * Delete multiple values from the cache. |
| 252 | + * |
| 253 | + * @param array $keys |
| 254 | + * @return bool |
| 255 | + */ |
| 256 | + public static function deleteMultiple( $keys ) |
| 257 | + { |
| 258 | + $keys = array_map( [self::class, 'applyPrefix'], $keys ); |
| 259 | + |
| 260 | + return parent::deleteMultiple( $keys ); |
| 261 | + } |
| 262 | +} |
0 commit comments