Redis basic introduction and API details

Common sense introduction to disk and memory comparison disk: 1. Addressing: millisecond level 2. Bandwidth: 10W times slower than memory at addressing. 1. Addressing: nanosecond level 2

I/O buffer: Cost issue

Disk with track, sector, a sector of 512bytes brings a cost increase: index 4K OS, no matter how much you read, is the minimum 4K taken from disk

MemCache vs. Redis

Redis: K – v structure, concept of value have a type (String, a List, Hash, Set, Sorted Set), can be persistent, very flexible and can operate on a certain value of a single element

Redis API

String

string

  1. GET key: Obtains the key value
  2. SET key value: Sets the key value value
  3. APPEND key Value: Appends the value to the end of the value with the key
  4. SETRANGE key offset value: overwrites and appends value from the offset position
  5. GETRANGE key start end: returns the subcharacter of the string value in the key
  6. STRLEN key: Returns the length of the string value stored in the key.
  7. SETNX key value: Value can be set only if the key does not exist. Usage scenario: Used for distributed locks

Numeric types

INCR and DECR use scene buying, seconds killing, detail pages, likes and comments to avoid concurrent transactions to the database completely replaced by redis memory operations

  1. INCR key: Increases the value of the key by 1
  2. INCRBY key increment: Increments the value of a key
  3. DECR key: Decreases the value of the key by 1
  4. DECRBY Key Decrement: Decreases decrement for key values

BitMap

  1. SETBIT key offset value: Sets or clears the bits at the specified offset of the string value stored in the key.
  2. GETBIT key offset: Obtains the bit of the offset offset of the key
  3. BITCOUNT key [start end]: the number of 1 bits used to obtain the key string value. In general, we take a year and the user’s ID or name as the key (requires uniqueness), and each bit as a day. When the day is logged in, we change it to 1. General operation procedure: 1. Setbit Sean 1 1,setbit Sean 7 1,setbit Sean 364 1 2.STRLEN Sean 3
  4. BITTOP operation destKey Key [key…] BITOP AND destkey k1 k2 K3… KN, calculates the logical sum of one or more keys and saves the result to the destkey. BITOP OR destkey k1, k2, k3… KN, calculates logical or for one or more keys and saves the result to destkey. BITOP XOR destkey k1 k2 k3 BITOP XOR destkey k1 k2 k3 KN: obtains logical XOR for one or more keys and saves the result to the Destkey. BITOP NOT destkey K Specifies the logical no of the given key and saves the result to the destkey. Usage scenario: Active user statistics! The random window calculates how many users are active in a certain period of time. The general idea is that the key is the time, and the bit represents the user whose ID is the number. setbit 20190101 1 1 setbit 20190102 1 1 setbit 20190102 7 1 bitop or destkey 20190101 20190102 BITCOUNT destkey 0 -1

List

A list is a simple list of strings, sorted by insertion order. You can add an element to the left or right of the list. Column tables can store up to 232-1 elements (4294967295, more than 4 billion per list).

  1. BLPOP key1 [key2 ] timeout

Removes and retrieves the first element of the list. If there are no elements in the list, the list is blocked until the wait times out or an eject element is found.

  1. BRPOP key1 [key2 ] timeout

Removes and retrieves the last element of the list. If there are no elements in the list, the list is blocked until the wait times out or an eject element is found.

  1. BRPOPLPUSH source destination timeout

Pops a value from a list, inserts the pop-up element into another list and returns it; If the list has no elements, it blocks until the wait times out or a popup element is found.

  1. LINDEX key index

Gets the elements in the list by index

  1. LINSERT key BEFORE|AFTER pivot value

Inserts an element before or after the element in the list

  1. LLEN key

Get the list length

  1. LPOP key

Removes and gets the first element of the list

  1. LPUSH key value1 [value2]

Inserts one or more values into the list header

  1. LPUSHX key value

Inserts a value into the head of an existing list

  1. LRANGE key start stop

Gets the elements in the specified range of the list

  1. LREM key count value

Remove list elements

  1. LSET key index value

Set the value of a list element by index

  1. LTRIM key start stop

To trim a list, that is, to keep only elements within a specified range, and to remove all elements that are not within a specified range.

  1. RPOP key

Removes and gets the last element of the list

  1. RPOPLPUSH source destination

Removes the last element of the list and adds it to another list and returns

  1. RPUSH key value1 [value2]

Adds one or more values to the list

  1. RPUSHX key value

Add a value to an existing list

Hash

  1. HDEL key field1 [field2]

Deletes one or more hash table fields

  1. HEXISTS key field

Check whether the specified field in the hash table key exists.

  1. HGET key field

Gets the value of the specified field stored in the hash table.

  1. HGETALL key

Gets all the fields and values of the specified key in the hash table

  1. HINCRBY key field increment

Add increment to the integer value of the specified field in the hash table key.

  1. HINCRBYFLOAT key field increment

Increments the floating point value of the specified field in the hash table key.

  1. HKEYS key

Gets all fields in the hash table

  1. HLEN key

Gets the number of fields in the hash table

  1. HMGET key field1 [field2]

Gets the values of all given fields

  1. HMSET key field1 value1 [field2 value2 ]

Set multiple field-value pairs into the hash key at the same time.

  1. HSET key field value

Set the value of field in hash table key to value.

  1. HSETNX key field value

Set the value of the hash table field only if the field field does not exist.

  1. HVALS key

Gets all values in the hash table

  1. HSCAN key cursor [MATCH pattern] [COUNT count]

Iterates key-value pairs in a hash table.

Set

  1. SADD key member1 [member2]

Adds one or more members to a collection

  1. SCARD key

Gets the number of members of the collection

  1. SDIFF key1 [key2]

Returns the difference set of all sets given

  1. SDIFFSTORE destination key1 [key2]

Returns the difference set for a given collection and stores it in destination

  1. SINTER key1 [key2]

Returns the intersection of all sets given

  1. SINTERSTORE destination key1 [key2]

Returns the intersection of all the given collections and stores them in destination

  1. SISMEMBER key member

Check whether the member element is a member of the collection key

  1. SMEMBERS key

Returns all members of the collection

  1. SMOVE source destination member

Move the member element from the source collection to the Destination collection

  1. SPOP key 
    Copy the code

Removes and returns a random element from the collection

  1. SRANDMEMBER key [count] 
    Copy the code

Returns one or more random numbers in a collection

  1. SREM key member1 [member2] 
    Copy the code

Removes one or more members of a collection

  1. SUNION key1 [key2] 
    Copy the code

Returns the union of all given sets

  1. SUNIONSTORE destination key1 [key2]

The union of all given collections is stored in the Destination collection

  1. SSCAN key cursor [MATCH pattern] [COUNT count] 
    Copy the code

Iterate over the elements in the collection

ZSet

Zset, like set, is a collection of string elements and does not allow duplicate members. The difference is that each element is associated with a double score. Redis uses scores to sort the members of a collection from smallest to largest. Members of a Zset are unique, but scores can be repeated. Collections are implemented by hashing tables, so adding, deleting, and searching are O(1) complexity. The maximum number of members in a collection is 232-1 (4294967295, each collection can store more than 4 billion members). SkipList jumps from On–>Ologn

  1. ZADD key score1 member1 [score2 member2]

Adds one or more members to an ordered collection, or updates the scores of existing members

  1. ZCARD key

Gets the number of members of an ordered collection

  1. ZCOUNT key min max

Computes the number of members in an ordered set with a specified interval fraction

  1. ZINCRBY key increment member

Increment the score of a specified member in an ordered set

  1. ZINTERSTORE destination numkeys key [key …]

Computes the intersection of one or more ordered sets given and stores the result set in a new ordered set key

  1. ZLEXCOUNT key min max

Computes the number of members in the specified dictionary range in an ordered collection

  1. ZRANGE key start stop [WITHSCORES]

Returns an ordered collection of the members of a specified interval through an indexed interval

  1. ZRANGEBYLEX key min max [LIMIT offset count]

Returns a member of an ordered collection through a dictionary interval

  1. ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT]

Returns an ordered set of members within a specified interval by a fraction

  1. ZRANK key member

Returns the index of the specified member in the ordered collection

  1. ZREM key member [member …]

Removes one or more members of an ordered collection

  1. ZREMRANGEBYLEX key min max

Removes all members of the given dictionary range from the ordered collection

  1. ZREMRANGEBYRANK key start stop

Removes all members of the given rank range from the ordered collection

  1. ZREMRANGEBYSCORE key min max

Removes all members of the given fractional interval from the ordered set

  1. ZREVRANGE key start stop [WITHSCORES]

Returns the members of an ordered set in a specified interval, indexed from high to bottom

  1. ZREVRANGEBYSCORE key max min [WITHSCORES]

Returns the members of the ordered set within the specified range of scores, sorted from highest to lowest

  1. ZREVRANK key member

Returns the ranking of the specified members of an ordered set, ordered in decreasing order (from largest to smallest) by score value

  1. ZSCORE key member

Returns the score value of a member in an ordered set

  1. ZUNIONSTORE destination numkeys key [key …]

Computes the union of a given one or more ordered sets and stores it in a new key

  1. ZSCAN key cursor [MATCH pattern] [COUNT count]

Iterating over elements in an ordered set (including element members and element scores)