The five major data types and three special data types of redis, as well as some basic commands of the five major types

keys * View all keys

flushdb clears the current database

EXISTS (key) to determine whether there is this key

move (key) 1 remove the key in library 1

type name View the data type of name

EXPIRE name 10 expires after setting name10s

ttl name View remaining time

Five major data types Redis-Key

String

 append string append

get length append

Increment and Decrement

 string range getrange

a:1>getrange name 0 -1 //Can be used to get the full length

"v1hello, nihao"
The following is to get the specified length

 replace a character setrange

a:1>set key1 abcdefg

"OK"
a:1>get key1

"abcdefg"
a:1>setrange key1 1 xx

"7"
a:1>get key1

"axxdefg"

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

setex ( set with expire ) #set expiration time

setnx ( set if not exist ) # Does not exist in the setting (commonly used in distributed locks)

a:1> setex key2 30 "hello"  //Set the value of key2 to hello, and expire in 30s

"OK"
a:1>ttl key2 //Check remaining time

"22"
a:1>get key2

"hello"


a:1> setnx mykey "redis"   //If mykey does not exist, create mykey

"1"


a:1> keys *

1) "name"

2) "mykey"

3) "views"

4) "key1"

a:1>ttl key2 
"-2" 

a:1> setnx mykey "MongoDb"    //If mykey exists, the creation fails 
"0" 

a:1>get mykey 
"redis"

################################

Set multiple values ​​at the same time, and check multiple values ​​at the same time

mset,mget

a:0>mset k1 v1 k2 v2

"OK"
a:0>get k1

"v1"
a:0>get k2

"v2"
a:0>mget k1 k2

1) "v1"

 2)  "v2"
a:0>

msetnx // Set multiple values ​​at the same time, and check multiple values ​​​​at the same time, atomicity, either succeed together or fail together

a:0>msetnx k1 v1 k3 v3

"0"

a:0>mset k1 v1 k3 v3
"OK"

################################

getset

Combined usage

user:1 can be regarded as the user id is 1, the name is zhangsan, and the additional attribute age is 2

##########################

Some usage scenarios of String

List (list)

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

First create a list and insert data into the list

insert

Lpush list (stored data) // Insert a value or multiple values ​​into the table header (left)

Rpush list (stored data) //Insert one or more values ​​at the end of the table

Get the specified range value

Lrange list 0 1 //Check the value from subscript 0 to subscript 1

remove

Lpop list //Remove the value of the first list

Rpop list //Remove the value of the last list

Get the specified value of the list

lindex list (serial number) //Get the value in the subscript of the serial number

get list length

Llen list //returns the length of the list "8"

delete specified value

lrem list 2 one1 //Delete two one1 in the list

Keep the elements in the specified interval in the list, and not delete all the elements in the interval

Ltrim list 1 2 //The elements in the subscript range 1-2 are reserved, and all others are deleted

Combined usage rpoplpush

 lset specifies the update operation

 Linsert inserts a specific value before or after an element in the list

 

 

Set (collection) The values ​​in the set are unordered and non-repetitive

Add value to set collection Sadd

sadd myset "hello"

View all values ​​of the specified set Smembers

smember's cozy

Determine whether a value exists Sismember

sismember myset hello

Get the number of elements in the set collection Scard

scard the coziness

  Remove the specified value Srem in the set collection      

srem myset hello

Randomly extract the specified number of elements Srandmember

Srandmember myset Randomly draw one

Srandmember myset 2 Randomly draw two, the number can be defined

Randomly delete some elements in the set spop

spop myset

Move the specified value in one set to another set

smove myset myset2 "kuangshen"

 Difference, intersection and union Sdiff , Sinter Sunion

 Some scenarios using set

 

Hash

key-map is not much different from key-valve in essence, it can be regarded as key-<key-value>

Hset,        Hmset,        Hget,        Hmget

 Delete the key specified by hash, and the corresponding value will also delete Hdel

 Get the number of fields in the hash table Hlen

 Determine whether the field exists Hexists

 Only get all field Hkeys

Just get all value Hvals

 Hincrby myhash field3 5 specifies increment

Hdecrby myhash field3 1 specifies the minus

Hsetnx myhash field hello can create settings if there is no value in the first place, and set a new value in the second time Hsetnx myhash field woreld, because he already has hello and all settings fail

Some scenes of hashing

Zset (ordered set)

Add Zset Zadd

 Sorting -inf +inf corresponds to negative infinity and positive infinity

Zrangenbyscore salary -inf +inf withscore display all users, the value from small to large, and with scores

 remove element zrem

 Get the specified interval elements

 

 summary

 

Three special data types

Geospatial

Hyperloglog

Bitmap

Guess you like

Origin blog.csdn.net/weixin_53810346/article/details/123991490
Recommended