Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save watermelon-man/a8ca905b6f2fbf6c41220a6f3ec8aacd to your computer and use it in GitHub Desktop.
Save watermelon-man/a8ca905b6f2fbf6c41220a6f3ec8aacd to your computer and use it in GitHub Desktop.
Redis laravel example
Route::get('redis', array('as' => 'cache', 'do' => function()
{
$redis = Redis::connection();
//STRING
$redis->set('name', 'Taylor');
$name = $redis->get('name');
// LIST
// A list is a series of ordered values. Some of the important commands for interacting with lists are RPUSH, LPUSH, LLEN, LRANGE, LPOP, and RPOP.
$redis->rpush('friends', 'alice');
$redis->rpush('friends', 'tom');
$redis->lpush('friends', 'bob');
$dosprimeros = $redis->lrange('friends', 0,1);
$todos = $redis->lrange('friends', 0,-1);
$cuantos = $redis->llen('friends');
var_dump($todos);
Removes the first count occurrences of elements equal to value from the list stored at key. The count argument influences the operation in the following ways:
count > 0: Remove elements equal to value moving from head to tail.
count < 0: Remove elements equal to value moving from tail to head.
count = 0: Remove all elements equal to value.
For example, LREM list -2 "hello" will remove the last two occurrences of "hello" in the list stored at list.
// SET
// A set is similar to a list, except it does not have a specific order and each element may only appear once.
// Some of the important commands in working with sets are SADD, SREM, SISMEMBER, SMEMBERS and SUNION.
$redis->sadd('superpowers', array('flight', 'run', 'kill'));
$values = $redis->smembers('superpowers');
$existe = $redis->sismember('superpowers','flight'); // => 1
$noexiste = $redis->sismember('superpowers','love'); // => 0
var_dump($values);
//HASH
$test = array(
'name' => 'Iván',
'lastname' => 'Sánchez'
);
$redis->hmset('me', $test);
$me = $redis->hgetall('me');
var_dump($me);
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment