Redis data types

string (string)

set key value
add data
get key
read data

Written under Linux operating system

127.0.0.1:1715> set num 100
127.0.0.1:1715> get num

incr key
increase (increment abbreviation, only +1 each time by default)

127.0.0.1:1715> incr num 

incrBy key value
to increase multiple quantities

127.0.0.1:1715> incrBy num 100

Decr key
decrement (default -1 each time)

127.0.0.1:1715> decr num 

decrBy key decrement number
Decrement the specified amount

127.0.0.1:1715> decrBy num 10

append The content to append to the variable name The content
to append to the string

127.0.0.1:1715> append name world

delete key
delete

127.0.0.1:1715> delete num 

expire key time length/second
Set the validity period of the data

127.0.0.1:1715> expire num 10

expireAt('variable name', timestamp)
sets the validity period of the data
strtotime('time')
converts the string into a timestamp

<?php        
    //设置时区
    date_default_timezone_set('PRC');
    //1.实例化redis对象
    $redis = new Redis();
    //2.连接redis服务器
    $result = $redis -> connect('127.0.0.1',1715);
    //3.设置数据的有效期
    echo strtotime("2018-04-25 10:50:00");
    $redis -> expireAt('name',strtotime("2018-04-25 10:50:00"));

exists variable name to
determine whether the variable exists

127.0.0.1:1715> exits name 

linked list (list)

Linked list, a logically continuous storage space (similar to PHP's index array), and the party related to the linked list is basically
image
lpush at the beginning of l

<?php        
    //1.实例化redis对象
    $redis = new Redis();
    //2.连接redis服务器
    $result = $redis -> connect('127.0.0.1',1715);
    //3.
    $redis -> lpush('waiting','one');
    $redis -> lpush('waiting','two');
    $redis -> lpush('waiting','three');

rpop

127.0.0.1:1715> RPOP waiting
"one"

rpush
lpop
lpush, rpop and rpush, lpop are commonly used to generate task queues, first come first served (FIFO). Usually, when generating an order, a linked list method is used to generate
the index of the starting index of the task queue lrange variable name and the ending index
Get the element of the specified index range (if the ending index is -1, it means the last one)

127.0.0.1:1715> LRANGE waiting 0 1
1) "two"
2) "one"
127.0.0.1:1715> LRANGE waiting 0 -1
1) "three"
2) "two"
3) "one"

llen variable name
length

127.0.0.1:1715> llen waiting 
(integer) 3

lset variable name index element
Set the element at a certain index

127.0.0.1:1715> lset waiting 0 first
OK

lrem variable name index element
delete the specified element

127.0.0.1:1715> lrem waiting 0 first
(integer) 1

Hash table (hash)

Store data in the form of key-value pairs (similar to associative arrays in PHP), and are often used to store data with associated relationships. The methods related to the hash table are all starting with h
hset variable name key value
to set a key

127.0.0.1:1715> hset people name David
(integer) 1

hget variable name key
Get a key

127.0.0.1:1715> hget people name 

hlen variable name
number of elements

127.0.0.1:1715> hlen people 

hdel variable name key
delete an element

127.0.0.1:1715> hden people name

hexists variable name key
whether an element exists

127.0.0.1:1715> hexists people name

set

Collection-type data has two characteristics:

  • Members have no order, no order
  • member only

Methods related to collection types, starting with s,
sadd member value
to add members

127.0.0.1:1715> sadd hobby swim

smembers member
Get all members in the collection, case-insensitive (use lowercase 0 when calling in php)

127.0.0.1:1715> smembers hobby 

spop members
pop members

127.0.0.1:1715> spop hobby 
"swim"

srem member
delete member

127.0.0.1:1715> srem hobby 
(integer) 1

scard members
number of members

127.0.0.1:1715> scard hobby 
(integer) 0

sorted-set

Suitable for developing leaderboard-type applications
zadd variable name value member
Add members, and specify the score at the same time

127.0.0.1:1715> zadd rank 5 baidu 
(integer) 1

zincrBy variable name member
Increment member's score

127.0.0.1:1715> zincrBy rank 5 baidu 
(integer) 1

zScore variable name member
Get member's score

127.0.0.1:1715> zScore rank baidu 

zrange variable name minimum score maximum score
According to the score, return the members of the specified range

127.0.0.1:1715> zrange rank 0 -1 

zRevRange variable name
reverse order (descending score)

127.0.0.1:1715> zRevRange rank 

zRangeByScore variable name maximum score minimum score
score filter member

127.0.0.1:1715> zRangebyScore rank 5 1 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324854659&siteId=291194637