[Redis] Detailed explanation of five basic types of commands

Hello, everyone, I am a pig who is dominated by cabbage.

A person who loves to study, sleepless and forgets to eat, is obsessed with the girl's chic, calm and indifferent coding handsome boy.

Basic data type

Looking at Redis Chinese official website, we know that Redis has five data types, and we will introduce them one by one through commands below.
Insert picture description here

(1) Strings

1. Set value, get value, append value, get string length

127.0.0.1:6379> ping #检查是否连接成功
PONG
127.0.0.1:6379> set name nihao #设置值
OK
127.0.0.1:6379> get name #获得值
"nihao"
127.0.0.1:6379> set age 19
OK
127.0.0.1:6379> keys * #获取所有的key
1) "name"
2) "age"
127.0.0.1:6379> EXISTS name #判断一个key是否存在
(integer) 1
127.0.0.1:6379> APPEND name ,dashuaige #追加字符串,假如key不存在,则相当于set key
(integer) 15
127.0.0.1:6379> get name
"nihao,dashuaige"
127.0.0.1:6379> STRLEN name #查看字符串的长度
(integer) 15

2. Self-increasing and self-decreasing

In the WeChat public account, there is a number of article views. This number does not change the value in the database every time you click, which will put too much pressure on database access. Instead, it will be stored in the cache and written to the database at regular intervals. .

127.0.0.1:6379> set views 0 #设置初始浏览量0
OK
127.0.0.1:6379> INCR views #使用INCR名来实现+1
(integer) 1
127.0.0.1:6379> INCR views
(integer) 2
127.0.0.1:6379> get views
"2"
127.0.0.1:6379> DECR views #使用DECR来实现-1
(integer) 1
127.0.0.1:6379> get views
"1"
127.0.0.1:6379> INCRBY views 10 #使用INCRBY来指定步长
(integer) 11
127.0.0.1:6379> get views
"11"
127.0.0.1:6379> DECRBY views 2
(integer) 9
127.0.0.1:6379> get views
"9"
127.0.0.1:6379> 

3. String range getrange [x,x]

It is different from Java's substring, which is left closed and right opened, that is, (x, x).

127.0.0.1:6379> get name
"nihao,dashuaige"
127.0.0.1:6379> GETRANGE name 0 2 #通过getrange来获得指定范围的值
"nih"
127.0.0.1:6379> GETRANGE name 0 -1 #最后一个索引为-1,来获取全部的值
"nihao,dashuaige"
127.0.0.1:6379> SETRANGE name 0 xx #使用setrange来替换相应位置的值
(integer) 15
127.0.0.1:6379> get name
"xxhao,dashuaige"

4、setex,setnx

setex (set with expire) set the expiration time, specify when to expire
setnx (set if not exist) does not exist in the setting, if it exists, the creation fails

127.0.0.1:6379> SETEX key1 5 wohuiguoqi #设置过期时间
OK
127.0.0.1:6379> ttl key1 #查看倒计时
(integer) 1
127.0.0.1:6379> get key1
(nil)
127.0.0.1:6379> setnx key1 nihao
(integer) 1
127.0.0.1:6379> get key1
"nihao"
127.0.0.1:6379> setnx key1 hhh #key1已存在,则创建失败
(integer) 0
127.0.0.1:6379> get key1 #这里获取key1的值,还是第一次设置的值
"nihao"

5、mset,mget

Mset sets multiple key-values ​​at the same time, mget obtains multiple key values ​​at the same time

127.0.0.1:6379> mset key1 value1 key2 value2 key3 value3
OK
127.0.0.1:6379> keys *
1) "key2"
2) "key1"
3) "key3"
127.0.0.1:6379> mget key1 key2 key3
1) "value1"
2) "value2"
3) "value3"

Object: Regard user:1:name as key

127.0.0.1:6379> set user:1:name ljl
OK
127.0.0.1:6379> get user
(nil)
127.0.0.1:6379> get user:1
(nil)
127.0.0.1:6379> get user:1:name
"ljl"

6 、 getset

First get the value before setting the value, if there is a value, get the original value first and then set the new value

127.0.0.1:6379> getset name nihao
(nil)
127.0.0.1:6379> get name
"nihao"
127.0.0.1:6379> getset name dashaibi
"nihao"
127.0.0.1:6379> get name
"dashaibi"
127.0.0.1:6379> 

Conclusion: String can be a string or a number. It can be used in a wide range of scenarios. It can be used as a counter, count the number of fans, and can also be stored in an object cache.

(2) Lists

The list here can be regarded as a queue, a stack... It can be inserted from the right or from the left. The commands for operating the list generally start with l, and Redis is not case-sensitive.
Insert picture description here
Let me get familiar with the simple operation of the list, add data, remove data, get data of a certain index...

1、lpush、lpop、lrange

127.0.0.1:6379> clear
127.0.0.1:6379> LPUSH list nihao1 #从左边添加元素
(integer) 1
127.0.0.1:6379> LPUSH list nihao2
(integer) 2
127.0.0.1:6379> lpush list nihao3
(integer) 3
127.0.0.1:6379> LRANGE list 0 -1 #获取所有元素
1) "nihao3"
2) "nihao2"
3) "nihao1"
127.0.0.1:6379> LPOP list #从左边弹出元素
"nihao3"
127.0.0.1:6379> LRANGE list 0 -1
1) "nihao2"
2) "nihao1"
127.0.0.1:6379> RPOP list #从右边弹出元素
"nihao1"
127.0.0.1:6379> LRANGE list 0 -1
1) "nihao2"

