This is the 18th day of my participation in Gwen Challenge

Introduction of redis

  1. What is a redis
    • REmote DIctionary server
    • C language development of open source high – performance key – value – pair database
  2. features
    • There is no relationship between the data
    • Internal use of single thread mechanism for work
    • Single thread
    • Multiple data type support
      • String string
      • List the list
      • Hash hash
      • The set collection
      • Sorted_set ordered set
    • Persistent support (power-off protection)
  3. Application scenarios
    • Speed up queries for hotspot data
    • Task queue
      • Seconds kill
      • To snap up
      • The ticket queue
    • Timeliness control
      • Such as the verification code
    • Distributed data sharing
      • Such as separation of the session
    • The message queue
    • A distributed lock
  4. Start the redis
# 1. The default
redis-server
#2. Start with port. The purpose is to start multiple Redis servers
redis-server --port 6379
redis-server --port 6380
#3. With a configuration file, you don't need to specify a port, and you can start multiple Redis services
redis-server conf/redis-6379.conf
redis-server conf/redis-6380.cong

Copy the code

Basic operation of Redis

Information to add

  • Function: Set key, value data, repeated Settings will be overwritten
  • The command
set key value
Copy the code
  • example
set name jack
Copy the code

Information query

  • Function: Query value by key, return nil if no key exists
  • The command
get key
Copy the code
  • example
get name
Copy the code

The data type

  • This section describes data store types
  • string
  • hash
  • list
  • set
  • sorted_set
  • Data type practice cases

I. Introduction of data types

  • What is the purpose of each data type design? What scenario does it correspond to?

The specificity of business data

Use as a cache

  • Original business function design
    • Seconds kill
    • A double tenth
    • Line up the ticket
  • Burst high-frequency access data monitored by the operation platform
    • Breaking news, strong attention
  • High-frequency, complex statistics
    • The number of online
    • Poll leaderboard

Optimize or upgrade system functions

  • Upgrade a cluster on a single server
  • Session management
  • Token management

Second, the string

What is the data type?

  • Redis itself is a map in which all data is stored in the form of key:value
  • The data type refers to the type of the data, the type of the value part, and the key is always a string

Basic operation

  • Add/modify data
set key value
Copy the code
  • To get the data
get key
Copy the code
  • Delete the data
#1. Return 1 on success
#2. Return 0 on failure

del key
Copy the code
  • Add/modify multiple data
mset key1 value1 key2 value2 ...
Copy the code
  • Get multiple data
mget key1 key2 ...
Copy the code
  • Get the number of characters (string length)
strlen key
Copy the code
  • Append information to the original message (append if the original message exists, otherwise create a new string)
append key value
Copy the code

Which is set or mset?

  • Both instructions can actually assign, but the difference is performance
  • Set requires a sequence of instructions
    • Send more times longer time
  • Mset can send multiple instructions at once
    • Less sending times
    • Long execution time (thread blocking)

String numeric operation

  • Sets numeric data to increment a specified range of values
incr key Increment value by 1
incyby key increment Alter table value increment increment
incrbyfloat key increment # for decimals, only integers
Copy the code
  • Numeric Numeric data reduces the value of a specified range
decr key
decrby key increment
Copy the code

Pay attention to

  • String internal storage is a string, increment when changed to an integer operation
  • Redis is an atomic operation without regard to concurrency
  • Values have a maximum range, which is the maximum value of a Long in Java
  • Redis increment can be used as the database primary key strategy

Data timeliness Settings

  • For example, wechat votes every four hours
  • Automatically control the existence time of hot news
  • At the end of time this key value will no longer exist
  • Set valid time
setex key seconds value
psetex key milliseconds value
Copy the code

Application scenarios

For example, if there is a microblog and many people open his personal space, it is necessary to put the number of his fans, the number of blogs and personal information into Redis for quick access

Naming conventions for string keys

Two ways to store data in Redis

  • Each attribute information for a user is stored separately with attributes and primary keys
  • Key The primary key, using JSON as the value to store all information about a user

Third, the hash

Why hash?

  1. There are two ways to store all of an object’s data, either using JSON or storing it individually. Neither approach is convenient for Redis to manipulate data
  2. Therefore, use hash to store all object information to facilitate Redis operations on data. However, do not abuse hash to store objects, which reduces redis efficiency
  3. Using json storage

4. Store them separately

  1. Using hash storage

Basic operation

  • Add/modify data
hset key field value
Copy the code
  • To get the data
hget key field
hgetall key
Copy the code
  • Delete the data
hdel key field1 [field2]
Copy the code
  • Add/modify multiple data
hmset key field1 value1 field2 value2
Copy the code
  • Get multiple data
hmget key field1 field 2
Copy the code
  • Gets the number of fields in the hash table
hlen key
Copy the code
  • Gets whether the specified field exists in the hash table
hexists key field
Copy the code
  • Gets the names or values of all fields in a hash table
hkeys key
hvals key
Copy the code
  • Sets the numeric data of the specified field to increase the value of the specified range
hincrby key field increment
hincrbyfloat key field increment
Copy the code
  • If the key has a value, none of the above will happen. If the key does not have a value, a new key-value pair will be created.
hsetnx key field value
Copy the code

Matters needing attention

  • Values of the hash type can only store strings and cannot store other data types. There is no nesting
  • Each hash can store 2^32-1 key-value pairs
  • You can’t abuse hash as a tool for storing objects
  • Don’t always usehgetallFetch all the attributes of the object at once, or it may become a data access bottleneck

Application Scenario 1

  1. E-commerce site shopping cart
    • With the user ID as the key, each user creates a hash storage structure to store the corresponding shopping cart information
    • Store the product number as field and purchase quantity as value
    • Add item: Append new field and value
    • Browse: Traverses the hash
    • Change quantity: Add/subtract, set value
    • Delete item: Delete field
    • Clear: Deletes the key
  2. Time format of the storage
Key: user ID value: product ID :info XXX Product ID :number XXX product ID: PIC XXX <! -- The above method solves the data storage, but like commodity information and commodity pictures, if each item is stored once, the data will be very redundant. So we need to extract these as the common part -->Copy the code

Application Scenario 2: E-commerce stores snap up commodities in activities

  • Use the merchant ID as the key
  • Take the id of the participating commodity as field
  • Take the quantity of commodities participating in the snap up as the corresponding value
  • Use a lower value to control the number of products when buying

Fourth, the list

  • Data storage requirements: Stores multiple data in an orderly manner
  • Save multiple data
  • The bottom layer uses a bidirectional linked list storage structure

Basic operation

  • Add/modify data
Enter from the left side of the list
lpush key value1 value2 ...
Enter from the right side of list
rpush key value1 value2 ...
Copy the code
  • To get the data
# use the index (starting from 0 and ending with -1)
The order is like a stack, last in first out, so rpush in, lrange in order
lrange key start stop
lindex key index
llen key
Copy the code
  • Get and remove data
lpop key
rpop key
Copy the code
  • Obtain and remove within a specified time
blpop key1 [key2] timeout
brpop key1 [key2] timeout

For example, there is no data in list0, but the command will wait 30 seconds, which will execute the command as soon as there is data in list0
blpop list0 30

# is block b
Copy the code
  • Removes a specified number of specified data
# delete the value of count in the key. If there are three AS, delete three as
lrem key count value
Copy the code

The business scenario

  • “Like” in wechat circle of friends requires that the information of “like” friends be displayed in the order of “like”
  • Can be used for data control with sequence of operations
  • If one of the people in the middle gets unliked, delete that person from the middle

Service Scenario 2

  • Log queue, there are multiple nodes have logs, you can write all the logs to the list of Redis, not only order but also atomic, do not worry about concurrency. The logs of all four nodes are written in one place.
  • There is a maximum amount of data
  • Value can only be a string

Fifth, the set

  • Storage requirements: Store large amounts of data to provide greater efficiency in queries
  • Can save a large amount of data, efficient internal storage mechanism, easy to query
  • Set: identical to hash, stores only keys, not nil, and keys are not allowed to be duplicated

Basic operation

  • Add data
sadd key member1 [member2]
Copy the code
  • Get all data
smembers key
Copy the code
  • Delete the data
srem key member1 [member2]
Copy the code
  • Get aggregate data
scard key
Copy the code
  • Determines whether the collection contains the specified data
sismember key member
Copy the code
  • Gets a specified amount of data randomly from the collection
srandmember key [count]
Copy the code
  • Randomly retrieves some data from the collection and moves it out of the collection
spop key
Copy the code

Application scenarios

  • Random recommendation information retrieval, such as song list recommendation, hot news recommendation, big V recommendation, etc
  • Second degree associative search
  • Show common concern
  • Show mutual friends

Set data interchange operation

  • Knowledge preparation

  • Find the intersection, union and difference sets of two sets
sinter key1 [key2]
sunion key1 [key2]
sdiff key1 [key2]
Copy the code
  • Find the intersection, union, and difference sets of two sets and store them in the specified set
sinterstore destination key1 [key2]
sunionstore destination key1 [key2]
sdiffstore destination key1 [key2]

# example: Place the intersection of U1 and U2 in U3
sinterstore u3 u1 u2
Copy the code
  • Moves the specified data from the original collection to the target collection
smove source destination member

Example: Move AAA from U2 to U1
smove u2 u1 aaa
Copy the code

Matters needing attention

  • Data cannot be duplicated
  • A set stores the same results as a hash, but cannot be used as a hash
  • It is best to use Redis to provide basic data rather than validation results. For example, exist is a business-level method to determine whether the value exists

Service scenario: Access volume

  • Pv: Every click of a link
    • The string
  • Uv: Access to a cookie
    • Use the set to heavy
  • IP: Access to an IP address
    • Use the set to heavy

Service scenario: Blacklist and whitelist

  • Use set to store this information and de-duplicate it
  • Blacklist Filters IP addresses, devices, and users

Six, sorted_set

  • Storage requirements: Data sorting facilitates the efficient presentation of data
  • Requirement: Can save sortable fields
  • Adds sortable fields to the storage structure of a set

Basic operation

  • Add data
# score is the sort field (set it arbitrarily, as long as it can be used for sorting)
# member's value
zadd key score1 member1 [score2 member2]

# Example: Will sort by grade
zadd scores 99 zhangsan
zadd scores 80 lisi
zadd scores 95 wangwu
Copy the code
  • Get all data
Select * from list; select * from list
# WITHSCORES indicates that the sorted fields are also displayed, with or without scores
The retrieved data is in orderZrevrange key start stop [WITHSCORES] @ reverse zrevrange key start stop [WITHSCORES]Example #
zrange scores 0 -1
zrange scores 0 -1 WITHSCORES
Copy the code
  • Delete the data
zrem key member [member ...]
Copy the code
  • Get data by conditions
# min, Max for indexing
# limit as in mysql
zrangebyscore key min max [WITHSCORES] [LIMIT]
zrevrangebyscore key max min [WITHSCORES]
Copy the code
  • Conditional deletion of data
Delete by index
zremrangebyrank key start stop
Delete by sort field
zremrangebyscore key min max
Copy the code
  • Get the amount of collection data
# how many keys
zcard key

# How many keys are in the scope
zcount key min max
Copy the code
  • Set intersection and union operation
# intersection
# destination: A collection of results
# numkeys: There are several collections to manipulate
# key: a collection used for operations
zinterstore destination numkeys key [key ...]

# and set
zunionstore destination numkeys key [key ...]

Example: ss is used to store, 3 represents 3 sets, s1/s2/s3 represents the set to operate on
zinterstore ss 3 s1 s2 s3
Copy the code
  • Get the index of the data (ranking)
The index starts at 0
zrank key member
# reverse
zrevrank key member

Example #Zadd movies 143 AA 97 bb 201 cc zrank movies bbCopy the code
  • Score value is obtained by modification
# is the value of score
zscore key member

# change the score value
zincrby key increment member
Copy the code

Business scenario: Ranking

  • Top10 websites of all kinds
  • Game intimacy
  • Broadcast room ranking

Matters needing attention

  • Score stores 64 bits of data space, with maximum and minimum values
  • Score can also store data as doubles, so there can be precision issues with decimals
  • It is a set structure, and the data cannot be modified, but note that the score will be modified

The business scenario

  • Experience THE VIP duration control
  • Used to manage scheduled task execution sequence or task expiration

Service scenario: Weight

  • For tasks with weights, tasks with high weights are processed finitely
  • Score is the weight and value is the task
  • Multiple sets of data with different meanings can be combined into a score. Note that all score numbers must be the same, or they cannot be compared

Practice cases of data types

Case a

Ai semantic analysis can only be used 10 times per hour.

Solutions to

  • Design a counter with the user ID as the key and the number of times used as the value
  • Determine whether the limit is exceeded before the number of calls
    • Incrementing each call up to a maximum of times
  • Set the life cycle for the counter
  • The command

improved

  • So this is going to be 10 every time, but you can change it
  • Instead of incrementing from 0, the counter takes the maximum value of set minus 10, and when it reaches the maximum value, an exception is thrown
  • But there is a problem: get is not empty, but the next moment is empty. And then if I add 1, I’m infinite?
  • Maximum value: 9223372036854775807