Basic usage
Redis String (String)
string (String) >>
-
Add data: set("country:state:city", "BeiJing")
-
Set only when the key does not exist: setnx($key, $value)
-
Add data: set("country:state:city", "BeiJing")
-
Set only when the key does not exist: setnx($key, $value)
-
Set the value and specify the expiration time: setex($key, $seconds, $value)
-
Get data: get("country:state:city")
-
Set expiration time: expire($key, 3600)
-
Set expiration timestamp: expireat($key, 163253000)
-
Add multiple data: mget($keys)
-
Get multiple data: mset($dictionary)
-
Self-increasing quantity: incrby($key, $num)
Redis list (List)
list (list, queue) >>
- Insert on the left: lpush($key)
- Pop up on the right: rpop($key)
- Get all the values of the list: lrange($key,0,-1)
- Get the number of elements: llen($key)
- Get the value by index index: lindex($key, $index)
Redis hash (Hash)
hash (dictionary, key-value pair) >>
- Add data: hset($key, $field, $value)
- Insert key for multiple field-values: hmset($key, $dictionary)
- Get the value of the specified field: hget($key, $field)
- Get all the fields and values of the specified key: hgetall($key)
- Get the number of fields: hlen($key)
- Get all fields: hkeys($key)
Redis set (Set)
set >>
- Add data: sadd($key, [1,2,3])
- Get all the values of the collection: smembers($key)
- Total acquisition: scard($key)
- Intersection: sinter($key, $key2)
- Difference: sdiff($key, $key2)
- Union: sunion($key, $key2)
- Move: smove($source, $destination, $member)
- Return one or more random numbers: srandmember($key, $count)
- Pop a random value: spop($key, $count)
- Remove the specified member: srem($key, $member)
- Determine whether member exists in the collection: sismember($key, $member)
Novice Tutorial-Redis sorted set
sortdset (sorted set) >>
- Add data: zadd($key,[$member=>'score'])
- Get all the values of the collection: zrange($key, 0, -1)
- Specify the number of members in the score interval: zcount($key, $min, $max)
- Get the total number: zcard($key)
- Get the index of the member: zrank($key,$member)
- Get the score value of the specified member: zscore($key, $member)
- Get the member's score value (from high to low): zrevrange($key, 0,9)
Get the member's score value (specify the score range from high to low): zrevrangebyscore($key, $max, $min)
Redis five data types and application scenarios
Laravel example
Expiration time setting
$redis = \Illuminate\Support\Facades\Redis::connection('default');
$redis->select(5); //选择数据库
$redis->set($key,$value);
$timestamp = mktime(23, 59, 59, date("m"), date("d"), date("Y")); //当天时间戳
$redis->expireAt($key, $timestamp); //设置键的过期时间(具体时间)
$redis->expire($key, $seconds); //设置键的过期时间(秒)
$redis->ttl($key); // 获取过期时间(秒)
List (list, queue)
$key = 'test:';
$redis = \Illuminate\Support\Facades\Redis::connection('default');
$redis->select(5);
$redis->lpush($key.'arr', rand(0, 100)); //左边插入
$redis->rpop($key.'arr'); //右边弹出
$a = $redis->lrange($key.'arr', 0, -1); //获取这个队列里所有的值
dump($a);
There are many uses of redis in ordinary projects, such as caching and spikes. You can refer to this article:
Several common ways and uses of php using redis