We found that there is a direction when adding elements like a list, and then we can imagine that the horizontal picture above it is erected and it becomes a queue. You see, when nihao1 was added first, it was found that the serial number was 3.
Insert picture description here

2、lindex

To get the value by index, you will find that without rindex, the value by index can only be obtained by lindex. You will also find that the l here not only refers to the l of the list, but also the l of the left, you just put The above picture is enough to keep in mind, and its microcosm starts from 0, not from 1.

127.0.0.1:6379> lpush list one
(integer) 1
127.0.0.1:6379> lpush list two
(integer) 2
127.0.0.1:6379> lpush list three
(integer) 3
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> LINDEX list 1 #从左边开始数第二个数
"two"
127.0.0.1:6379> lindex list 0
"three"
127.0.0.1:6379> rindex list 3

3 、 llen

length length, llen is to check the length in the list

127.0.0.1:6379> llen list
(integer) 3
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> 

4 r lrem

Remove element (remove)

127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> LREM list 2 three #假如有多个相同的值,2代表要移除几个相同的元素
(integer) 1
127.0.0.1:6379> lrange list 0 -1
1) "two"
2) "one"
127.0.0.1:6379> lrem list 1 two
(integer) 1
127.0.0.1:6379> lrange list 0 -1
1) "one"
127.0.0.1:6379> lpush list three
(integer) 2
127.0.0.1:6379> lpush list three
(integer) 3
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "three"
3) "one"
127.0.0.1:6379> lrem list 2 three
(integer) 2
127.0.0.1:6379> lrange list 0 -1
1) "one"

5 、 ltrim

In java, trim is to remove the left and right spaces. Here it means cutting, which will change the data in the list. Using lindex is to get the value of the corresponding index without changing the original list. ltrim will round out the values ​​outside the range.

127.0.0.1:6379> lrange list 0 -1
1) "four"
2) "three"
3) "two"
4) "one"
127.0.0.1:6379> LTRIM list 1 2 #1,2 代表要裁剪出的范围
OK
127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"

Note that the range here also starts from zero. At this time, after clipping, the original list has been changed.

6 、 rpoplpush

Come out from one list from the right and enter another list from the left. If the other list doesn't exist, it's okay.

127.0.0.1:6379> lrange list 0 -1
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> RPOPLPUSH list otherlist #otherlist不存在,则创建
"one"
127.0.0.1:6379> lrange otherlist 0 -1
1) "one"
127.0.0.1:6379> 

7 set lset

Replace the element, and report an error if it does not exist

127.0.0.1:6379> LSET list 0 nihao
OK
127.0.0.1:6379> lrange list 0 -1
1) "nihao"
2) "two"
127.0.0.1:6379> lset list 3 nihao
(error) ERR index out of range
127.0.0.1:6379> 

8 、 linsert

Insert an element before or after the specified element

127.0.0.1:6379> LINSERT list before nihao hello
(integer) 3
127.0.0.1:6379> lrange list 0 -1
1) "hello"
2) "nihao"
3) "two"

What Redis does here is still very humane. Each command will have a prompt and will tell you what to fill in here. We only need to roughly remember these commands. There is no need to memorize them. The key is that our brain capacity is not allowed. Appropriate It's okay to be lazy.

(3) Set

We found that sorting notes like the above is a waste of time. Every command is copied and pasted. Seriously, but the time is too much and the efficiency is not very high, so the following is a silent writing method and written in accordance with the framework consciousness.

As we know from high school, the elements in the collection are unordered and non-repetitive. Each data type is inseparable from adding, deleting, querying, and modifying, that is, obtaining values ​​and overwriting previous values.

  • sadd: add value
  • smembers: view all values ​​of the specified set
  • sismember: Check whether a value exists
  • scard: Get the number of elements in the specified collection
  • srem: remove an element of the specified element
  • smove: is to move the elements in a certain collection to another collection

If we want to implement the lottery, we can use the following command to get the value

  • srandmember: randomly get the elements of the specified set, followed by a number, such as 2, then randomly get two elements
  • spop: randomly remove elements in the specified set

There is also a scenario, such as Weibo, station b. The number of people we want to follow together, so we can use
sets. There are unions, intersections, and differences.

  • sunion: union
  • sinter: intersection
  • sdiff: difference set

(4) Hash

Redis hash is a mapping table between field and value of string type. Hash is especially suitable for storing objects. It stores the key-value form. A hash can store multiple key-values.

  • hset: set value
  • hget: Get the value
  • hgetall: get all values
  • hdel: Delete the key field of the specified hash, and the corresponding value will be deleted accordingly
  • hlen: Get the number of fields in the hash table
  • hexisits: Determine whether the field in the hash exists

Get only the key or only the value

  • hkeys: Only get all keys
  • hvals: only get all values
  • hincrby: specify the increment
  • hsetnx: It can be set if it does not exist, and it cannot be set if it exists

Hash is suitable for storing objects and information whose content changes frequently.

(5) Ordered set (Zset)

Redis ordered collections are also collections of string type elements like collections, and duplicate members are not allowed. The difference is that each element is associated with a double type score. Redis uses scores to sort the achievements in the collection from small to large. The members of an ordered set are unique, but the score can be repeated.

  • zadd: add elements
  • zrangebyscore: rank based on score
  • zrem: remove an element
  • zcard: Get the number in the ordered set
  • zcount: get the number of members in the specified interval

The ordered set can be applied to rankings, class scores, top rankings, and so on.

The above are the most basic commands. We still need to check the official website if we don’t.

Guess you like

Origin blog.csdn.net/weixin_44226263/article/details/109059063
Recommended