Home  >  Article  >  Database  >  Introduction to common redis commands

Introduction to common redis commands

尚
forward
2020-03-06 09:35:402257browse

Introduction to common redis commands

1. Key related:

(1) redis allows fuzzy query key (keys *) There are 3 wildcards *,?,[ ]

(2)randomkey: Return random key 

(3)type key: Return the type of key storage

(4) exists key: Determine whether a key exists

(5) del key: delete key

FLUSHALL: delete all keys (use with caution)

2. Data operations:

Redis supports five data types: string (string), hash (hash), list (list), set (set) and zset (sorted set: ordered gather).

1. string (string)

A key corresponds to a value. A key can store up to 512MB. The string type is binary safe.

(1)set key value [ex seconds] [px milliseconds] [nx/xx] 

If ex and px are written at the same time, the subsequent validity period shall prevail

nx: If the key does not exist, create it

xx: If the key exists, modify its value

(2) get key: value

(3) mset key1 value1 key2 value2 Set multiple values ​​at one time

(4) mget key1 key2: Get multiple values ​​at one time

(5)setrange key offset value: Change the offset byte of the string to value

If the offset > the length of the string, the character will be automatically filled with 0x00

(6)append key value: Append value to the original value of key

(7)getrange key start stop: Get the value in the [start, stop] range of the string

For the subscript of a string, the left number starts from 0 and the right number starts from -1

Note:

When start>length, then Return an empty string

When stop>=length, intercept to the end of the string

If the position of start is to the right of stop, return an empty string

(8) getset key nrevalue: Get and return the old value, and set the new value

2, hash (hash)

Redis hash is a A mapping table of string type fields and values, hash is particularly suitable for storing objects. Each hash can store 232 - 1 key-value pairs (more than 4 billion).

(1)hset myhash field value: Set the field of myhash to value

(2)hsetnx myhash field value: Set the field of myhash to value if it does not exist

(3) hmset myhash field1 value1 field2 value2: Set multiple fields at the same time

(4) hget myhash field: Get the specified hash field

( 5) hmget myhash field1 field2: Get multiple fields at one time

(6) hincrby myhash field 5: Add the specified hash field to the given value

(7 ) hexists myhash field: Test whether the specified field exists

(8) hlen myhash: Return the number of hash fields

(9) hdel myhash field: Delete the specified field

(10) hkeys myhash: Returns all hash fields

(11) hvals myhash: Returns all hash values

(12) hgetall myhash: Get all fields and values ​​in a hash

3. list (list)

Redis list is a simple list of strings, sorted according to insertion order . You can add an element to the head (left) or tail (right) of the list. Lists can store up to 232 - 1 elements (4294967295, each list can store more than 4 billion).

(1)lpush key value:Insert the value into the head of the linked list

(2)rpush key value:Insert the value into the tail of the linked list

(3 ) lpop key: Return and delete the head element of the linked list

(4) rpop key: Return and delete the tail element of the linked list

(5) lrange key start stop: Return Elements in [start, stop] in the linked list

(6) lrem key count value: Delete the value value from the linked list, and end after deleting the absolute value of count values

count > 0 Delete from the header

count < 0 Delete from the tail

count=0 Delete all

(7) ltrim key start stop: Cut the link corresponding to the key, cut the [start, stop] section and re-assign the modification to the key

(8) lindex key index: Return the value on the index index

4. Set (set)

Redis’ Set is an unordered collection of string type. Values ​​are not repeated.

(1) sadd key value1 value2: Add elements to the collection

(2) smembers key: Get all elements of the collection

(3) srem key value: Delete an element of the collection

(4) spop key: Return and delete 1 random element in the set (you can sit in the lottery, and you will not draw someone repeatedly)

(5) srandmember key: randomly pick an element

(6) sismember key value: Determine whether the collection has a certain value

(7) scar key: Return the number of collection elements

(8) smooth source dest value: Move the value of source to the dest collection

(9) sinter key1 key2 key3: Find the intersection of key1 key2 key3

(10) sunion key1 key2: Find the union of key1 key2

(11) sdiff key1 key2: Find the difference set of key1 key2

(12) Sinterstore res key1 key2: Find the intersection of key1 key2 And stored in res

5. zset (sorted set: ordered set)

Redis zset, like set, is also a collection of string type elements. And no duplicate members are allowed. The difference is that each element is associated with a double type score. Redis uses scores to sort the members of the collection from small to large. The members of zset are unique, but the scores can be repeated.

(1) zadd key score1 value1: Add element

(2) zrange key start stop [withscore]: After sorting the collection, return the elements ranked [start, stop]

The default is ascending order withscores is to print out the score too

(3)zrank key member: Query the ranking of member (starting from 0 in ascending order)

(4) zrangebyscore key min max [with scores] limit offset N: Set (ascending order)

After sorting, take the elements with score within [min, max], skip offset, and take out N elements

(5) zrevrank key member: Query the member ranking (starting from 0 in descending order)

(6) zremrangebyscore key min max: Delete elements according to score, delete the score in [min , max] between

(7) zrem key value1 value2: Delete elements in the set

(8) zremrangebyrank key start end: Delete elements by ranking, delete

ranked between [start, end] (9) zcard key: Returns the number of collection elements

(10) zcount key min max: Returns [ min, max]The number of elements in the interval

For more redis knowledge, please pay attention to the redis tutorial column on the PHP Chinese website.

The above is the detailed content of Introduction to common redis commands. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete