I. Description of features

String is the most basic type of Redis. Each key corresponds to a value.

(2) String is binary safe. The redis String can contain any data. Like JPG images or serialized objects;

(3) String is the most basic data type of Redis. A key can store up to 512MB.

Binary security means that if the information security of binary data is ensured during data transmission, that is, it is not tampered with or deciphered, and if it is attacked, it can be detected in time. Binary security features: encoding and decoding occur in the client, high execution efficiency; Do not need frequent encoding and decoding, there will be no garbled code;Copy the code

2. Common commands

(a) the SET

Command format: SET key value

Function: Store string key value

The demo:

(2) the GET

The command format is get key

Function: Get a string key

The demo:

(3) the MSET

MSET key value [key value]

Function: Batch store string key value

The demo:

(4) MGET

Syntax: mget key value [key value]

Function: batch obtain string key value

The demo:

(5) SETNX

The command format is setnx key value

Function: Store a string key that does not exist

Demo note: Save a key named test444. If the first key succeeds, the second key fails

(6) DEL

The command format is setnx key value

Function: Store a string key that does not exist

The demo:

(7) the EXPIRE

The command format is setnx key value

Function: Store a string key that does not exist

The demo:

(eight) INCR

Command format: incr key

Run the following command to increase the value of a key by one. If it does not exist for the first time, a key with a value of 0 will be created

The demo:

(9) DECR

Command format: decr key

Run the following command to reduce the value of a key by one. If it does not exist for the first time, a key with a value of 0 will be created

The demo:

INCRBY (10)

The command format is incrby key increment

Run the following command to increment a key by increment if the key does not exist for the first time

The demo:

(11) DECRBY

The command format is incrby key increment

Run the following command to increment a key by incrementing the stored value in the key

The demo:

Three, application scenarios

Summarize the common production application scenarios, mainly divided into the following types

(1) General data cache

Note: The following configuration data stored in mysql can be loaded into Redis

1, requirements,

Saas service each merchant can dynamically fill in their own payment configuration in the background, which needs to be cached after each query

2, design

  • To obtain the configuration, check the cache first. If there is no data in the cache, query mysql. If yes, the configuration data is directly returned
  • Query mysql to get the configuration data, cache the data redis, and then return to the configuration
  • Paysetting: Payment :{service provider ID}

3, development,

// The main logic is: Public PaymentDto getPaymentSetting() throws Exception{ObjectMapper ObjectMapper = new ObjectMapper(); // Query cache String key = "paysetting:payment:1"; / / query cache PaymentDto PaymentDto = objectMapper. ReadValue ((String) redisTemplate. OpsForValue () get (key), PaymentDto. Class); If (objectutils.isEmpty (paymentDto)){// Query database paymentDto data = getData(); / / cache redisTemplate. OpsForValue (). The set (key, objectMapper writeValueAsString (data), 3000, TimeUnit. SECONDS); return data; } // return data return paymentDto; } private PaymentDto getData(){PaymentDto PaymentDto = new PaymentDto(); paymentDto.setAppId("98765365830261325"); paymentDto.setAppSecret("fodkcjeuhaytwgvbdmi873tg4doo"); return paymentDto; }Copy the code

(2) Object instance cache

Note: Common object instances required by business can also be cached. The Spring framework starts caching singleton non-lazy-loaded objects into the JVM ahead of time

(3) counter

Note: In some business scenarios with high real-time requirements and frequent writing, the performance of mysql and other databases is squeezed. At this time, Redis can be used to alleviate the problem. Such as the number of articles like, comments, or interface flow limit

1, requirements,

Some open interfaces require customers to limit the number of times they can access each day based on their different plans

2, design

  • Get the package configuration to get the maximum number of times a customer can access the interface per day
  • Obtain the number of times that a customer has accessed the interface from the cache. Compare the number with the maximum number. If the number is equal to, a message is displayed indicating that the access limit has been reached. Otherwise, the number of times recorded on the interface increases by one
  • The cache key is designed as {interface id} :{year-month-day}:{service provider ID}

3, development,

Public void limitInterface() throws Exception{// Obtain the package configuration. Integer limitNum = getLimitNum(); String key = "findData:2021-11-26:100"; / / query cache cache key design for {interface identifier} : {year - month - day}, {service id} String count = (String) redisTemplate. OpsForValue () get (key); Integer countNum = Integer.valueOf(count); If (countNum < limitNum) {/ / interface record number plus one redisTemplate opsForValue (). The increment (key); }else{system.out.println (" today access limit ");}else{system.out.println (" today access limit "); }} public Integer getLimitNum(){return 100; }Copy the code

(4) distributed lock

Redis setnx command can be used to achieve only a client successfully set a key value, so as to achieve a distributed lock.

(5) Session sharing

Note: If multiple machines want to realize session sharing, they need to find a common place to store data, or to realize data synchronization. Obviously, Redis can be a common place to store data.

(6) Distributed ID generation.

Note: In order to ensure the uniqueness of ID, you can let Redis unified generation of a batch of ID, and then take out to use