Summary of Redis Common command usage

Source: Internet
Author: User

Redis is a key-value storage system. Similar to memcached, it supports storing more value types, including string (string), list (linked list), set (set), Zset (sorted set-ordered collection), and hash (hash type). These data types support Push/pop, Add/remove, and intersection-set and difference sets, and richer operations, and these operations are atomic. Based on this, Redis supports sorting in a variety of different ways. As with memcached, data is cached in memory to ensure efficiency. The difference is that Redis periodically writes the updated data to disk or writes the modified operation to the appended record file, and Master-slave (Master-Slave) synchronization is implemented on this basis.

Redis is a high-performance Key-value database. The emergence of Redis, to a large extent, compensates for the lack of memcached such key/value storage, in some cases can be a good complement to the relational database. It provides clients such as Java,c/c++,c#,php,javascript,perl,object-c,python,ruby,erlang, which is convenient to use.

Redis supports master-slave synchronization. Data can be synchronized from the primary server to any number of slave servers, from the server to the primary server that is associated with other slave servers. This enables Redis to perform single-layer tree replication. You can write to the data intentionally or unintentionally. Because of the full implementation of the publish/subscribe mechanism, you can subscribe to a channel and receive a complete message release record from the master server when the tree is synchronized anywhere from the database. Synchronization is helpful for the scalability and data redundancy of read operations.

The Redis common data types are as follows:

The five data types used by Redis are String,hash,list,set,zset (sorted set)

1. String type

String is the simplest type, a key corresponding to a value, the maximum data 1G, the value can be considered as an integer, so that the "INCR" command family operations (Incrby, DECR, Decrby), in this case, The integer value is limited to a 64-bit signed number. The separate element types contained in list, set, and Zset are Redis string types.

2. Hash type

Hash is the data type closest to the relational database structure, which can convert one object in a database or program into a hashmap stored in Redis.

Action commands

Commands for the value operation

Exists (key): Verify that a key exists del (key): Delete a key type (key): The type of the return value keys (pattern): Returns all key randomkey that satisfy a given pattern: randomly returns one of the key spaces   Key rename (Oldname, newname): Rename key from Oldname to NewName, delete newname if newname exists dbsize: Returns the number of keys in the current database Expire: Setting a Key's active time (s) TTL: Get the active time of a key Select (index): Query by index Move (key, Dbindex): Transfers the key in the current database to the database with the Dbindex index flushd B: Delete all keys in the currently selected database Flushall: Delete all keys in all databases

Commands for string manipulation

     set (Key, value): Assigns a value value   get (key) to a string named key in the database : Returns the Value   getset (Key, value) of a string with the name key in the database: assigns the last value   to a string named key  mget (Key1, key2,...,  key n): Returns multiple strings in the library (their names are key1,key2 ... Value   setnx (Key, value): If there is no string with the name key, add a string to the library with the name key and the value value    setex (Key, time, value): Adds a string (named key, value) to the library and sets the expiration Time Time   mset (Key1,  value1, key2, value2,... key n, value n): Assign values to multiple strings at the same time, string assigned value named Key i  i   msetnx (key1, value1, key2, value2,... key n, value n) : If all strings with the name key i do not exist, add a string to the library with the name Key i assigned to VALUE I   INCR (key) : string with the name key is incremented by 1 operations    incrby (Key, integer): string with the name Key added INTEGER   DECR (key ): string minus 1 operation named Key    decrby (Key, integer): string with the name key reduced inTeger   append (Key, value): The value of string named key is appended with the VALUE   SUBSTR (Key, start,  end): Returns a substring of the value of string with the name key

Commands for list operations

     rpush (Key, value): Adds a value of element    lpush (key) at the end of the list named key,  value): Adds a value of   element    llen (key) to the list header with the name key: Returns the length of the list named key     Lrange (Key, start, end): Returns the element between start and end in list named key (subscript starting from 0, same as below)    ltrim (key,  Start, end): Intercepts the list named key, preserving the element between start and end    lindex (Key, index) : Returns the element    lset (key, index, value) of the index position in the list named key: assigns the element of the index position in the list named key to value    lrem (Key, count, value): Deletes an element with a value of count in the list named key. Count is 0, removing all elements that have values of value, Count>0 removes the element with count values from start to end, Count<0 removes |count| values as value from the tail.    lpop (Key): Returns and removes the first element of the list named key    rpop (key): Returns and removes the tail element in the list named key     blpop (Key1, key2,...  key n, timeout): The block version of the Lpop command. That is, when timeout is 0 o'clock, the command ends if a list with the name Key i does not exist or the list is empty. If timeout>0, if this is the case, wait for timeout seconds, and if the problem is not resolved, execute the PO for the list starting with key i+1P operation.    brpop (Key1, key2,...  key n, timeout): Block version of Rpop. Refer to the previous command.    rpoplpush (Srckey, dstkey): Returns and removes the tail element of the list named Srckey and adds the element to the head of the list named Dstkey

Commands for set operations

     sadd (Key, member): Adds an element member     srem to a set named key ( Key, member) &nbsp: Delete the element in the set named Key Member     spop (key)   : Randomly returns and deletes an element in the set named Key      smove (Srckey, dstkey, member)   : Move the member element from the collection named Srckey to the collection named Dstkey      scard (Key)  : Returns the cardinality of the set named key       sismember (Key, member)  : Test whether member is a set element with the name key       sinter (Key1, key2,... key n)  : Intersection      sinterstore (dstkey, key1 ,  key2,... key n)  : Intersection and save the intersection to the Dstkey collection      sunion (Key1, key2,... key  n) &nbsp:      sunionstore (Dstkey, key1, key2,... Key N)   : Set and save the set to Dstkey collection      sdiff (Key1, key2,... key n)  : Differential set       sdiffstore (Dstkey, key1, key2,... kEy n)  : Differential set and save the difference to Dstkey collection      smembers (key)  : Returns all elements of a set with the name key       srandmember (key)  : Randomly returns an element of a set with the name key

Commands for Zset (sorted set) operations

     zadd (Key, score, member): Adds an element member,score for sorting to a zset named key. If the element already exists, the order of the element is updated according to score.      zrem (Key, member)  : delete element in Zset with name Key member      zincrby (Key, increment, member)  : If the element member already exists in Zset with the name key, the score of the element increases increment Otherwise, add the element to the collection, whose score value is Increment     zrank (key, member)   : Returns the rank of the member element (that is, index, starting from 0) of the Zset with the name key (the element has been sorted score from small to large), and returns "nil" if there is no member element       Zrevrank (key, member) &nbsp: Returns the rank (that is, index, starting from 0) of the score element in the Zset with the name key (the element has been sorted by member from large to small), and returns "nil" if there are no member elements      zrange (Key, start, end) : Returns the index of Zset with the name key (the element has been sorted score from small to large) from start to end of all elements      zrevrange (Key, start,  end): Returns all the elements of the index from start to end of Zset with the name key (the element has been sorted by score from large to small)      zrangebyscore (key ,  min, max): Returns Zset in Score >= min with the name Key and SCORE&NBSP;&LAll elements of T;= max      zcard (key): Returns the cardinality of the Zset with the name Key      zscore (key, element): Returns the Score     zremrangebyrank (key, min) of element elements in Zset with the name Key,  max): Remove all elements of rank >= min and Rank <= max in Zset named Key       Zremrangebyscore (Key, min, max)  : Delete score >= min in Zset with name Key and score <=  All elements of Max      zunionstore / zinterstore (Dstkeyn, key1,..., keyN,  WEIGHTS&NBSP;W1,... wn, aggregate sum| min| MAX): Sets and intersections of N Zset, and saves the last collection in Dstkeyn. For the score of each element in a collection, multiply the weight parameter for the aggregate operation before doing it. If weight is not provided, the default is 1. The default aggregate is sum, that is, the score of the elements in the result set is the value of the sum operation for all the corresponding elements of the collection, while Min and Max means that the score of the elements in the result set are the minimum and maximum values in the corresponding elements of all the collections.

Commands for hash operations

     hset (Key, field, value) : add element Field<->value     hget (Key, field) to hash with name key : Returns the Value     hmget (key, field1, ...,field n) of field corresponding to the hash named key : Returns the Value     hmset of field i corresponding to the hash named key (key, field1, value1,..., field  n, value n): Adds an element to a hash named key field i<->value i      Hincrby (Key, field, integer): Adds the value of field in the hash named key integer     hexists ( Key, field): Whether there is a field      hdel (Key, field) in the hash with the name key : Delete the field      hlen (key) with the key field in the hash named key: Returns the number of elements in the hash named key       hkeys (Key): Returns all keys      hvals (key) in a hash with the name key: Returns the value  of all keys in the hash named key     hgetall (Key): Returns all keys (field) and their corresponding value 
in a hash with the name key.

2. List Type

Linked list type, the main function is push, pop, get all values of a range and so on. The key can be understood as the name of the linked list. In Redis, lists are the list of Redis strings, sorted by insert order. For example, using the Lpush command to insert an element in the list header, use the Rpush command to insert an element at the end of the list. When one of these two commands acts on an empty key, a new list is created. The maximum length of a list is 2^32-1 elements.

3. Set type

The collection type is similar to the set concept in mathematics, where key in action is understood as the name of the collection. In Redis, set is an unordered collection of redis strings, and no duplicate elements are allowed. The maximum number of elements of set is the operation of set in 2^32-1,redis and the intersection, the set, the difference set and so on.


4, Zset (Sorted Set) type

Zset is an upgraded version of Set, which adds a sequential attribute on the set, which can be specified when a modification element is added, and Zset automatically installs the specified value in the reorder order after each assignment. It can be understood as a table, a column of value, and a list of stored orders. The key in the operation is understood as the name of Zset. The maximum number of elements in the Zset is 2^32-1, and for an already ordered zset, you can still use the sort command by specifying asc| The desc parameter to sort it.

Summary of Redis Common command usage

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.