The next entry Redis

This series is the content of the second part of the series of entry Redis: Redis introduces data types: String, Hash, List, Set and zset

Redis

Redis data types

Command Daquan official website

Directives URL

  • String (String Type)
  • hash (hash type)
  • List (list type)
  • Set (collection type)
  • SortedSet (indexed collections, referred zset)

Commands are not case sensitive, but the key is case sensitive

String type

  • Assignment:

    SET key value

  • Value:

    GET key

  • And assign values:

    GETSET key value

  • Increase or decrease in value:

    • Prerequisites:

      • When the data value is an integer
      • It is atomic value decrease
    • Incrementing number

      INCR key

    • Increase specified by the integer

      INCRBY key increment

    • Decreasing numbers

      DECR key

    • Reducing the number of frames specified

      DECRBY key increment

  • Assignment (Distributed Lock function can be achieved) only when there is no

    setnx key value

Example:

  • Aft additional value: value is added to the end of the key, the key does not exist if the key is set to value. The total length of the string is added after the last return

    APPEND key value

  • Get string length: 0 absence of return

    STRLEN key

  • And set / get a plurality of keys

    • MSET key value key value …
    • MGET key key …
  • Auto-increment primary keys of scenarios

    • Demand: Product Number generated using INCR command
    • Realization: Defining Product Number key: items: id

Hash type

Hash hash type is called, provides a mapping of fields and field values. Field values ​​can only be a string type, it does not support other types of hash types, collections, etc.

  • Assignment:

    • HSET command does not distinguish between insert and update operations, when the insertion command HSET operation returns 1 when executing an update operation 0
    • HSET key field value
  • Assignment once more

    • HMSET key field value [field value …]
  • When the field is not present assignment

    • HSET is similar, except that if the field is present, the command does not perform any operation
    • HSETNX key field value
  • The value

    • HGET key field
  • Fetching more than one value

    • HMSET key field [field …]
  • Gets the value of all the fields

    • HGETALL key

  • Field Delete: delete one or more fields may return value is the number of fields deleted

    • HDEL key field [field …]
  • Increase the numbers

    • HINCRBY key field increment

  • Determine whether the field exists

    • HEXISTS key field
  • Gets only the field name or field value

    • HKEYS key
    • whales key
  • Gets the number of fields

    • HKEN key
  • Get all fields

    • HGETALL key
  • Storage of goods Application of Information

    • Note: Data which objects exist, especially object attribute data CRUD operations frequently occur
    • Commodity information field
      • [Praise commodity id, product name, product description, product inventory, merchandise]
    • the definition of key product information
      • Product id as the key information in Redis in 1001 is as follows: [items.1001]

List type

Redis list type (list) can store an ordered list of strings, common operation is to add elements to the ends of the list, or to obtain a fragment of a list.

List the type of content is to use the doubly linked list (double linked list) to achieve, so adding elements to the ends of the list time complexity is 0/1 to get closer to both ends of the elements faster. Means that even if there is a list of tens of millions of elements, get the head or tail of 10 records is also very fast.

  • Add elements to the ends of the list

    • Add an element to the left of the list
      • LPUSH key value [value …]
    • Add elements to the list on the right
      • RPUSH key value [value …]
  • Check List: Gets a list of segments, the return start, all the elements between the stop (including both ends of the element), the index starts from 0. Indices may be negative, "- 1" represents the last time an element

    • LRANGE key start stop

  • From both ends of the pop-up element list: first list element removed from the list on the left, and then returns the element value removed,

    • LPOP key
    • RPOP key
  • Gets the number of elements in the list

    • LLEN key
  • The value specified number of deleted from the list

    • LREM command deletes the list for the value of the first count the number of elements, returns the number of elements actually removed
    • Syntax: LREM key count value
      • count> 0: start the removal from the list on the left
      • count <0: to the right of the list to start deleting
      • count = 0: remove all elements of the value value

  • Gets the specified index setting element values ​​/

    • Gets the element at the specified index value
      • LINDEX key index
    • Element value of the specified index
      • LSET key index value
  • Insert element to the list: first from left to right pivot query element value in the list, and in accordance with the second parameter is determined to BEFORE AFTER value is inserted in front of or behind the element

    • LINSERT key BEFORE|AFTER pivot value
  • The elements from one list to another list

    • RPOPLPUSH source destination
  • Product Reviews application of the list

    • Requirement 1: User comment on a commodity, a commodity will be different users to comment, while storage product reviews, sorted chronologically
    • Requirement 2: The user queries to review this product at the front page, you need to sort chronologically descending
    • Ideas:
      • Use the list storage product reviews information, key is the id of the goods, value is the product reviews Product code 1001 product reviews key

Set Types

That type of set collection type, wherein the data sequence is not repeated and no

Collection Type List Type
Memory contents Up to 2 32 -1 string Up to 2 32 -1 string
Orderliness no Yes
Uniqueness Yes no

Collection type is added to the common set of operations or deleting elements, determines whether there is an element, etc., since the internal collection type Redis is implemented using the hash table is empty.

Redis also provides a plurality of intersection between the set and the union, difference calculation

  • Adding elements
    • SADD key member [member …]
  • Removing elements
    • SREM key member [member …]
  • Get all elements in the collection
    • SMEMBERS key
  • Determining whether the element in the collection
    • SISMEMBER key member
  • Set Operations Command
    • Difference set operation
      • SDIFF key [key …]
    • Intersection operator
      • SINTER key [key …]
    • Their union
      • SUNION key [key …]
  • Get the number of elements in the set
    • SCARD key
  • Pops an element from the collection: due to unordered collection of all spop command select an element from the collection of random pop-up
    • SPOP key

SortedSet type zset

On the basis of the collection type, ordered set is a collection of each element is associated with a score, which allows us not only to complete the insertion, deletion and determine whether there are elements in the collection, but also to get the highest or lowest first N elements, obtaining scores and other elements related to operation within a specified range of scores

And ordered collection list

  • the same:
  • Ordered
  • You can get a range of elements
  • the difference:
  • List is implemented by a linked list, close to both ends of the acquired data fast, and when the element increases, the speed will slow down access to the intermediate data
  • Ordered collection type is implemented using a hash, so even in the middle portion of the read data it is very fast
  • The list can not simply adjust the position of an element, but can be ordered set (change score)
  • Ordered set consumes more memory than the type of list
  • Add elements: element will have a fractional present, the return value is newly added to the number of elements in the collection

    • ZADD key score member [score member …]
  • Get ranked in a range of elements in the list: from small to large fraction of elements in accordance with the order from start to return to the index of all elements between the stop (including both ends of the element)

    • ZRANGE key start stop [WITHSCORES] need to get points at the end of parameters plus WITHSCORES

    [Pictures of foreign chains dump fails, the source station may have a security chain mechanism, it is recommended to save the picture down directly upload (img-oxNLid1O-1578993448744) (C: \ Users \ sunmingzhi \ AppData \ Roaming \ Typora \ typora-user-images \ 1578648433108.png)]

  • Get scores element

    • ZSCORE key member
  • Removing elements: remove the key ordered set of one or more members, there is no member will be ignored; when there is a single key instead of the ordered set type, an error is returned

    • ZREM key member [member …]
  • Gets element specifies the range of scores

    • ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
  • Increase the score of an element: the return value is a fraction of the changed

    • ZINCRBY key increment member
  • Gets the number of elements in the collection

    • ZCARD key
  • Get the number of elements in the specified range of scores

    • ZCOUNT key min max
  • Delete elements in accordance with the position range

    • ZREMRANGEBYRANK key start stop
  • Get ranking elements

    • From small to large:
      • ZRANK key member
    • Descending
      • ZREVRANK key member
  • Application of merchandise sales charts

    • Demand: The goods are sorted according to the commodity sales
    • Ideas: the definition of merchandise sales charts (sorted set collection), key for the item: sellsort, a score of merchandise sales

Spoken instructions

  • keys

    • keys pattern
  • of the

    • of the key
  • exists: to confirm the existence of a key

    • exists key
  • expire

    EXPIRE key seconds

    Set key survival time (unit: second) key is automatically deleted after the number of seconds

    TLL key

    View key remaining survival time

    PERSIST key

    Clear survival time

    PEXPIRE key milliseconds

    Survival time setting unit: seconds

  • rename

    • rename oldkey newkey
  • type

    • type key
Published 47 original articles · won praise 35 · views 3211

Guess you like

Origin blog.csdn.net/issunmingzhi/article/details/103976688