Skip to content

Instantly share code, notes, and snippets.

@tournasdim
Created August 21, 2013 16:56
Show Gist options
  • Save tournasdim/6297053 to your computer and use it in GitHub Desktop.
Save tournasdim/6297053 to your computer and use it in GitHub Desktop.
L4 Cache signature (list of most user methods)
<?php
Note that all items stored in the cache are serialized, so you are free to store any type of data
Cache::put('key', 'value', $minutes); // Storing An Item In The Cache
Cache::add('key', 'value', $minutes); // Storing An Item In The Cache If It Doesn't Exist
if (Cache::has('key')) { // } ; // Checking For Existence In Cache
$value = Cache::get('key'); // Retrieving a Cached value
$value = Cache::get('key', 'default'); // set a default value if key doesn't exists
$value = Cache::get('key', function() { return 'default'; }); // set a callback if the key doesn't exists
Cache::forever('key', 'value'); // storing a value permanently
Sometimes you may wish to retrieve an item from the cache, but also store a default value if the requested item doesn't exist :
$value = Cache::remember('users', $minutes, function() { return DB::table('users')->get(); });
$value = Cache::rememberForever('users', function() { return DB::table('users')->get();}); //remember forever
Cache::forget('key'); // Removing An Item From The Cache
Cache::increment('key');
Cache::increment('key', $amount);
Cache::decrement('key');
Cache::decrement('key', $amount);
Note: Cache sections are not supported when using the file or database cache drivers.
Cache::section('people')->put('John', $john);
Cache::section('people')->put('Anne', $anne);
$anne = Cache::section('people')->get('Anne'); // Accessing Items In A Cache Section
Cache::section('people')->flush(); // Flush all items in the section
When using the database cache driver, you will need to setup a table to contain the cache items. Below is an example Schema declaration for the table :
Schema::create('cache', function($table) {
$table->string('key')->unique();
$table->text('value');
$table->integer('expiration');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment