Driver-level data caching method, system and equipment based on redis client and storage medium
By introducing a caching mechanism and a scheduled update mechanism into the redis client driver, the problems of slow access speed and high load pressure of the traditional redis client driver are solved, and efficient key-value access and load reduction are achieved.
Patent Information
- Application Number
- CN202410420092.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-04-09
- Publication Date
- 2024-09-20
AI Technical Summary
The traditional redis client driver is slow to access data through the network, resulting in limited network bandwidth and increasing redis load pressure.
Introduce a caching mechanism into the redis client driver to store frequently accessed key-values, and keep the data up-to-date through scheduled updates and write update mechanisms to avoid direct access to redis through the network and optimize read and write command processing.
It significantly improves the access speed of key-value, reduces the load pressure of redis, and provides flexible cache time setting and version information aware update mechanism.
Smart Images

Figure CN118673050A
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of data caching, and in particular to a method, system, device and storage medium for data caching based on a Redis client driver level. Background Art
[0002] Traditional redis client drivers create a TCP link with the redis server and send commands that comply with the RESP protocol (Redis Serialization Protocol, which is a data serialization protocol for communication between redis clients and servers) to read and write redis. Since the server network bandwidth (the amount of data that can pass through the link per unit time, used to identify data transmission capacity) is limited, the speed at which clients request redis access through the network is much slower than accessing data directly from memory. Summary of the invention
[0003] The technical problem to be solved by the present invention is to provide a data caching method, system, device and storage medium based on redis client driver level, which avoids these requests from directly accessing redis through the network by storing frequently accessed key-values in the driver, thereby greatly improving the access speed of these key-values and reducing the redis load pressure.
[0004] In order to solve the above technical problems, the present invention provides a data caching method based on redis client driver level, comprising the following steps:
[0005] Step 1: Initialize the basic cache configuration;
[0006] Step 2: The business calls the redis client cache driver to send commands to redis and return results.
[0007] Preferably, in step 1, the basic cache configuration includes the total limit size of the cache, the maximum number of keys that can be cached, the cache elimination strategy, and the cache update cycle.
[0008] Preferably, in step 2, when sending a command, if it is a read command, first try to query the result in the cache; if there is a query result in the cache, return directly; if there is no query result in the cache, send a command to redis to obtain the query result, and then add the result to the cache and return.
[0009] Preferably, in step 2, when sending a command, if it is a write command, redis is accessed first, and after obtaining the result, the cache will try to update the key-value according to the version information of the key and return the result.
[0010] Preferably, the cache periodically updates the key-value as follows: after the redis client driver is started, the cache will start an independent thread to perform a periodic update task, which will periodically traverse all keys in the cache and send the above-mentioned specific command to redis to query the key version information; if the key type changes, the cache will immediately clear the current key-value cache; if the key type does not change, but the key update timestamp changes, the cache will immediately update the value of the key-value.
[0011] Preferably, the user sets the cache time for each key-value.
[0012] Preferably, the cache promptly senses the change in the version information of the key-value in the redis client, so that the cache updates the value of the key-value.
[0013] Correspondingly, a data cache system based on redis client driver level includes: an initialization module and a business call module; the initialization module initializes the basic cache configuration, and the business call module calls the redis client cache driver to send commands to redis and return results.
[0014] Accordingly, a redis client driver-level data cache device is characterized by comprising: one or more processors;
[0015] A storage device for storing one or more programs and user data;
[0016] When the one or more programs are executed by one or more processors, the one or more processors implement the redis client driver-level data caching method as described in any one of claims 1 to 8.
[0017] Correspondingly, a redis client driver level data caching storage medium is characterized in that a computer program is stored thereon, and when the program is executed by a processor, a redis client driver level data caching method based on any one of claims 1 to 8 is implemented.
[0018] The beneficial effects of the present invention are as follows: (1) The present invention adds a cache in the redis client driver and stores the frequently accessed key-value in the driver, thereby avoiding these requests from frequently accessing redis directly through the network, greatly improving the access speed of these key-values and reducing the redis load pressure; (2) The present invention provides an extended command that can set a specified key cache time, allowing users to set the cache time for each key-value, facilitating the refined management and control of the cached key-value; (3) The present invention provides a method for periodically updating the cached key-value based on a specific command, which allows the cache to promptly perceive the version information changes of the key-value in redis, thereby allowing the cache to update the key-value. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] Figure 1 This is a schematic diagram of parsing the return value of the query key version information command of the present invention.
[0020] Figure 2 It is a schematic diagram of the method flow of the present invention.
[0021] Figure 3 The present invention is a redis client driver data cache read command execution flow chart.
[0022] Figure 4 The present invention is a redis client driver data cache write command execution flow chart.
[0023] Figure 5 This is a flow chart of the redis client-driven data cache key timing update of the present invention. DETAILED DESCRIPTION
[0024] like Figure 2 As shown, a data caching method based on redis client driver level includes the following steps:
[0025] S101, redis client driver initialization (including initializing driver cache, starting timer thread to execute cache timer task, etc.).
[0026] S102, parsing the command sent by the redis client driver called by the business logic (including parsing command type, command parameters, etc.).
[0027] S103, the read command first queries the result in the cache, and the write command is directly sent to redis to obtain the result.
[0028] S104, the redis client drives the cache storage or updates the command and returns the result.
[0029] The cache will decide whether to store or update the current key-value according to different command types. When updating, it uses pipeline to send commands and then returns the results to the business logic caller.
[0030] like Figure 3 The figure shows the execution flow chart of the cache read command of the redis client driver. Specifically, taking the command [getkey1] as an example, this command is of read type, first querying whether key1 has been cached, if it has been cached, directly returning the cache result; if not, first querying the value of key1 in redis, then updating the key1-value in the cache, and returning the result.
[0031] like Figure 4 The following is a flowchart of the Redis client driver cache write command execution. Specifically, take the command [setkey1value1] as an example. This command is a write type. First, query Redis. After obtaining the result, first determine whether the key1 version information has changed. If it has changed, directly clear the key1-value from the cache; if it has not changed, directly return the result.
[0032] Command definition: version key[k1,k2,k3...]
[0033] Return value analysis: Figure 1 As shown, the command return value consists of a 64-bit decimal number, the first 2 bits are the key type, and the last 62 bits are the key update timestamp (accurate to ms).
[0034] Command function: Based on the key type and whether the key update timestamp has changed, you can compare whether the key in the current cache has been updated.
[0035] After the redis client driver is started, the cache will start an independent thread to perform a scheduled update task. This task will periodically traverse all keys in the cache and send the above specific command to redis to query the key version information. If the key type changes, the cache will immediately clear the current key-value cache; if the key type does not change, but the key update timestamp changes, the cache will immediately update the key-value value.
[0036] like Figure 5 The figure shows the redis client driver cache command timed update flow chart. Specifically: after the redis client cache driver is started, a thread will be started to regularly update all key-values in the cache.
[0037] S301, redis client driver initialization.
[0038] S302, traverse and update the key-value in the cache.
[0039] After getting the key from the cache, first determine whether the key is expired. If the key is expired, directly clear the cache of the key. If it is not expired, use Pipeline (pipeline, the client can send multiple commands to the server at one time, and the server will return the results at one time through a response after processing them in sequence) to send the above specific command to redis to query whether the version information of the current key has changed. If the key version information has changed, it means that the value of the key has been updated in redis, then the key-value is directly cleared from the cache; if the version has not changed, it means that the value of the key has not been updated in redis, and the cache does not need to be changed.
[0040] Repeat the above process to update all key-value pairs in the cache.
[0041] Correspondingly, a data cache system based on redis client driver level includes: an initialization module and a business call module; the initialization module initializes the basic cache configuration, and the business call module calls the redis client cache driver to send commands to redis and return results.
[0042] Accordingly, a redis client driver-level data cache device is characterized by comprising: one or more processors;
[0043] A storage device for storing one or more programs and user data;
[0044] When the one or more programs are executed by one or more processors, the one or more processors implement the redis client driver-level data caching method as described in any one of claims 1 to 8.
[0045] Correspondingly, a redis client driver level data caching storage medium is characterized in that a computer program is stored thereon, and when the program is executed by a processor, a redis client driver level data caching method based on any one of claims 1 to 8 is implemented.
[0046] The present invention caches the key-value with high frequency access in the redis client driver, and keeps the data in the cache in the latest state by means of timed update and write update. When requesting to query a key, first try to query the key-value in the client driver cache, and directly return after obtaining the query result from the cache. When the query result cannot be obtained in the cache, send the command query result to redis. In this way, it is possible to avoid these key-values stored in the cache from querying the redis server through the network to obtain the result, greatly reducing the network communication cost, reducing network access delay, greatly improving the access speed, and reducing the redis load pressure.
Claims
1. A data caching method based on redis client driver level, characterized in that: The steps include: Step 1: Initialize the basic cache configuration; Step 2: The business calls the redis client cache driver to send commands to redis and return results.
2. The redis client driver-level data caching method according to claim 1, characterized in that: In step 1, the basic cache configuration includes the total cache size limit, the maximum number of keys that can be cached, the cache elimination strategy, and the cache update cycle.
3. The redis client driver-level data caching method according to claim 1, characterized in that: In step 2, when sending a command, if it is a read command, first try to query the result in the cache; if there is a query result in the cache, return it directly; if there is no query result in the cache, send a command to redis to obtain the query result, and then add the result to the cache and return it.
4. The redis client driver-level data caching method according to claim 1, characterized in that: In step 2, when sending a command, if it is a write command, redis is accessed first. After obtaining the result, the cache will try to update the key-value according to the key version information and return the result.
5. The redis client driver-level data caching method according to claim 4, characterized in that: The specific details of the cache timed key-value update are as follows: after the redis client driver is started, the cache will start an independent thread to perform the timed update task. This task will periodically traverse all keys in the cache and send the above-mentioned specific command to redis to query the key version information; if the key type changes, the cache will immediately clear the current key-value cache; if the key type does not change, but the key update timestamp changes, the cache will immediately update the key-value value.
6. The redis client driver-level data caching method according to claim 5, characterized in that: The user sets the cache time for each key-value.
7. The redis client driver-level data caching method according to claim 5, characterized in that: The cache promptly senses changes in the key-value version information in the redis client, allowing the cache to update the key-value value.
8. A cache system based on the redis client driver-level data cache method as claimed in claim 1, characterized in that: include: Initialization module and business call module; The initialization module initializes the basic cache configuration, and the business call module calls the redis client cache driver to send commands to redis and return results.
9. A data cache device based on redis client driver level, characterized in that: include: one or more processors; A storage device for storing one or more programs and user data; When the one or more programs are executed by one or more processors, the one or more processors implement the redis client driver-level data caching method as described in any one of claims 1 to 8.
10. A redis client driver-level data cache storage medium, characterized in that: A computer program is stored thereon, and when the program is executed by a processor, a data caching method based on a redis client driver level as described in any one of claims 1 to 8 is implemented.
Citation Information
Cited By
Client local cache and Redis cache synchronization system based on Redis Module
CN121365107A
