Use of PHP redis

Basic usage

Redis String (String)
string (String) >>

  1. Add data: set("country:state:city", "BeiJing")

  2. Set only when the key does not exist: setnx($key, $value)

  3. Add data: set("country:state:city", "BeiJing")

  4. Set only when the key does not exist: setnx($key, $value)

  5. Set the value and specify the expiration time: setex($key, $seconds, $value)

  6. Get data: get("country:state:city")

  7. Set expiration time: expire($key, 3600)

  8. Set expiration timestamp: expireat($key, 163253000)

  9. Add multiple data: mget($keys)

  10. Get multiple data: mset($dictionary)

  11. Self-increasing quantity: incrby($key, $num)

Redis list (List)
list (list, queue) >>

  1. Insert on the left: lpush($key)
  2. Pop up on the right: rpop($key)
  3. Get all the values ​​of the list: lrange($key,0,-1)
  4. Get the number of elements: llen($key)
  5. Get the value by index index: lindex($key, $index)

Redis hash (Hash)
hash (dictionary, key-value pair) >>

  1. Add data: hset($key, $field, $value)
  2. Insert key for multiple field-values: hmset($key, $dictionary)
  3. Get the value of the specified field: hget($key, $field)
  4. Get all the fields and values ​​of the specified key: hgetall($key)
  5. Get the number of fields: hlen($key)
  6. Get all fields: hkeys($key)

Redis set (Set)
set >>

  1. Add data: sadd($key, [1,2,3])
  2. Get all the values ​​of the collection: smembers($key)
  3. Total acquisition: scard($key)
  4. Intersection: sinter($key, $key2)
  5. Difference: sdiff($key, $key2)
  6. Union: sunion($key, $key2)
  7. Move: smove($source, $destination, $member)
  8. Return one or more random numbers: srandmember($key, $count)
  9. Pop a random value: spop($key, $count)
  10. Remove the specified member: srem($key, $member)
  11. Determine whether member exists in the collection: sismember($key, $member)

Novice Tutorial-Redis sorted set
sortdset (sorted set) >>

  1. Add data: zadd($key,[$member=>'score'])
  2. Get all the values ​​of the collection: zrange($key, 0, -1)
  3. Specify the number of members in the score interval: zcount($key, $min, $max)
  4. Get the total number: zcard($key)
  5. Get the index of the member: zrank($key,$member)
  6. Get the score value of the specified member: zscore($key, $member)
  7. 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

Guess you like

Origin blog.csdn.net/qq_39004843/article/details/108237037