Skip to content

Instantly share code, notes, and snippets.

@sangar82
Last active April 24, 2024 08:10
Show Gist options
  • Save sangar82/e83e255da149882582b8 to your computer and use it in GitHub Desktop.
Save sangar82/e83e255da149882582b8 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);
}));
@watermelon-man
Copy link

在laravel里面,如何在redis->set里面设置过期时间呢?只能重新redis->expire(),请指教,谢谢。

@ilyaDegtyarenko
Copy link

在laravel里面,如何在redis->set里面设置过期时间呢?只能重新redis->expire(),请指教,谢谢。

Сам в шоке.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment