Skip to content

Instantly share code, notes, and snippets.

@cosmeoes
Created March 22, 2022 15:18
Show Gist options
  • Save cosmeoes/8a7967d37d574a2f5299c92c4dbacbc8 to your computer and use it in GitHub Desktop.
Save cosmeoes/8a7967d37d574a2f5299c92c4dbacbc8 to your computer and use it in GitHub Desktop.
Cache cheat sheet
// Store item for the given seconds
Cache::put('key', 'value', $seconds);
// Store item indefinitely
Cache::put('key', 'value');
// You can also use a DateTime instance
Cache::put('key', 'value', now()->addMinutes(10));
// Store if not present
Cache::add('key', 'value', $seconds);
// Store items forever
Cache::forever('key', 'value');
// Remove item
Cache::forget('key');
// You can remove items by passing a zero or negative expiration
Cache::put('key', 'value', -5);
// Clear entire cache
Cache::flush();
// Get value
$value = Cache::get('key');
// Get value, or 'default' if the key is not present
$value = Cache::get('key', 'default');
// You may pass a that will only get
// executed if the key doesn't exist
$value = Cache::get('key', function () {
return DB::table(...)->get();
});
//Checking for a item existence
if (Cache::has('key')) {
}
// Increment/decrement the value of an integer
Cache::increment('key');
Cache::decrement('key');
// You may pass a value with the amount
Cache::increment('key', $amount);
Cache::decrement('key', $amount);
// Retrieve item or store if it doesnt exists
$value = Cache::remember('users', $seconds, function () {
return DB::table('users')->get();
});
// Same as above but store forever
$value = Cache::rememberForever('users', function () {
return DB::table('users')->get();
});
// Retrieve item and delete it
$value = Cache::pull('key');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment