当前位置:网站首页>Five data structures of redis

Five data structures of redis

2022-07-06 18:12:00 eight hundred and forty-eight million six hundred and ninety-ei

1. Redis Five data structures for

Redis You can store mappings between keys and five different types of values . key Can only be of type character string , Value supports five data types : character string 、 list 、 aggregate 、 Hash table 、 Ordered set .

Programs are used to process data ,redis It's used to store data , The data processed by the program shall be stored in redis in , Data with different characteristics should be stored in redis Different types of data structures in .

character string :          single key: single value
list list : Orderly and repeatable      single key: Multi order value( The order here refers to the order of input and output )
set aggregate : Disordered and unrepeatable    single key: Multiple disorder value
pojo           hash single key : object ( attribute : value )
             zset single key: Multi order value( Order here refers to sorting )

  1. String type String

The string type is Redis The most basic data structure in , It can store any kind of data , Including binary data , Serialized data ,JSON The object is even a picture . Maximum 512M.

  1. List the type list

Redis List is a simple list of strings , Sort by insertion order . You can add an element to the head of the list ( On the left ) Or tail ( On the right ), The bottom layer is a linked list structure .

  1. Collection types set

Redis Of Set yes string An unordered unrepeated set of types .

  1. Hash type hash

Redis hash It's a string Type of field and value Mapping table ,hash Ideal for storing objects .

  1. Ordered set type zset (sorted set)

Redis Ordered set zset And collection set The same is true. string Collection of type elements , And duplicate members are not allowed .
The difference is zset Each element of the is associated with a score ( Scores can be repeated ),redis Sort the members of the set from small to large by score .

Is to implement the sortedset Interface TreeSet aggregate , Use comparator to realize sorting

2. redis Operation command in

redis Stored in the Key-Value

2.1 redis In the relevant key Operation command of :

2.1.1 View all in the database key : keys pattern

effect : Find all matching patterns pattern Of key. pattern You can use wildcards :
*: Express 0 Or more character , for example : keys* Query all key.
?: Express Single character , for example : wo?d, matching word , wood
[]: Express choice [] A character in , for example wo[or]d, matching word, wood, Mismatch wold、woord

keys *: View all in the database instance key
keys k*: View all in the data with k At the beginning key
keys h*o: View all in the data with h Begin with o At the end of the key

keys h[abc]llo: View all in the data with h Begin with llo ending , also h You can only take abc Of one character in the key

2.1.2 Judge key Does it exist in the database :exists key

exists key
If there is , return 1; If it doesn't exist , Then return to 0

exists key [key key .....] The return value is the quantity that can exist

2.1.3 Move the specified key To the specified database instance : move key index

move k 1
Rarely used , In general , A database instance corresponds to a project , This transfer key Rarely used in another database instance

2.1.4 View specified key The remaining lifetime of :ttl

time to live
ttl k1 see k1 The remaining lifetime of
If k1 There is no return -2.
If k1 Return without setting the lifetime -1, As long as there is no downtime, data always exists

2.1.5 Set up key Maximum lifetime of :expire key seconds

expire k2 20 to k2 Set the lifetime 20s,,, By time the data disappears

2.1.6 View specified key Data type of type key

type k1
Returns the current data type

2.1.7 rename key: rename key newkey

rename k1 k2 hold k1 Change to k2,,, The value does not change, but it is renamed key

2.1.8 Delete key : del key

del key Delete the specified key The data of , The return value is the actual deletion key The number of
You can also delete multiple data
del k1 k2 k3 .... Delete multiple data

2.2 redis In the relevant String Operation command of data type

2.2.1 take string Data setting of type redis in :set key value

set zsname zhangsan
set zsage 20
set totalRows 100

2.2.2 from redis In order to get string Data of type :get key

get zsname
get zsage
get totalRows

2.2.3 Append string :append key value

set phone 1388888 add to key-value
append phone 6666 stay key=phone Add 6666, Returns the length of the appended string
If key non-existent , Create key-value

2.2.4 Get the length of string data :strlen key

strlen phone Gets the length of the string

2.2.5 Add the string value 1 operation :incr key

incr zsage Add Zhang San's age value 1 , Back to plus 1 Operation of After the value of

If key non-existent , First set up a key, The value is initialized to 0, Then proceed incr operation

requirement key Indicated by value It must be a number , otherwise , Report errors
incr zsname Report errors

2.2.6 Subtract string values 1 operation :decr key

  • Return minus 1 Data after operation
  • If key non-existent , First set up a key, The value is initialized to 0, Then proceed decr operation .
  • requirement key Indicated by value It must be a number , otherwise , Report errors

2.2.7 Adds a string value offset operation :incrby key offset

incrby key offset

  • Back to plus offset Data after operation
  • If key non-existent , First set up a key, The value is initialized to 0, Then proceed incrby operation .
  • requirement key Indicated by value It must be a number , otherwise , Report errors

incrby zsage 10 Add... To Zhang San's data 10 operation ,

2.2.8 Subtract string values offset operation :decrby key offset

  • Back to plus offset Data after operation
  • If key non-existent , First set up a key, The value is initialized to 0, Then proceed decrby operation .
  • requirement key Indicated by value It must be a number , otherwise , Report errors

decrby zsage 10 Subtract the data of Zhang San 10 operation

2.2.9 Close interval intercept string :getrange key startIndex endIndex

The subscript is intercepted from 0 Start , Closed interval interception
The subscript from left to right is 0,1,2,3…
The subscript from right to left is -1,-2,-3 …
zhangsan
getrange zsname 2 3 The return is an

gettrange zsname 2 -3 Return results angs

2.2.10 use value Override subscript startIndex Starting string :setrange key startIndex value

zhangshan
setrange zsname 5 xiaoming The modified data is zhangxiaoming

原网站

版权声明
本文为[eight hundred and forty-eight million six hundred and ninety-ei]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061007569346.html