Redis data type and api operation, it is recommended to collect

key

keys *

scan 0 match * count 1

exists key Determine whether a key exists

move key db The current library is gone, go to the specified library

expire key set the expiration time for the given key

ttl key to see how much time has expired -1 means never expire -2 means expired

type key Check what type of key is

1.
String String is the most basic type of redis. You can understand it as exactly the same type as Memcached. A key corresponds to a value.

The string type is binary safe. This means that the redis string can contain any data. Such as jpg images or serialized objects.

The string type is the most basic data type of Redis. The string value in a redis can be up to 512M

set key value 设置key value

get key view the value of the current key

del key Deletion key

append key value If the key exists, add it at the end of the specified key, if the key exists, it is similar to set

strlen key returns the length of this key

The following commands can only operate normally when the key value is a number

incr key is the value of the holding key plus one

decr key is the value of the specified key minus one

The incrby key value increases the value of the specified key

The decrby key value is the value of the specified key minus the value

getrange key 0 (start position) -1 (end position) Get the value in the specified range, similar to the relationship between...and (0 -1) means all

setrange key 1 (start position, where to start setting) specific value setting (replacement) the value within the specified range

setex Jane second value real value setting key with expiration time, dynamic setting.

setnx key value The value of the key can only be set when the key does not exist.

mset key1 value key2 value Set one or more key-value pairs at the same time.

mget key1 key 2 Get all (one or more) values ​​of a given key.

msetnx key1 value key2 value Set one or more key-value pairs at the same time, if and only if all the given keys do not exist.

getset key value sets the value of the given key to value and returns the old value of the key.

2.list
is a string linked list, left and right can be inserted and added; if the key does not exist, create a new linked list; if the key already exists, add content; if all values ​​are removed, the corresponding key will disappear . The operation of the linked list is extremely efficient in both head and tail, but if it is to operate on the middle element, the efficiency is very bleak.

Redis lists are simple lists of strings, sorted in the order of insertion. You can add an element to the head (left) or tail (right) of the list. Its bottom layer is actually a linked list

lpush key value1 value2 Add one or more values ​​to the head of the list

rpush key value1 value2 Add one or more values ​​to the bottom of the list

lrange key start end Get the elements of the specified range of the list (0 -1) means all

lpop key move out and get the first element of the list

rpop key move out and get the last element of the list

lindex key index Get the elements in the list by index

llen get list length

lrem key 0 (number) value, which means to delete all given values. This is to delete all the values ​​from left to right, the specified number of elements whose value is equal to the specified value, and the returned value is the actual deleted number

ltrim key start (where to start the cut) end (end position) intercept the elements of the specified index interval, the format is the key start index and end index of the ltrim list

3.
Set Redis's Set is an unordered collection of string type that cannot be repeated.

sadd key value1 value 2 Add one or more members to the collection

smembers key returns all members in the set

sismembers key member determines whether the member element is a member of the set key

scard key Get the number of elements in the collection

srem key value Delete the specified element in the collection

The value of srandmember key is randomly selected from the set collection. If the number of elements exceeds the maximum number, all elements will be taken out.

spop key randomly remove and return an element in the collection

smove key1 key2 value (a value in key1) The function is to remove the value determined in key1 and add it to the key2 collection

sdiff key1 key2 items in the first set but not in any subsequent sets (difference set)

sinter key1 key2 is in the first set and the second set (intersection)

sunion key1 key2 All elements of the two sets (union)

4.hash
Redis hash is a collection of key-value pairs. Redis hash is a mapping table between field and value of string type, and hash is particularly suitable for storing objects.

The kv mode remains unchanged, but v is a key-value pair similar to Map in Java

hset key (key value) adds an element to the hash table

hget key key Get an element from the hash table

hmset key key1 value1 key2 value2 key3 value3 Add one or more elements to the collection

hmget key key1 key2 key3 Get one or more elements from the collection

hgetall key Get all the fields and values ​​of the specified key in the hash list

hdel key key1 key2 delete one or more hash fields

hlen key Get the number of fields in the hash table

hexits key key Check whether the specified key (field) exists in the hash table

hkeys key Get all keys (fields) in the specified hash table

hvals key Get all values ​​(values) in the specified hash table

hincrdy key key1 Quantity (integer) Determine the number of fields in the hash table, which has the same meaning as incr

hincrdyfloat key key1 Quantity (floating point number, decimal number) Determines the number of fields in the hash table, which means the same as incr

hsetnx key key1 value1 has the same function as hset, the difference is that there is no assignment and invalidity.

5.
zset Redis zset, like set, is also a collection of string type elements, 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 members of the set from small to large. The members of zset are unique, but the score (score) can be repeated.

zadd key score value score value to add one or more members to the collection

zrange key 0 -1 means all return all values ​​in the specified set

zrange key 0 -1 withscores returns all values ​​and scores in the specified set

zrangebyscore key start score end score return value between specified score

A corresponding value of zrem key score (value), which can be multiple values ​​to delete elements

zcard key Get the number of elements in the collection

zcount key Start score End score Get the number of elements in the score interval

zrank key vlaue Get the subscript position of value in zset (sorted according to score)

zscore key value Get the corresponding score according to the value

Guess you like

Origin blog.csdn.net/yueyunyin/article/details/108430801