[Redis] Five data types for Redis with key-value/server-related commands

Source: Internet
Author: User
Tags redis server

--------------------------------------------------------------------------------------

String (String): The simplest data type.

Set age 18

(set Age ex 5) # Set expiration time 5 seconds

Setex age 5 18 # Effect Same as above, use TTL to see the remaining valid time

Psetex Age 5000 18 # Ibid, just set the expiration time of key in milliseconds

(set age NX) # Set if the age key does not exist

Setnx age 18 # Effect Ditto

Get age

    

(Set email Chenw (a) 59store.com)

SetRange Email 9 live.com # Replace the value of the key with the value from the 6th position, and the result is [email protected], and the extra one will not replace

  

Mset Key1 a Key2 B # set one or more values at once, using get

Msetnx Key1 C # If the key does not exist, set one or more values

Getset email Farwish (a) Live.com # key set to the specified value and return the old value

GetRange Email 3 6 # Returns the key from 3 to 6 substrings, gets wish

Mget Key1 Key2 # Returns the value of all given keys

INCR Age # increases the value of the key by 1, returns the new value, or sets the key when it does not exist

Incrby Age 3 # Sets the value of the key to increment the specified value (can be negative), returns the new value, or set when the key does not exist

Incrbyfloat Age 0.5 # adds floating-point increment value to the value of key

DECR Age # Self minus 1

Decrby Age 3 # Self-decrement specified value

Append email. CN # Append value to the end of the original key value

strlen Email # Returns the string length stored by the key

Bitcount e-Mail # Calculates the number of bits that are set to 1 in a given string

Bitop Operation Deskkey Key1 Key2 # Bitwise operation of one or more saved bits string key, and save the result to Destkey

Setbit key Offset Value # Sets or clears a bit (bit) on the specified offset for the string value stored by key

Getbit Key Offset # The string value stored on key to get the bit (bit) on the specified offset

Hash (hash table): Applies to storage objects, consuming less memory than storing a single string.

Hset user:1 Name Farwish # Sets the name value in the hash table to farwish, overwriting when it already exists

Hget user:1 Name # Remove the name value from the hash table

HSETNX user:1 Name # Set if not present, return 0 if present

  

Hmset user:2 name Weichen age 20 # Set multiple values to the hash table at the same time

Hmget user:2 name Age # Bulk Get values for multiple fields

  

Hincrby User:2 Age 5 # adds 5 to the field age in the hash table

Hincrbyfloat User:2 Age 0.5 # allows adding floating-point values to fields in a hash table

Hexists user:2 Name # Determine if a field exists in the hash table

Hlen User:2 # Returns the number of fields in the hash table

  

Hdel user:2 Age # Remove a field from a hash

Hkeys user:2 # Returns all fields in the hash table

Hvals User:2 # Returns all the values in the hash table

Hgetall user:1 # Returns all fields and values in the hash table

  

Hscan # Key-value pairs in iterated hash keys, usage with scan

List: Linked list structure, each child element is a string type of doubly linked list, by push, pop from the end of the list of links to add delete elements, either as a stack (advanced post-out), or as a queue (FIFO).

Lpush MyList a b C # presses one or more value on the head of the list, returns the number of elements, and creates and executes the Lpush operation if the key does not exist

Lpushx Nolist A # differs from Lpush in that (1) can only be pressed into a value (2) When key does not exist, do nothing

Lrange MyList 0-1 # Returns the element of the specified interval in the list, 0 for the first, 1 for the last, 2 for the penultimate

Rpush MyList A # Press one or more value;c B a A in the tail of the list

Rpushx MyList # differs from Rpush in that (1) can only be pressed into a value (2) When key does not exist, do nothing

Linsert MyList before a B # insert B into the list before A; c b b a A

(Linsert mylist after a b) # Insert B into the linked list after A; c b b a a B

LSet mylist 0 C # Set the value of MyList subscript to 0 to C;c b b a A B

Lrem MyList 2 B # (remove) deletes the 2 list of elements equal to B, if the number is 0 to remove all B, returns the number of successful deletions; C B a A

LTrim mylist 1-1 # keeps the elements of the specified interval range within the list, and the remainder is deleted; b a A

Lpop MyList # Delete and return the header element of the list; a

Rpop MyList # Delete and return the trailing element of the list; a

Rpoplpush MyList MyList # pops an element from the tail and presses it from the beginning; a

(Lpush list1 a B C; Rpoplpush list1 mylist) pops the element from the List1 tail and presses it into the mylist head; a

Lindex MyList 1 # Returns the element labeled 1 in the list, no return nil

Llen MyList # Returns the number of elements in the list; 2

Blpop Key Timeoutseconds # Lpop the blocked version of the command, there is no element in the list when the connection will block until the wait time-out or to find a popup element; Given multiple key parameters, the Blpop is ordered once by the given key parameter The same key can be blocked by multiple clients at the same time, the key is null to return nil, otherwise the key of the popup element and the element being popped are returned.

(Blpop List2 5) because the List2 does not exist, after 5 seconds will time out;

(Blpop list2 mylist 2) starts to block at List2, until MyList, pops up A;

Brpop key Timeout # with Blpop

Brpoplpush List1 a 5 # Rpoplpush blocked version, when the list is non-empty, behaves as if the Rpoplush, when the list is empty, Brpoplpush will block the connection until it expires or another client executes the Lpush or Rpush command

Set (SET): An unordered set of string types, implemented by a hash table, can take the set, intersection, and difference sets of the set.

Sadd SNS crazysns.cn farwish.com # adds one or more elements to the collection SNS, ignoring if the element already exists

Smembers SNS # Returns all members in the collection

Srem SNS crazysns.cn # removes one or more elements from the collection, does not ignore, does not set the error; Returns the number that was successfully removed

(Sadd SNS weibo.com, Sadd SNS qq.com, Sadd SNS renren.com, Sadd SNS fb.com)

Spop SNS # removes and returns a random element in the collection

(Sadd sns2 qq.com t.com)

Sdiff SNS Sns2 # Take the difference set, return the SNS not in the SNS2 element, take the first key as the standard

Sdiffstore sns3 SNS Sns2 # Similar to Sdiff, just saves the difference between SNS and Sns2 to SNS3, returning the number of successful saves

Sinter SNS SNS2 # Fetch intersection (INTERSECT), returns the list of intersection elements

Sinterstore sns4 SNS Sns2 # Similar to sinter, just saves the intersection of SNS and Sns2 to SNS4, returning the number of successful saves

Sunion SNS Sns2 # take the set and return all members

Sunionstore sns5 SNS Sns2 # Similar to Sunion, just saves the intersection of SNS and Sns2 to SNS5, returning the number of successful saves

Smove SNS Sns2 weibo.com # Moves the element weibo.com in the SNS to SNS2, successfully returns 1

SCard SNS # Returns the number of elements in a collection

Sismember SNS qq.com # Determines if qq.com is an element of SNS, is returned 1, not returned 0

Srandmember SNS # randomly returns an element

Scan 0 # Iterates through the database keys in the current database, returns a subset of the collections, returns 0 and a list of collections if they are all listed, returns the next cursor value and the current collection list if only a subset is listed; An with cursor record position, when the appropriate data volume is large, instead of keys or smembers, but there will be data is displayed or updated, can only guarantee that the current displayed data accurate.

  

SortedSet (Ordered collection): Set of the upgraded version, on the basis of the set to increase the order of properties, similar to a column sort field

Zadd Company 1 Apple 2 Google 3 ms # adds one or more elements and sequential values to the collection, and only updates the order value if an element exists

Zrange company 0-1 Withscores # Displays the first to last element and sequence number in the collection, without withscores, without displaying the sequence number

Zrem company Apple Ms # removes one or more elements from an ordered collection, successfully returning the number of deletions, excluding ignored members

Zincrby Company 3 Google # adds an increment value to an ordered set member sequence number, returning a new sequential value

(Zadd company 3 Twitter)

Zrank company Google # returns the index of the specified element in the ordered collection, sorted from small to large in order; 1

(Zadd company ebay)

(Zrank company Google) # 2

Zrevrank company Google # returns the index of the specified element in the ordered collection, in order from large to small; 0

Zrevrange company 0-1 Withscores # Zrange in descending order

  

Zrangebyscore Company 3 4 # returns elements from small to large in a specified sequence range

Zrevrangebyscore Company 4 3 # returns elements from large to small in a specified order interval

Zcount Company 3 4 # Returns the number of elements in the specified sequence range

Zcard Company # Returns the number of all ordered collections

  

Zremrangebyrank Company 2 2 # Delete members within an indexed range in an ordered collection

Zremrangebyscore Company 4 5 # Delete a member in an ordered set within a sequence range

  

Zscore company Google # Order number of ordered collection members

(Zadd CP GG)

Zunionstore all 2 company CP # saves 2 sequential collections of the company and CP in the all collection

(Zunionstore all 2 company CP weight 1 3) # Weight is used to add a factor to the corresponding set, that is, the sequence number of elements in the CP ordered set is multiplied by 3

(Zadd min A; Zadd min b; zadd min C;)

(Zadd max A; Zadd Max B; zadd Max;)

(Zadd mid-A; zadd mid-B; zadd mid-C;)

Zinterstore sum 2 min Max # calculates the intersection of a given ordered set, 2 is the number of collections, and the intersection is stored in the sum

  

(Zinterstore sum2 3 min Max mid) # If the number is incorrect, you will get an error.

  

  

Zscan # Usage with scan

  

(zadd Myzset 0 B 0 A 0 D 0 C 0 E 0 F)

Zrangebylex MySet-+ # Sorts an ordered set of elements within a specified range, and if the score value is the same, it is sorted according to the dictionary order of the members;-denotes negative infinity, + denotes positive infinity, (denotes open interval, [denotes closed interval

  

  

  

Zlexcount Myzset-+ # Returns the number of members in an ordered set between-to +; 6

Zremrangebylex Myzset [A (C #) Removes a continuous set of members from [A to (c), returning the number of successfully removed; 2

-------------------------------------farwish.com---------------------------------------

 

Related commands for keys

Keys * # returns all keys that match the matching pattern

(Keys sns*)

Exists MyList # Check if a key exists, there is a return of 1, there is no return 0

Del mylist SNS # deletes one or more keys, successfully returns the number of deletions, fails to return 0

Expire SNS 5 # Set the expiration time of a key (in seconds)

Pexpire SNS 5000 # function Ibid. (in milliseconds expires)

Pexpireat SNS 1231312331 # Set key expiration Unix timestamp in milliseconds

Persist SNS # Removes the expiration time for a given key, that is, cancels the expiration setting, at which time the TTL SNS returns-1 indicates that the expiration date has been successfully removed

TTL SNS # View the remaining lifetime (seconds) of a key, 1 indicates expiration

Pttl SNS # Returns the remaining life time of key in milliseconds

(select 0) # 0 database selected, default of 0-15 total 16 databases

Move SNS 1 # moves SNS to database 1;

Radomkey # Returns a random key

Rename min miin # rename a key; successful return 1

Renamenx min Max # Performs a rename only if the new key name does not exist; return 0 already exists

Type Myzset # Returns the types of values stored by a key; Zset

Scan # iterates through the database keys in the current database and returns a subset of the collections; See the String type section above for details

Dump Myzset # Serializes the given key, returning the serialized value

Restore Myzset-again 0 "\xoc\x1" # deserializes the given serialization value for a Redis key, the Restore Key TTL Serialized_value (TTL is milliseconds, 0 does not set the expiration time)

Migrate 192.168.1.11 6379 SNS 0 1000 # milliseconds timeout time to transfer key atomicity from the current instance to the specified database of the target instance, the key appears on the target instance, the key of the current instance is deleted, and only three results are found: migration succeeded, Migration failed, wait timeout; Migrate host port key DB timeout

Object

Sort # Returns or saves the ordered elements of the given list, collection, and ordered set of keys; Sort the default Sort object is a number, sorting the string requires displaying the add Alpah modifier

(sort [get pattern] [limit] [get pattern] [ASC|DESC] [store destination])

(Lpush number 1 4 10 3)

(sort number) # 1 3 4 10

(Sort number desc) # 10 4 3 1

(Lpush site www.baidu.com www.z.cn www.farwish.com)

# using the Alpha modifier

(sort site Alpha) # www.baidu.com www.farwish.com www.z.cn

# using the Limit modifier

(Sort number Limit 0 2) # 1 3

(Sort number Limit 3 4) # 10

# Use external key to sort;

(Lpush UID 1)

(Set User_name_1 user1)

(Set User_level_1 999)

(Lpush UID 2)

(Set user_name_2 user2)

(Set user_level_2 777)

(Lpush UID 3)

(Set User_name_3 User3)

(Set User_level_3 888)

(Lpush UID 4)

(Set User_name_4 user4)

(Set User_level_4 889)

(sort uid) # 1 2 3 4

# with the by option, let the UID be sorted by the elements of the other keys

(Sort uid by user_level_*) # 2 3 4 1 (sorted by Level value 777 888 889 999)

# using the Get option, you can remove the corresponding key value based on the sorted result

(Sort uid Get user_name_*) # user1 User2 User3 user4

# Combine by and get to make sorting results appear in a more intuitive way

(Sort uid by user_level_* get user_name_*) # User2 User3 User4 user1

# Use multiple get options at the same time to get the values of multiple foreign keys, which can get the value of the sorted key

(Sort uid Get user_level_* get user_name_*) # 999 user1 777 user2 888 user3 889 USER4

(Sort uid Get # get user_level_* get user_name_*) # 1 999 user1 2 777 user2 3 888 user3 4 889 user4

# Gets the foreign key but does not sort; Lets sort skip the sort operation by passing a non-existent key as a parameter to the by option

(Sort uid by user_nick_* get # get user_level_* get user_name_*)

# use hash tables as parameters for by and get

(Hmset user_info_1 name U1 age 9)

(Hmset user_info_2 name U2 age 7)

(Hmset user_info_3 name U3 age Level 8)

(Hmset user_info_4 name U4 age level 6)

(Sort uid by User_info_*->level) # 4 2 3 1

(Sort uid by User_info_*->level get User_info_*->name) # U4 U2 U3 U1

# Save the sort results (you can set expire as the cache for the sort results, which avoids frequent calls to sort, while avoiding multiple clients for cache rebuilds, consider using SETNX)

(Sort number Store stored_numbers)

(Lrange stored_numbers 0-1) # 1 3 4 10

  

Server-related commands

Ping # Detects if the Redis connection is valid and returns Pong

echo ' message ' # prints specific information, tested with

Auth 123456 # After entering the client, enter the password to use

Select 0 # Selects the database of the specified index, starting at 0

Quit # Quit client connections

Dbsize # Returns the number of all keys in the current database

Info # returns various information and statistics for Redis server

Config Get * # returns configuration parameters for Redis in operation

(Config get timeout)

Flushdb # Empty The current database all keys

Flushall # empty all databases all keys

  

Link:http://www.cnblogs.com/farwish/p/4712377.html

@ Black eyed poet <www.farwish.com>

[Redis] Five data types for Redis with key-value/server-related commands

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.