Redis sort sort order using the method detailed

Source: Internet
Author: User
Tags hash min redis redis server

For the Redis sort order I'm sure we don't know much about it, so I've sorted out some Redis sort order using methods and examples, hoping the examples will help you guys.

Redis sort is a sort command provided by Redis. The tags in the collection are unordered and can be sorted by using sort. Such as:

Redis>sadd Jihe 5

(integer) 1

Redis>sadd Jihe 1

(integer) 1

Redis>sadd Jihe 2

(integer) 1

Redis>sadd Jihe 8

(integer) 1

Redis>sort Jihe

1) "1"

2) "2"

3) "5"

4) "8"

If you are using the Redis sort to sort the numbers, the letters, and rank them in the order of the dictionary, you need to use the

SORT Jihe ALPHA

If the alpha parameter is not added, an error is prompted: (error) ERR One or more scores can ' t is converted into double. We can also use the desc of relational databases for reverse ordering and limit offset count to qualify the number of fetched bars

SORT Jihe DESC LIMIT 0 2

You can also add a by parameter to the Redis sort command. A statement can have only one by parameter. At this point, sort does not sort by its own values, such as (1,5,2,8 and A,a,g,b), but by the fields in the specified other key. Such as:

SORT tag:redis:article by Article:*->time DESC

