Created
October 14, 2019 17:17
-
-
Save mityukov/8d7ba1b30f49f2c2d1c2a33f6bf8fd53 to your computer and use it in GitHub Desktop.
Runtime cache for Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ... | |
/** | |
* Get / set the specified *runtime* cache value. | |
* | |
* If an array is passed, we'll assume you want to put to the cache. | |
* TLDR: same API as for `cache()` | |
* | |
* @param dynamic key|key,default|null | |
* @return mixed|\Illuminate\Cache\CacheManager | |
* | |
* @throws \Exception | |
*/ | |
function runtime() | |
{ | |
$arguments = func_get_args(); | |
if (empty($arguments)) { | |
return app('cache')->store('array'); | |
} | |
if (is_string($arguments[0])) { | |
return app('cache')->store('array')->get(...$arguments); | |
} | |
if (! is_array($arguments[0])) { | |
throw new Exception( | |
'When setting a value in the cache, you must pass an array of key / value pairs.' | |
); | |
} | |
// 99999 -- is for TTL. Supposedly, unreachable within one process' life. | |
return app('cache')->store('array')->put(key($arguments[0]), reset($arguments[0]), 99999); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment