Summary of Redis operations commands

Source: Internet
Author: User
Tags delete key hash min redis

key pattern Query the corresponding key

(1) Redis allow fuzzy query key has 3 wildcard characters * 、?、 []

(2) Randomkey: Return to Random key

(3) Type key: Returns the type of the key store

(4) Exists key: Determine if a key exists

(5) Del key: Delete key

(6) Rename key newkey: renaming

(7) Renamenx key Newkey: If the Newkey does not exist then the modification succeeds

(8) Move key 1: Move key to 1 database

(9) TTL key: Query key lifetime (seconds)

Expire key integer value: Sets the life cycle of the key in seconds

(one) pexpire key integer value: Sets the lifetime of the key in milliseconds

Pttl key: Querying the life cycle of a key (MS)

Perisist key: Set the specified key to be permanently active

second, the operation of the string type

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

If both ex and PX are written at the same time, the following validity period will prevail

NX: Established if key does not exist

XX: Modify the value if key exists

(2) Get key: Take value

(3) Mset key1 value1 Key2 value2 set multiple values at once

(4) Mget key1 Key2: Get multiple values at once

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

If offset > string length, the character automatically complements 0x00

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

(7) GetRange key start stop: Gets the value of the [Start, stop] range in the string

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

Note: When Start>length, an empty string is returned

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

Returns an empty string if start is located at the right of stop

(8) Getset key nrevalue: Gets and returns the old value, setting the new value

(9) incr key: Self-increment, return the new value, if incr a value other than int returns an error, incr a nonexistent key, set key to 1

Incrby Key 2: Jump 2 self-increment

(one) Incrbyfloat by 0.7: self-increment floating point number

Setbit Key Offset Value: Sets the value on the offset corresponding to the binary, returning the old value on that bit

Note: If offset is too large, it will be filled in the middle 0

What is the maximum offset?

2^32-1, you can launch the largest string to 512M

Bitop Operation Destkey Key1 [Key2 ...] Do opecation to Key1 Key2 and save the results on Destkey

Opecation can be an and OR not XOR

Strlen key: Takes the length of the value of the specified key

(Setex) Key time value: Sets the value corresponding to key, and sets the validity period to time seconds

third, linked list operation

The Redis list type is actually a doubly linked list where each child element is a string type, and the maximum length of the linked list is 2^32. Lists can be used either as stacks or as queues.

The pop operation of the list also has a blocking version, mainly to avoid polling

(1) Lpush key value: Inserts the value into the list header

(2) Rpush key value: Inserts the value into the tail of the list

(3) Lpop key: Returns and deletes the list header element

(4) Rpop key: Returns and deletes the trailing element of the linked list

(5) Lrange key start stop: Returns the element in the list [Start, stop]

(6) Lrem key count value: Remove value from linked list, delete count of absolute value after value is ended

Count > 0 Remove from table header count < 0 remove count=0 from footer Delete all

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

(8) Lindex key index: Returns the value on index

(9) Llen key: Count the number of elements in the list

Linsert key After|before Search value: Search in the key list and insert value before the search value |

(one) Rpoplpush source dest: Take out the end of source, put it on the Dest head, and return the cell value

Scenario: Task + Bak doubly linked list complete secure queue

Business logic: Rpoplpush Task bak

Receive return values and do business processing

Rpop Bak Cleanup Task If successful, and if unsuccessful, the next time the task is taken from the Bak table

Brpop,blpop Key timeout: Wait for the tail/head element of the popup key

Timeout is the wait timeout, and if timeout is 0, wait.

Application scenario: Long polling ajax, can be used in online chat

Iv. types and operation of hashes

Redis Hash is a string-type field and value mapping table with an O (1) (average) addition and deletion operation. Hash is especially useful for storing objects, storing an object in a hash type consumes less memory, and provides easy access to the entire object.

Configuration: Hash_max_zipmap_entries #配置字段最多64个

Hash_max_zipmap_value #配置value最大为512字节

(1) Hset myhash field value: Set Myhash field as value

(2) Hsetnx myhash field value: Does not exist in the case of setting the Myhash field as value

(3) Hmset myhash field1 value1 field2 value2: Set multiple field at the same time

(4) Hget myhash field: Gets the specified hash field

(5) Hmget myhash field1 field2: Get more than one field at a time

(6) Hincrby myhash field 5: Specify the hash field plus the given value

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

(8) Hlen Myhash: Number of field returned hash

(9) Hdel myhash field: Delete the specified field

(Ten) Hkeys Myhash: Return hash of all field

(one) Hvals Myhash: Returns the hash of all the value

Hgetall Myhash: Get all field and value in a hash

v. Collection structure Operation

Features: disorder, certainty, uniqueness

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

(2) Smembers key: Gets all the elements of the collection

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

(4) Spop key: Returns and Deletes 1 random elements in the collection (can be drawn in a lottery, not repeatedly pumped to someone)

(5) Srandmember key: Randomly take an element

(6) Sismember key value: Determine if a set has a value

(7) SCard key: Returns the number of collection elements

(8) Smove source Dest Value: Moves the value of source to the Dest collection

(9) Sinter key1 Key2 Key3: Intersection of Key1 Key2 Key3

(Ten) Sunion Key1 Key2: The collection of Key1 Key2

(one) Sdiff key1 Key2: The difference set of Key1 Key2

Sinterstore Res key1 Key2: Finding the intersection of Key1 Key2 and the presence of RES

six, ordered set

Concept: It adds a sequential property based on set, which can be specified when adding modified elements, and Zset automatically adjusts the order of the new values after each assignment. It can be understood that there are two columns of MySQL table, one column stores the value, one column stores the order, and the operation key is understood as Zset's name.

As with set Sorted,sets is also a collection of elements of type string, but each element is associated with a double type of score. The implementation of the sorted set is a mixture of skip list and hash table.

When an element is added to the collection, an element-to-score mapping is added to the hash table, so the cost of getting score for a given element is O (1). Another score-to-element mapping is added to the skip list and sorted by score, so the elements in the collection can be obtained in an orderly manner. The cost of adding and removing operations is the same as that of O (logn) and skip list, and the skip list implementation of Redis is a doubly linked list, so you can reverse the elements from the tail. The most common use of sorted set is to use it as an index, we can store the field to be sorted as score, and the ID of the object when the element is stored.

(1) Zadd key score1 value1: add Element

(2) Zrange key start Stop [Withscore]: After sorting the set, return to the position [Start,stop] element by default is the ascending sequence withscores is the score also print out

(3) Zrank Key member: Ranking of query member (starting from 0 in ascending order)

(4) Zrangebyscore key min Max [withscores] limit offset N: set (ascending) sorts the elements in [Min, Max] after score, and skips the offset, takes out N

(5) Zrevrank Key member: Query member ranking (starting from descending 0)

(6) Zremrangebyscore key min Max: Delete elements according to score, delete score between [Min, Max]

(7) Zrem key value1 value2: Delete the elements in the collection

(8) Zremrangebyrank key start end: Delete the element by rank, delete the position between [start, end]

(9) Zcard key: Returns the number of collection elements

(Ten) Zcount key min Max: Returns the number of elements in the [min, Max] Interval

(one) Zinterstore dest Numkeys Key1[key2 ...] [WEIGHTS weight1 [weight2 ...] [AGGREGATE sum| min| MAX]

To seek the intersection of Key1,key2, the weights of Key1,key2 are weight1,weight2 respectively.

Polymerization method with Sum|min|max

Aggregation results in the Save sub-dest Collection

Note: Weights,aggregate how to understand.

Answer: If there is a intersection, the intersection element has score,score how to deal with it. Aggregate num->score Add, Min min Score,max maximum score, in addition you can set the weight of different keys by weights, when the intersection score*weight

VII. Server-related commands

(1) Ping: Determine if the connection is alive

(2) Echo: Print some content at the command line

(3) Select: Select Database

(4) Quit: Quit connection

(5) Dbsize: Returns the number of keys in the current database

(6) Info: Get server information and statistics

(7) Monitor: Real-time dump of received requests

(8) Config get configuration item: Get Server configuration information

Config set configuration item value: Set Configuration item information

(9) FLUSHDB: Delete all keys in the currently selected database

Flushall: Remove all keys from all databases

Time: Displays server time, timestamp (seconds), microseconds

Bgrewriteaof: Save Rdb Snapshot in background

Bgsave: Save Rdb Snapshot in background

Save: Saves an RDB snapshot

(Lastsave): Last Save time

(shutdown) [Save/nosave]

Note: If you accidentally run the Flushall, immediately shutdown Nosave, shut down the server, and then manually edit the AoF file, remove the Flushall related lines in the file, and then turn on the server, you can rewind the original data. If the system happens to be bgwriteaof after Flushall, then the AOF is emptied and the data is lost.

(+) Showlog: Show Slow Query

Q: How slow is slow.

Answer: Specified by Slowlog-log-slower-than 10000 (in microseconds)

Q: How many slow query records the server stores

Answer: By Slowlog-max-len 128, to do the limit

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: info-contact@alibabacloud.com 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.