Data types and directives for Redis

Source: Internet
Author: User
Tags delete key

1. Global key operation:

Test instructions:

Global Key Action command: ignores the type of value associated with key delete Flushdb  clears the currently selected database del MyKey mykey2  deleted two keys change move Mysetkey 1 the   current database Mysetkey key is moved into the database with ID 1 rename mykey mykey1 renamed MyKey Mykey1renamenx oldkey newkey   If Newkey already exists, it is invalid expire my Key to   set the time-out of the key to 100 seconds persist MyKey The  timeout of the key, and becomes the persistent key  my*  gets all keyexists mykey< in the current database that begin with my c11/> if not present, returns 0, existence returns 1select 0   open the database ttl with ID 0 mykey  View inventory time How many seconds remaining type MyKey  returns the type of MyKey corresponding value Randomkey  returns any key in the database
2.String (String)

The string is the most basic type of redis, and a key corresponds to a value.

The string type is binary safe. This means that a Redis string can contain any data. For example, JPG images or serialized objects.

The string type is the most basic data type of Redis, and a key can store up to 512MB.

The process of testing the instructions is not posted, the following is the test instructions

Add set MyKey"Test"set a new value for the key and overwrite the original value Getset mycounter0set the value and take the value at the same time Setex MyKeyTen "Hello"Setting the expiration time for the specified Key is 10 seconds and can be obtained valuesetnx MyKey at the time of survival"Hello"if the key does not exist, set the new value for the key Mset Key3"Stephen" Key4 "Liu"Bulk Set Keys
Delete del MyKey remove an existing key
Change Append MyKey"Hello"if the key does not exist, returns the length of the current value that the key already exists, returns the length of the appended value incr MyKey value increases by 1, if the key does not exist, creates a key, the initial value is set to 0, and the result is 1decrby Myke Y5value reduced by 5setrange MyKey20DD replaces the 21st and 22 bytes with DD, exceeds the value length, and automatically complements 0 exists MyKey to determine whether the key exists, the existence of a return1, otherwise returns 0get MyKey gets the key corresponding to the Valuestrlen MyKey gets the character length of the specified key TTL mykey Check the remaining surviving time (in seconds) of the specified key GetRange MyKey1 20gets the 2nd to 20th byte, and if 20 exceeds the value length, intercept 2nd and all subsequent mget Key3 key4 bulk Fetch key

3.Hashes type: (map<string,string>)

We can consider the hashes type in Redis as a map container with string key and string value.

Therefore, this type is ideal for storing information about value objects. such as username, password, and age. If a hash contains very few fields, the data of that type will take up only a small amount of disk space.

Each hash can store 4,294,967,295 key-value pairs.

Test instructions:

Case Explanation: Map type:    hset myhash field1 "s"      redis.key=myhash   redis.value= (map.key=field1   map.value=s)     Add Hset myhash field1 "s"    if the field field1 does not exist, create the key and its associated hashes, hashes, key is field1, and set value to S, if the field field1 exists, it is invalid                   hsetnx Myhash field1 s    if the field field1 does not exist, create the key and its associated hashes, hashes, key is field1, and set value to S, if the field field1 exists, then invalid Hmset myhash field1 " Hello "Field2" World   One-time set multiple fields delete Hdel myhash field1   Delete myhash key field named Field1 Fields del myhash  Delete key change  Hincrby Myhash Field 1  adds 1 to the value of field Hget Myhash field1   Gets the value   of Myhash, field field1 Hlen Gets the number of fields for the Myhash key hexists myhash field1     Determine if there is a  field named Myhash in the Field1 key hmget myhash field1 field2 field3 Get multiple fields at once Hgetall myhash   returns all fields and their values for the Myhash key Hkeys Myhash  Gets the name of all fields in the Myhash key hvals myhash   get Myhash The values of all the fields in the key

4.List Type:

The list type is a list of strings sorted by insertion order. Like a regular list in a data structure, we can add new elements to their head (left) and tail (right). When inserting, if the key does not exist, Redis creates a new linked list for that key. Conversely, if all the elements in the list are removed, the key will be removed from the database as well.

The maximum number of elements that can be contained in a list is 4294967295.

Test instructions:

