Redis data type Learning

Source: Internet
Author: User

Redis is a key-value memory database. Value provides the data structures in string, list, set, sort set, and hash 5.

Summary of commands and operation methods: It is really troublesome to write them one by one ,,,

Operations on a keys

1) DEL key [Key...]
Delete multiple keys. Ignore the key that does not exist.
Returns the number of actually deleted keys.

2) exists key
Returns 1 and 0, indicating whether the key exists.

3) expire key seconds
Set the key survival time

4) expireat key Timestamp
UNIX timestamp (seconds since January 1, 1970)

5) Keys Pattern
Returns the list of keys of match pattern.

6) Move key DB
Move the key in the current database to the specified database
If the key does not exist in the current dB or the specified dB already has this key, it will not be moved
Returns 1 or 0 to indicate whether to moved.

7) object refcount <key>
Returns the number of references associated with the key.
Object encoding <key>
Returns the compression encoding of keys in the database.
Object idletime <key>
How long has the returned key not been read/written?
It is available only after version 2.2.3. It is installed in version 2.0.2 and not supported.

8) persist key
Remove the Time Limit attribute on the key to make it permanently exist.
Effective after version 2.1.2

9) randomkey
Returns a random key from the database.

10) Rename key newkey
Rename a key.

11) renamenx key newkey
Rename a key. If newkey already exists, the command fails.

12) Sort key [by pattern] [limit offset count] [get pattern [get pattern...] [ASC | DESC] [Alpha] [store destination]
Various sorting methods

13) TTL key
Returns how long a key can survive in the database (seconds)
If the key does not exist or there is no time limit,-1 is returned.
Expire mykey can be used to set the survival time of a key.

14) type key
Returns the type of the key (string, list, set ...)

2 strings

1) append key value
If the key does not exist, create a new key.
If the key already exists, apend after the value changes the value of the value.

2) decr key
Key minus 1, applicable to INTEGER (or string represented by integer)
For other types, an error is returned.

3) decrby key Decrement
Reduce the number of specified keys
If the key does not exist, reduce it from scratch.

4) Get key
Not explained

5) getrange key start end
Returns a substring containing the start and end indexes.
0 indicates the first index,-1 indicates the last index.

6) GetSet key value
Set the new value for an existing key and return the old value.
If the key is not of the string type, an error is returned.

7) incr key
Add, same as above

8) incrby key Increment
Same as above, not explained

9) mget key [Key...]
Returns multiple values.

10) mset key value [key value...]
Set multiple key-value values at the same time

11) msetnx key value [key value...]
If a key already exists, the entire command is not executed.

12) set key value
Not explained

13) setex key seconds Value
Set a key and set the time for it
Equivalent to the following command:
Set mykey Value
Expire mykey seconds

14) setnx key value
Set if not exists. If the key does not exist, set a new key.
Otherwise, do nothing.

15) setrange key Offset Value
Replace the previous value with value from the offeset index of an existing key
Valid after version 2.1.8

16) strlen key
Not explained


1) hdel key field [field...]
Delete multiple fields in the hash table (key)
Ignore nonexistent domains and return the number of actually deleted Domains

2) hexists key field
Whether the field exists in the hash table (key)

3) hget key field
Obtain the field value from the hash table.

4) hgetall key
Returns the values of all fields in the hash table.

5) hincrby key field Increment
Add a value to the specified field in the hash table.

6) hkeys key
Returns all domains.

7) hlen key
Returns the length of the hash table.

8) hmet key field [field...]
Returns the values of multiple fields in the hash table.

9) hmset key field value [field value...]
Same as above

10) hset key field value
Set a pair of Values

11) hsetnx key field value
Set only when the domain does not exist

12) hvals key
Returns all values.

Iv. Lists

1) blpop key [Key...] timeout
Remove and get the first element in a list, or block until one is available
In the blocking version of lpop, the first element in the first non-null key of POP within the timeout time (the element is returned together with its key)
You can allow null values in a key. It will find the first non-empty key.
If both are empty, blocking will take effect, and it will wait until a key passes through lpush or rpush is not empty.

2) brpop key [Key...] timeout
Remove and get the last element in a list, or block until one is available
Last one. Same as above

3) brpoplpush source destination timeout
Pop a value from a list, push it to another list and return it; or block until one is available

4) lindex Key Index
Get the value with the index (the index in the back-to-list is in front)

5) linsert key before | after each value
Insert value before or after values

6) llen key
Not explained

7) lpop key
Pop and return the first value (index 0 value)

8) lpush key value [value...]
Not explained

9) lpushx key value
The difference from the previous one is that a value is pushed only when the key already exists and is not empty.

10) lrange key start stop
Returns the index value from start to stop.

11) lrem key Count value
Delete | count | values. Count indicates the number from left to right, and negative indicates the number from right to left.
0 indicates deleting all items whose values are value.
The return value is the number of actually deleted items.

12) lset key index value
The index range to be set must already exist.

13) ltrim key start stop
Trim a list that retains

14) rpop key
Last pop key

15) rpoplpush source destination
Append the last element of pop to another list and return this value.

16) rpush key value [value...]
Add to the end of the list

17) rpushx key value
Add the list to the end of the list only when the list is not empty

Five Sets

1) sadd key member
If the member already exists, the command is invalid. Note that it is a set and the elements cannot be repeated.

2) scard key
Returns the number of elements in a collection.

3) sdiff key [Key...]
Difference set, which exists in the first set but does not exist in other sets

4) sdiffstore destination key [Key...]
Same as above. Only the returned set is included in the Set destination.

5) Sinter key [Key...]

6) sinterstore destination key [Key...]
Same as above

7) sismember key member
Whether it belongs to a set

8) smembers key
Returns all elements of the set.

9) smove source destination Member
Move elements from one set to another

10) spop key
Pop is an incoming element, because the set is unordered.

11) srandmember key
Returns a random element.

12) Srem key member [member...]
Remove one or more elements
Ignore non-existing elements

13) sunion key [Key...]

14) sunionstore destination key [Key...]
Same as above

Six sorted Sets

1) zadd key score member [score] [member]
Add one or more elements. If the element already exists, update its socret value.
Although an ordered set is ordered, it is also a set and cannot repeat elements.
Update the score value of the original element.

2) zcard key
Number of Elements

3) zcount key min max
Number of elements in the score value between min and Max

4) zincrby key increment Member
Add increment to the element member in the sorted set key
If the key does not exist, create a key
If the member does not exist, create a member and add it from 0.
An error occurs only when the key exists but the type is not an ordered set.

5) zinterstore destination numkeys key [Key...] [weights weight [weight...] [aggregate sum | min | max]

Nubberkeys specifies the number of sets that require Intersection
Calculate the intersection of nubberkeys sets and put the result in the Set destination
(If des already exists, overwrite it)

Parameter [weights weight [weight...]
Used to calculate the weight of an intersection set.

Parameter [aggregate sum | min | max]
When two sets have elements with the same name but score is different, specify which score to take

6) zrange key start stop [withscores]

Index elements from start to stop

7) zrangebyscore key min max [withscores]
Elements of score between min and Max

8) zrank key member
Returns the index of the menber (sorted in ascending order of scores)

9) zrem key member [member...]
Not explained

10) zremrangebyrank key start stop
Not explained

11) zremrangebyscore key min max
Not explained

12) zrevrange key start stop [withscores]
Returns the elements between indexes.

13) zrevrangebyscore key Max min [withscores]
Same as above.

14) zrevrank key member
8), score in descending order

15) zscore key member
Not explained

16) zunionstore destination numkeys key [Key...] [weights weight [weight...] [aggregate sum | min | max]

Returns the Union, and the others are the same as 5) returns the intersection.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.