Explanation: According to the value in Tag:redis:article (tag is Redis's article ID), the combination of a new key is article: (a value in ag:redis:article): time. Get the list of article IDs that tag is Redis, and then sort according to their time of publication.

The Redis Sort command also has a get parameter that resembles an associated query in a relational database. For example, query tag is a list of Redis article IDs, sort the list by reverse order, and then get the title of each article. Get can have multiple:

SORT tag:redis:article by Article:*->time DESC get Article:*->title get Article:*->time Get #

Get #的意思是, return the article ID back, you can write get Article:*->id, you can also write get #.

The Redis Sort command also has an argument for the store, which stores the sorted content in a new key. The type of the new key is the list type, which is overwritten if it exists. This time you can use expire to set the cache:

SORT tag:redis:article by Article:*->time DESC get Article:*->title get Article:*->time get # STORE Resultkey

The Redis Sort command is one of the most complex and powerful commands in Redis, with a time complexity of O (N+MLOGM). n is the list length to be sorted, and M is the number of elements returned. Reducing n and M will improve the performance of sort.

  Add:

1, suitable for all types of orders

EXISTS key to determine whether a key exists; there is a return of 1; otherwise, return 0;

DEL key Deletes a key, or a series of Key;del Key1 Key2 Key3 Key4

Type key returns the data type of a key element (none: Not present, string: Character, List,set,zset,hash)

Keys pattern returns a matching key list (keys foo*: Find the keys at the beginning of Foo)

Randomkey randomly obtains an already existing key and returns an empty string if the current database is empty

RENAME oldname newname Change the name of the key, and the new key will be overwritten if it exists

Renamenx oldname newname Change the name of the key, if the name exists, the change fails

Dbsize returns the total number of key keys in the current database

EXPIRE Sets the expiration time (in seconds) of a key, (EXPIRE Bruce 1000: Set Bruce this key1000 seconds after the system is automatically deleted) Note: If the value has not expired, the values are changed, then the value will be cleared.

How long does the TTL find a key to expire, and returns the second time

Select Index Choose Database

Move key Dbindex moves the specified key from the current database to the target database Dbindex. Successful return of 1, otherwise return 0 (the source database does not exist key or the target database already has the same name key);

Flushdb empty all keys in the current database

Flushall clears all keys in all databases

2, commands for handling strings

Set key value sets a string value for a key. Set KeyName datalength data (set Bruce Paitoubing: Save Key is Burce, string length is 10 of a string paitoubing to the database), the data maximum can not exceed 1G.

Get key gets the value of a key. If the key does not exist, the string "nil" is returned and an error is returned if the value of the key is not a string type.

Getset key value can be understood as the value of the obtained key and set this value, more convenient operation (set Bruce Paitoubing, this time need to modify Bruce to 1234567890 and get the previous data paitoubing , Getset Bruce 10 1234567890)

Mget key1 Key2 ... keyn a one-time return value of multiple keys

The difference between Setnx key value Setnx and set is that set can create and update key value, and setnx if key does not exist, create key and value data

Mset key1 value1 key2 value2 ... keyn valuen set multiple keys and values one at a time under atomic operation

Msetnx key1 value1 key2 value2 ... keyn valuen set multiple keys and values at once under atomic operation (if the target key does not exist, it fails if more than one key already exists)

INCR key Self-add value

Incrby key integer To specify a value by itself

DECR key self-subtraction value

Decrby key integer To specify numeric values by self reduction

3, handling the lists command

Rpush key value adds an element from the end of the List (if the sequence does not exist, it is created first, an error is returned if a key of the same name already exists, not a sequence)

Lpush key value adds an element from the List header

Llen Key returns the length of a List

Lrange key start to return the elements of the sequence from a custom range (Lrange testlist 0 2; return sequence testlist top 0 1 2 elements)

LTRIM key start to trim data outside a range (LTRIM testlist 0 2; Keep 0 1 2 elements, delete the rest)

Lindex key index Returns the sequence value of a position (lindex testlist 0; Returns the element where the sequence testlist position is 0)

LSET Key index value updates the value of a location element

Lrem Key Count value deletes a certain number (count) of elements that match value from the head (count positive) or tail (count negative numbers) of the List, returning the number of deleted elements.

Lpop key pops up the first element of the List

Rpop key pops up the last element of the List

Rpoplpush Srckey Dstkey pops up the last element in the _SRCKEY_ and presses it into the _DSTKEY_ header and returns "nil" if the key does not exist or the sequence is empty.

4, processing set (sets) commands (indexed unordered sequence)

Sadd Key member adds an element to the sets sequence, and if the element (Membe) does not exist, add success 1, or failure 0; (Sadd testlist 3 n One)

Srem key member Deletes an element of the sets sequence, fails 0 if the element does not exist, or succeeds 1 (Srem testlist 3 N one)

Spop key randomly pops a member from the collection

Smove Srckey Dstkey member moves an element of a sets sequence to another sets sequence (Smove testlist test 3n Two; moves the element testlist from the sequence two to test. Two element is not present in testlist)

SCard key counts the number of elements in a sets sequence

Sismember key member to see if the specified members exist in the collection

Sinter key1 Key2 ... Keyn returns Key1, Key2, ..., intersection in Keyn

Sinterstore dstkey key1 key2 ... Keyn will Key1, Key2, ..., Keyn in the intersection deposit Dstkey

Sunion key1 Key2 ... Keyn returns Key1, Key2, ..., Keyn of the collection

Sunionstore Dstkey key1 Key2 Keyn will Key1, Key2, ..., Keyn and set deposit in Dstkey

Sdiff key1 Key2 ... keyn according to Key2, Keyn seek the difference set of Key1. Official Example:

Key1 = X,a,b,c

Key2 = C

Key3 = A,d

Sdiff Key1,key2,key3 => x,b

Sdiffstore Dstkey key1 Key2 Keyn According to Key2, ... Keyn the difference set and deposit Key1

Smembers key returns all elements of a sequence

Srandmember key randomly returns elements of a sequence

5, processing ordered set (sorted sets) command (zsets)

Zadd key score member to add specified members to an ordered set and update score if the target exists (score, sort)

Zrem key member deletes specified members from an ordered collection

Zincrby Key Increment member adds _increment_ if a member is present, or it sets a score as _increment_

Zrange Key start end returns a member of the specified range after ascending sort

Zrevrange Key start end returns a member of the specified range after descending sort

Zrangebyscore key min Max returns all member <= key that conforms to score >= min and score Zcard Max returns an ordered set of elements Zscore key element returns the score value of the specified member Zremrangebyscore Key min Max deletes all members that meet score >= min and score <= max conditions

6,hash type

Command Parameters Description

Hset key field value Set the hash field to the specified value. Creates the hash if needed.

Hget key field Retrieve The value of the specified hash field.

Hmget key field1 ... fieldn get the hash values associated to the specified fields.

Hmset key field1 value1 ... fieldn valuen Set the hash fields to their values.

Hincrby key field integer Increment the integer value of the hash at key in field with integer.

Hexists key field Test for existence the a specified field in a hash

Hdel key field Remove the specified field from a hash

Hlen key return the number of items in a hash.

Hkeys Key return all of the fields in a hash.

Hvals Key return all of the values in a hash.

Hgetall key return all the fields and associated values in a hash.

7, sort (List, set, Sorted set)

The SORT key by pattern LIMIT The start end get pattern asc| DESC ALPHA to sort the collection or list in the specified mode

SORT mylist DESC Descending

SORT mylist LIMIT 0 10 starting from ordinal 0, taking 10 strips

Sort mylist LIMIT 0 ALPHA DESC sorted by first character

SORT MyList by weight_* store Resultkey store returned results in Resultkey sequence (List)

8, persistent control

Save synchronize data to disk

Bgsave asynchronously saves data to disk

Lastsave returns the Unix timestamp that was last successfully saved to disk

SHUTDOWN sync to the server and shut down the Redis server (save+quit)

Bgrewriteaof the log file when it is too long

9, REMOTE control command

Info provides information and statistics about the server

Monitor to output all incoming requests in real time

slaveof Modify Replication Options

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.