List type: (linked list: Last inserted element, position index O)
Lpush MyKey a B if key does not exist, create the key and its associated list, insert a, B, if the list type key exists, insert value
Lpushx Mykey2 e If key does not exist, this command is invalid, if key exists, insert value
Linsert MyKey before a A1 insert a new element in front of a A1
Linsert MyKey after e E2 inserts a new element after E E2rpush MyKey a B inserts a b at the end of the list, inserts the ARPUSHX MyKey e If key exists, inserts E at the end, and if key does not exist, the RPO is invalid Plpush MyKey Mykey2 The tail element of the MyKey is ejected and then inserted into the Mykey2 's head (atomic operation) Delete del MyKey remove the existing key Lrem MyKey 2 A from the head to find, in order, The element with a value of a, the number of deletions is 2, if there is a 3rd, then do not delete LTrim MyKey 0 2 from the beginning, the index is 0,1,2 3 elements, the rest of all delete LSet MyKey 1 E from the beginning , the index is 1 of the element value, set to the new value E, If the index is out of bounds, an error message is returned Rpoplpush MyKey MyKey moves the trailing element in the MyKey to its head. Lrange MyKey 0-1 The entire list of elements, with 0 representing the first element, 1 representing the last element.
Lrange MyKey 0 2 starting from scratch, take an indexed element of 0,1,2
Lrange MyKey 0 0 from the beginning, take the first element, starting from No. 0, to the No. 0 end of the Lpop MyKey get the head element, and eject the head element, out of the stack lindex MyKey 6

5.set Type:

The set type looks as if the character set is not sorted. The maximum number of elements that a set can contain is 4294967295.
If you add the same element more than once, only one copy of the element will be retained in set.

Test instructions:

Add Sadd MySet a b c  if key does not exist, create the key and its associated set, insert a, B, if key exists, insert value, if a is already present in MySet, then insert D and e two new members.
Delete Spop MySet The tail of B is removed, in fact B is not the first or last member inserted before Srem MySet a D f if F does not exist, remove a, D, and return 2 smove myset Myset2 A will be a from MySet Move to Myset2, check Sismember myset A to determine if a is already present, and a return value of 1 indicates existence. Smembers myset View the contents of a set SCard MySet Gets the number of elements in the set collection Srandmember MySet randomly returns a member Sdiff Myset1 Myset2 Myset3 1 and 2 Get a result, take this set and 3 comparison, get each unique value
Sdiffstore diffkey myset Myset2 myset3 3 sets and comparisons, gets the unique element, and deposits Diffkey sinter MySet in the associated set. Myset2 Get elements from 3 collections
Sinterstore interkey myset Myset2 myset3 Place intersection in Interkey associated set sunion myset Myset2 Myset3 Gets the of the members of the 3 collection
Sunionstore unionkey myset Myset2 myset3 put the set into the Unionkey associated set

6.sorted-sets Type:

Each member of the sorted-sets has a fraction (score) associated with it, which is the small-to-large ordering of the members in the collection through fractions. Members are unique, but fractions (score) can be duplicated.

Test instructions:

Score: Sort position index by score: The lowest index of the score is 0 Zadd myzset 2 "two" 3 "three" add two fractions 2 and 3 members respectively by deleting Zrem myzset One two deleting multiple member variables, returning  The number of deletions changed Zincrby Myzset 2 One adds a member's score by 2 and returns the member's updated score Zrange Myzset 0-1 Withscores returns all members and fractions, without withscores, returning only members Zrank Myzset one gets the position index value of member one in Sorted-set. 0 represents the first position Zcard Myzset gets the number of members in the Myzset key Zcount Myzset 1 2 Gets the score satisfies the expression 1<= Score<= 2number of members Zscore Myzset three gets the score of the member three Zrangebyscore Myzset (1 2 Gets the score satisfies the expression 1 < score <= 2the member #-inf represents the first member, +inf the last member #limit the Limit keyword # 3 is the index number zrangebyscore myzset-inf +inf limit 2 3 The returned index is a member of 2 and 3 Zremran Gebyscore Myzset 1 2 delete score 1<= Score<= 2member and returns the actual number of deletions Zremrangebyrank myzset 0 1 Delete location index satisfies expression 0 <= Rank<= 1members of the Zrevrange Myzset 0-1 withscores by position index from high to low, get all members and fractions #原始成员: Position index from small to large one 0 the order of execution: reverse the index Location index: From big to small one 1 0# output result:one zrevrange myzset 1 3 Gets the position index, in the opposite order of the members of the three-way: from high to low order Zrevrangebys Core Myzset 3 0 get score 3>the members of the =score>=0 and output zrevrangebyscore in reverse order myzset 4 0 Limit 1 2 Gets the members of the index 1 and 2 and reverses the position index


Data types and directives for Redis

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.