Redis basic operation instructions and five commonly used data types

Basic operation instructions

function Instructions for use For example show result
select (not case sensitive) Select the database, the default database has 0 to 15 select 0 OK
DBSIZE View the number of current database keys DBSIZE 5
keys View current content keys * Display all key values ​​of the current database
FLUSHDB Clear the number of keys in the current database
FLUSHALL Clear the keys of all libraries
exists key Determine whether the key exists Return 1 if it exists, return 0 if it does not exist
TYPE key Determine the type of key Return the type of key

One, redis-string (single K single V)

Note: string is the basic type of redis, a key corresponds to a value
string can contain any data, the maximum cannot exceed 512M

Function instructions
function Instructions for use For example show result
set Settings set name kk Add a record
get Get value by key get name “Kk”
mset Set multiple values mset name1 mm name2 ll Add 2 records
mget Get multiple values mget name name1 name 2 “kk” “mm” “ll”
append Add field append name mmll "Kkmmll"
of the Delete specified field del name delete
strlen Return string length strlen name1 2
incr The field value needs to be an integer, and the field value +1 is executed once incr int1 1
incrby How much to increase the value of the string incrby int1 2 3
decrby The value of the string determines how much to subtract decrby int 1 3 0
getrange Get the value in the specified interval, if it shows all 0 -1 getrange name 0 -1 "Kkmmll"
setrange From which position to replace setrange name 0 l “Lkmmll”

Two, redis-list (single K and multiple V)

Note: The list is a simple list of strings, sorted in the order of insertion, you can add the head (left) or tail (right) of an element list. Its bottom layer is actually a linked list.
Note: It cannot be inserted into the string type key value

Function instructions

key list2
value ‘1’ ‘2’ ‘3’ ‘4’ ‘5’
下标 0 1 2 3 4

function Instructions for use For example show result
lpush Insert from left, read from left to right, you can insert multiple lpush key value[value] Return the number of inserts
rpush Insert from the right, read from left to right, you can insert multiple rpush key value[value] Return the number of inserts
long Get the specified length, return the current number inserted (or show how many values ​​are in the key) lrange key 0 -1 ‘1’ ‘2’ ‘3’ ‘4’ ‘5’
lpop Remove the leftmost element and return the popped value lpop key 1
rpop Remove the rightmost element and return the popped value rpop key 5
lindex lindex key index; get elements according to index subscript (from top to bottom) lindex key 0 1
llen Find the length of the list and return the length value fill key 5
lrem lrem key N value; delete N Values ​​(delete 2 1), return 1 if deleted, return 0 if none is deleted lrem key 2 1 1
rpoplpush rpoplpush key1 key2; Press the last item in list1 into the first place in list2, and the return result is the last place in list1, which can also be said to be the value inserted into list2 rpoplpush list1 list2
lset lset key index value; replace the first bit in the list key with X lset list2 0 X Return OK
lensed linsert key before/after x php; add field PHP before/after the key field linsert list2 befort x php Returns the length of the current KEY
of the del key; from list2 Return 1 to indicate successful deletion

Three, redis-Hash

Hash is a collection of
key- value pairs The value corresponding to the key is similar to a dictionary in Python

Function instructions
function Instructions for use For example show result
hset hset key filed value; set a hash value hset hm id 11 返回 value中的个数
hget hget key filed;取出一个指定filed的值 hget hm id 返回取出的值
hmset hmset key filed value [filed value]; 设定多个hash值 hmset hm4 id 1 name kk age 18 sex 0 返回OK
hmget hmget key filed[filed] ;取出指定的filed值 hmget hm4 id name ‘1’,‘kk’
hgetall hgetall key;取出所有的内容 hgetall hm4 ‘id’ ‘1’ ‘name’ ‘kk’ ‘age’ ‘18’ ‘sex’ ‘0’
hdel hdel key filed;删除指定filed hdel hm4 id 删除成功返回1
hlen hlen key;显示key对应filed个数 hlen hm4 返回4
hexists hexists key filed;判断key对应的value中的某个值 hexists hm4 id 存在返回1
hkeys hkeys key;显示key中所有的键 hkeys hm4 ‘id’ ‘name’ ‘age’ ‘sex’
hvals hvals key;显示key中所有的值 hkeys hm4 ‘1’ ‘kk’ ‘18’ ‘0’

四、redis-set(不重复序列)

Set(集合)无序集合

函数使用说明
函数 使用说明 举例 显示结果
sadd sadd key member[menber];增加一个无序集合 sadd st 1 2 3 4 5 返回结果为5,增加了5个数
smembers smembers key;显示key中的集合 smembers sadd ‘1’ ‘2’ ‘3’ ‘4’ ‘5’
sismenber sismenber key menber;显示key的集合中指定值是否存在 sismenber st 1 若存在,返回1
scard scard key;显示key的集合里面的元素个数 scard st 5
srem srem key member;删除key中的指定的元素,可删除多个 srem st 5 返回删除参数个数
srandmember srandmember key count;从key中随机出来count个数 srandmember st 1 随机出现一个元素
spop spop key [conut];随机出栈count个元素,默认一个 spop st 2 随机出来2个,原st减少2个,显示随机出来2个
smove smove source destination member;将source中指定的元素移动到destination中 smove st st1 1 显示结果为移动1个
SDIFF SDIFF key [key];差集,显示仅第一个set存在,不在第二个set中存在的值 SDIFF st st1
SINTER SINTER key [key];交集,显示第一个set和第二个set共同存在的元素 SINTER st st1
SUNION SUNION key [key] SUNION st st1;并集,显示一个set和第二个set中除共同值之外的两个set的值

四、redis-Zset

Zset有序集合

函数使用说明
函数 使用说明 举例 显示结果
zadd zadd key [排序的值 元素];增加Zset有序集合 zadd zst 50 v1 60 v2 70 v3 90 v4 80 v5 显示建立的zset中含有几个元素
zrange zrange key start endwithscores[];查看排序后元素的内容加上withscores后,排序值也会显示 zadd zst 0 -1
zrangebyscore zrangebyscore key 开始排序值 结束排序值 [limit] start count;显示根据排序值删选出元素,根据删选出的元素,limit 从第start开始选取count条 zrangebyscore zst 50 90 limit 2 1 显示V3
zcard zcard key;求key的总条数 zcard zst 5
zcount zcount 开始排序值,结束排序值;求开始排序到结束排序的个数 zcount 50 60 2
zrank zrank key 元素;返回元素对应的下标,从0开始 zrank zst v2 1
zrem zrem key 元素;根据元素删除 zrem zst v1 成功返回1

注:zrangebyscore中结束排序值如果前面加( 则表示不包含结束排序值
如:zrangebyscore zset01 50 (70

Guess you like

Origin blog.csdn.net/qq_37697566/article/details/104755092