当前位置:网站首页>Redis-02. Redis command

Redis-02. Redis command

2022-07-05 06:28:00 Cold leaves elegant_

Redis-02.Redis command

1.Redis Data structure introduction

Redis yes - individual key-value The database of , key- Like String type , however value There are many types of :

Basic types :

  • String:HelloWorld
  • Hash:{name:“Jack”, age:12}
  • List:[A->B->C->B]
  • Set:{A, B, C}
  • SortedSet:{A:1, B:2, C:3}

Special type :

  • GEO:{A:(111.1, 22.2)}
  • BitMap:0011010101100101
  • HyperLog:0011010101100101

2.Redis General Command

General instructions are partial data types , Can be used , Common are :

  • KEYS: View all that match the template key( It is not recommended to use... On production environment equipment ,redis Single thread , Blocking occurs when the amount of data is large )
  • DEL: Delete a specified key
  • EXISTS: Judge key Whether there is
  • EXPIRE: Give me a key Set expiration date , At the expiration of the validity period, the key Will be automatically deleted
  • TTL: View one KEY The remaining validity period of (-1 For permanent validity ,-2 Is invalid )

3.String type

String type , That is string type , yes Redis The simplest storage type in .

Its value Is string , However, depending on the format of the string , Can be divided into 3 class :

  • string: Normal string
  • int: Integer types , Can do self increasing 、 Since the reduction of operating
  • float: Floating point type , Can do self increasing 、 Since the reduction of operating

Either format , The bottom layer is stored in the form of byte array , It's just that the coding method is different . The maximum space of string type cannot exceed 512m.

String Common commands are :

  • SET: Add or modify an existing - individual String Key value pairs of type
  • GET: according to key obtain String Type of value
  • MSET: Batch add multiple String Key value pairs of type
  • MGET: According to the multiple key Get multiple String Type of value
  • INCR: Let an integer key Self increasing 1
  • INCRBY: Let an integer key Increment and specify the step size , for example : incrby num 2 Give Way num Value on the 2
  • INCRBYFLOAT: Increment a floating-point number and specify the step size
  • SETNX: Add one String Key value pairs of type , The premise is this key non-existent , Otherwise, do not execute
  • SETEX: Add one String Key value pairs of type , And specify the validity period

4.Key Hierarchical structure

key Structure

Redis Of key Multiple words are allowed to form a hierarchy , Use... Between multiple words ’:' separate , The format is as follows :

Project name : Business name : type :id

This format is not fixed , You can delete or add entries according to your own needs

if value It's a java object , For example, one user object , You can serialize the object as JSON String is stored :

keyvalue
project1:user:1{“id”:1,“name”:“Sam”,age:22}

5.Hash type

Hash type , Also called hash , Its value It's one Unordered dictionary , Be similar to Java Medium HashMap structure .
String Structure is to serialize an object into JSON Store after string , It is inconvenient to modify a field of an object :

keyvalue
project1:user:1{“id”:1,“name”:“Sam”,age:22}

Hash Structure can store each field in the object independently , You can do this for a single field CRUD:

keyfiledvalue
project1:user:1id1
nameSam
age22

Hash Common commands are :

  • HSET key field value: Add or modify hash type key Of field Value
  • HGET key field: Get one hash type key Of field Value
  • HMSET: Batch add multiple hash type key Of field Value
  • HMGET: Get more than one in batch hash type key Of field Value
  • HGETALL: Get one hash type Of key All of them field and value
  • HKEYS: Get one hash Type of key All of them field
  • HVALS: Get one hash Type of key All of them value
  • HINCRBY: Give Way - individual hash type key The field value of is incremented and the step size is specified
  • HSETNX: Add one - individual hash Type of key Of field value , The premise is this field non-existent , Otherwise, do not execute

List type

Redis Medium List The type and Java Medium LinkedList similar , You can view it as - A two-way linked list structure . It can support both forward retrieval and reverse
retrieval .
Characteristics are also related to LinkedList similar :

  • Orderly
  • Elements can be repeated
  • Insert and delete fast
  • The query speed is average

Often used to store an ordered data , for example : Friends circle the likes list , Comment list, etc .

List Common commands are :

  • LPUSH key element… : Insert one or more elements to the left of the list
  • LPOP key: Remove and return the first element to the left of the list , No return nil
  • RPUSH key element… : Insert one or more elements to the right of the list
  • RPOP key: Remove and return to the first... On the right side of the list - Elements
  • LRANGEkeystarend: Returns all elements within a range of subscripts
  • BLPOP and BRPOP: And LPOP and RPOP similar , Just wait for the specified time when there is no element , Not directly back null

Set type

Redis Of Set Structure and Java Medium HashSet similar , You can view it as - - individual value by nul Of HashMap. Because it is also a hash surface , Therefore, it has the ability to communicate with HashSet Similar features :

  • Element is not repeatable
  • Quick search
  • Support intersection 、 Combine 、 Difference set and other functions

Set Common types of commands :

  • SADD key member … : towards set Add one more - Elements or elements
  • SREM key member … : remove set The specified element in
  • SCARD key: return set The number of elements in
  • SISMEMBER key member: Determine whether an element exists in set in
  • SMEMBERS: obtain set All elements in
  • SINTER key1 key2 … : seek key1 And key2 Intersection
  • SDIFF key1 key2 … : seek key1 And key2 The difference between the set
  • SUNION key1 key2 …: seek key1 and key2 Union

SortedSet type

Redis Of SortedSet yes - - Sortable set aggregate , And Java in Of TreeSet Some similar , But the underlying data structure is very different .SortedSet in
Every - - Each element has - individual score attribute , Can be based on score Attribute to sort elements , The underlying implementation is a jump table (SkipList) Add hash surface .
SortedSet It has the following characteristics :

  • Sortable
  • Elements do not repeat
  • Fast query speed

because SortedSet Sortable properties of , Often used to implement functions like leaderboards .

SortedSet Common commands are :

  • ZADD key score member: Add one or more elements to sorted set, If it already exists, update its score value
  • ZREM key member: Delete sorted set One of the specified elements in
  • ZSCORE key member : obtain sorted set Of the specified element in score value
  • ZRANK key member: obtain sorted set The ranking of the specified elements in
  • ZCARD key: obtain sorted set The number of elements in
  • ZCOUNT key min max: Statistics score The number of all elements whose values are within a given range
  • ZINCRBY key increment member: Give Way sorted set The specified element in is incremented , The step size is the specified increment value
  • ZRANGE key min max: according to score After ordering , Get the elements within the specified ranking range
  • ZRANGEBYSCORE key min max: according to score After ordering , Get specified score Elements in scope
  • ZDIFF、ZINTER、 ZUNION: Difference set 、 intersection 、 Combine

notes : All rankings are in ascending order by default , If you want to order in descending order, in the... Of the command Z Add later REV that will do

Reference material :

  1. https://www.bilibili.com/video/BV1cr4y1671t?p=16&spm_id_from=pageDriver
原网站

版权声明
本文为[Cold leaves elegant_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050620030405.html