Test the existing Redis Data Structure

Source: Internet
Author: User

Data Structures Supported by redis: strings, lists, hashes, set, sorted set. Next we will do the experiment one by one.

Strings operations include get/set/setnx, append, strlen, getrange/setrange, incr/decr/incrby/decrby, mget/mset.

Get/set is to insert data (key-value corresponds ):

  1. Redis 127.0.0.1: 6379 [1]>Set test chen
  2. OK
  3. Redis 127.0.0.1: 6379 [1]>Set test2 zhou
  4. OK
  5. Redis 127.0.0.1: 6379 [1]>Get test
  6. "Chen"
  7. Redis 127.0.0.1: 6379 [1]>Get test2
  8. "Zhou"
Setnx is used to insert data to check whether the same key value exists. If yes, 0 is returned if no value exists. If no value exists, 1 is returned:
  1. Redis 127.0.0.1: 6379 [1]>Setnx test 11111
  2. (Integer) 0
  3. Redis 127.0.0.1: 6379 [1]>Get test
  4. "Chen"
Append is even simpler, that is, append characters used at ordinary times:
  1. Redis 127.0.0.1: 6379 [1]>Get test
  2. "Chen"
  3. Redis 127.0.0.1: 6379 [1]>Append test @ gmail.com
  4. (Integer) 14
  5. Redis 127.0.0.1: 6379 [1]>Get test
  6. Chen@gmail.com"
Getrange/setrange is a simple string operation that retrieves or sets the character at the relative position. There was a problem during the experiment here, mainly because we were not familiar with the setrange operation. Let's take a look at the following operation and we will understand that setrange is used to replace the number of corresponding positions, the location of 5 is @, and QQ.com is 6 characters to replace mysina. com10 characters can only Replace the first six.
  1. Redis 127.0.0.1: 6379 [1]>Set test3 chen@gmail.com
  2. OK
  3. Redis 127.0.0.1: 6379 [1]>Get test3
  4. Chen@gmail.com"
  5. Redis 127.0.0.1: 6379 [1]>Setrange test3 5 mysina.com
  6. (Integer) 15
  7. Redis 127.0.0.1: 6379 [1]>Get test3
  8. Chen@mysina.com"
  9. Redis 127.0.0.1: 6379 [1]>Setrange test3 5 QQ.com
  10. (Integer) 15
  11. Redis 127.0.0.1: 6379 [1]>Get test3
  12. Chen@QQ.com.com"
Incr/decr/incrby/decrby means that the incrby/decrby after auto-increment and auto-subtraction are added with the step size. If there is no key value, it can also be operated:
  1. Redis 127.0.0.1: 6379>Set age 20
  2. OK
  3. Redis 127.0.0.1: 6379>Incr age
  4. (Integer) 21
  5. Redis 127.0.0.1: 6379>Get age
  6. "21"
  7. Redis 127.0.0.1: 6379>Incrby age 5
  8. (Integer) 26
  9. Redis 127.0.0.1: 6379>Incrby ss 3
  10. (Integer) 3
  11. Redis 127.0.0.1: 6379>Incr tt 2
  12. (Error) ERR wrong number of arguments for 'inc' command
  13. Redis 127.0.0.1: 6379>Get tt
  14. (Nil)
Mget/mset indicates batch insertion and removal.

The basic operations of a hash table are the same as those of a string, such as hget/hset/hsetnx, happend, hstrlen, hgetrange/hsetrange, hincr/hdecr/hincrby/hdecrby, and hmet/hmset, just add H to the front. It is especially suitable for storing object data. The basic operation is the same as above. Here the key is used as the hash name. During the operation, you need to provide the hash field you want to insert:

  1. Redis 127.0.0.1: 6379 [1]>Hset myhash ID 001
  2. (Integer) 1
  3. Redis 127.0.0.1: 6379 [1]>Hset myhash name chen
  4. (Integer) 1
  5. Redis 127.0.0.1: 6379 [1]>Hset myhash address ncut
  6. (Integer) 1
  7. Redis 127.0.0.1: 6379 [1]>Hset myhash telephone 12332123
  8. (Integer) 1
  9. Redis 127.0.0.1: 6379 [1]>Hget myhash id
  10. (Nil)
  11. Redis 127.0.0.1: 6379 [1]>Hget myhash ID
  12. "001"
  13. Redis 127.0.0.1: 6379 [1]>Hget myhash name
  14. "Chen"
  15. Redis 127.0.0.1: 6379 [1]>Hget myhash0000 name
  16. (Nil)
Other operations are the same as above:
  1. Redis 127.0.0.1: 6379 [1]>Hkeys myhash
  2. 1) "ID"
  3. 2) "name"
  4. 3) "address"
  5. 4) "telephone"
  6. Redis 127.0.0.1: 6379 [1]>Hvals myhash
  7. 1) "001"
  8. 2) "chen"
  9. 3) "ncut"
  10. 4) 12332123"
  11. Redis 127.0.0.1: 6379 [1]>Hgetall myhash
  12. 1) "ID"
  13. 2) "001"
  14. 3) "name"
  15. 4) "chen"
  16. 5) "address"
  17. 6) "ncut"
  18. 7) "telephone"
  19. 8) "12332123"
  • 1
  • 2
  • Next Page

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.