Method for realizing expiration and invalidation and automatic refreshing of cache

文档序号:1963725 发布日期:2021-12-14 浏览:17次 中文

阅读说明:本技术 一种实现缓存到期失效、自动刷新的方法 (Method for realizing expiration and invalidation and automatic refreshing of cache ) 是由 李智超 于 2021-07-27 设计创作,主要内容包括:本发明涉及计算机技术领域,具体为一种实现缓存到期失效、自动刷新的方法,包括以下步骤:步骤一,缓存通用配置,应用程序将数据存储的需求写入数据库;步骤二,注解配置,标记自动刷新时间或数据过期时间;步骤三,缓存处理器拦截,系统运行时缓存处理器拦截设置缓存过期时间并标记是否需要自动刷新;步骤四,定时任务刷新缓存,定时任务时间一到,拉取携带刷新参数的缓存,本发明本发明提供了一套通过缓存注解配置、处理器拦截,定时任务刷新,实现方法结果缓存过期和自动更新的技术方案,支持设置过期时间,可以更灵活地根据业务运用缓存;可自动刷新缓存,避免缓存的时效性不足。(The invention relates to the technical field of computers, in particular to a method for realizing expiration and invalidation and automatic refreshing of a cache, which comprises the following steps: caching general configuration, wherein an application program writes the requirement of data storage into a database; annotating configuration, marking automatic refreshing time or data expiration time; step three, the cache processor intercepts, and when the system runs, the cache processor intercepts and sets the cache expiration time and marks whether automatic refreshing is needed; the invention provides a technical scheme for realizing the overdue and automatic update of the result cache of the method by buffer annotation configuration, processor interception, timed task refreshing and the like, supports the setting of the overdue time and can more flexibly apply the cache according to the service; the cache can be automatically refreshed, and the insufficient timeliness of the cache is avoided.)

1. A method for realizing expiration and invalidation and automatic refreshing of a cache is characterized by comprising the following steps;

caching general configuration, wherein an application program writes the requirement of data storage into a database;

annotating configuration, marking automatic refreshing time or data expiration time;

step three, the cache processor intercepts, and when the system runs, the cache processor intercepts and sets the cache expiration time and marks whether automatic refreshing is needed;

and step four, refreshing the cache by the timing task, and pulling the cache carrying the refreshing parameters when the timing task is up.

2. The method according to claim 1, wherein in the first step, the general configuration of the cache includes whether to start the auto-refresh, the cycle time of the auto-refresh task, and a task thread pool, and the task thread pool is used for configuring the number of threads and the size of the queue.

3. The method for realizing cache expiration and automatic refreshing as claimed in claim 1, wherein the method comprises a user, a server and a database, the user is used for sending a request instruction to the server, and the server is used for sending a data caching instruction or a data expiration instruction to the database.

4. The method for implementing cache expiration and automatic refresh as claimed in claim 1 or 3, wherein the database comprises a cache database and a Redis database, the cache database is provided with a cache queue, and the Redis database is provided with a ttl cache.

5. The method according to claim 3 or 4, wherein a data distribution program is installed in the server, and when the operations of writing, changing or deleting data in the redis database are found, the operated data are written into the cache database, and the cache data are sorted by the cache queue.

6. A method for implementing cache expiration and automatic refresh as claimed in any one of claims 3 to 5, wherein a data monitoring program is provided in the data distribution program, and if it is monitored that reading of a certain data becomes very frequent, the data is placed in front of a data queue in the cache database, and the more frequent reading of the data by the cache data queue is scheduled to the data queue at the front.

7. The method for realizing expiration and automatic refresh of the cache according to claim 2, comprising a mobile terminal, wherein the mobile terminal is provided with a user login system, a data transmission system, a data reception system and an internet system, the user logs in the mobile terminal through the user login system, the mobile terminal sends a request instruction to the server through the internet system, the mobile terminal sends the cache data to the server through the data transmission system and the data reception system, the server is used for sending a data cache instruction or a data expiration instruction to the database, and the expired data information is fed back to the mobile terminal through the server.

Technical Field

The invention relates to the technical field of computers, in particular to a method for realizing expiration and invalidation and automatic refreshing of a cache.

Background

Spring cache annotation does not support setting of expiration time and regular refreshing of cache, and in service, the expiration time of a designated part of cache is sometimes required to be set, so that the influence on service or space occupation caused by the existence of cache after cache is not required is avoided; the manual updating of the cache is inconvenient, the latest request result cannot be synchronized in time, and the technology is designed as the extension of Spring cache annotation.

The defects in the prior art are as follows:

(1) the new cached annotations only support the redis database.

(2) The automatic cache refreshing time can only be set uniformly and cannot be set independently.

(3) If the cache expiration time is set and the cache expires just before the refresh time, there is no cache for that time difference.

(4) Before the cache is refreshed, the original cache is cleared, then the annotated method is called to obtain a result and the cache is re-cached, and if an external request exists in the time difference, the condition of no cache occurs.

Disclosure of Invention

Technical problem to be solved

Aiming at the defects of the prior art, the invention provides a method for realizing expiration invalidation and automatic refreshing of a cache, which is convenient for setting the expiration time of the cache and refreshing the cache regularly.

(II) technical scheme

In order to achieve the purpose, the invention provides the following technical scheme: a method for realizing expiration and invalidation and automatic refreshing of a cache is characterized by comprising the following steps;

caching general configuration, wherein an application program writes the requirement of data storage into a database;

annotating configuration, marking automatic refreshing time or data expiration time;

step three, the cache processor intercepts, and when the system runs, the cache processor intercepts and sets the cache expiration time and marks whether automatic refreshing is needed;

and step four, refreshing the cache by the timing task, and pulling the cache carrying the refreshing parameters when the timing task is up.

In order to adjust the automatic refresh time conveniently, the present invention has an improvement that in the first step, the general cache configuration includes whether to start the automatic refresh, the cycle time of the automatic refresh task, and a task thread pool, and the task thread pool is used for configuring the number of threads and the size of the queue.

In order to improve the using effect of the invention, the invention is improved by comprising a user, a server and a database, wherein the user is used for sending a request instruction to the server, and the server is used for sending a data caching instruction or a data expiration instruction to the database.

In order to improve the data caching effect, the improvement of the invention is that the database comprises a cache database and a Redis database, a cache queue is arranged in the cache database, and a ttl cache is arranged in the Redis database.

In order to shorten a line for updating the cache, the invention has the improvement that a data distribution program is arranged in the server, when the operation of writing, changing or deleting data in the redis database is found, the operated data is written into the cache database, and the cache data is sequenced by a cache queue.

In order to improve the efficiency of data caching, the invention improves that a data monitoring program is arranged in the data distribution program, if the reading of certain data is monitored to become very frequent, the data is placed in front of a data queue in a cache database, and the more frequent data reading of the cache data queue is dispatched to the data queue which is positioned at the front.

In order to realize remote data caching, the invention has the improvement that the invention comprises a mobile terminal, wherein the mobile terminal is provided with a user login system, a data transmission system, a data receiving system and an internet system, the user logs in the mobile terminal through the user login system, the mobile terminal sends a request instruction to a server through the internet system, the mobile terminal sends caching data to the server through the data transmission system and the data receiving system, the server is used for sending a data caching instruction or a data expiration instruction to a database, and expired data information is fed back to the mobile terminal through the server.

(III) advantageous effects

Compared with the prior art, the invention provides a method for realizing expiration and invalidation and automatic refreshing of cache, which has the following beneficial effects:

the invention provides a technical scheme for realizing the overdue and automatic update of the result cache of the method by cache annotation configuration, processor interception and timed task refreshing, supports the setting of the overdue time and can more flexibly apply the cache according to the service; the cache can be automatically refreshed, and the insufficient timeliness of the cache is avoided.

Drawings

FIG. 1 is a schematic structural view of the present invention;

fig. 2 is a connection diagram of timing tasks.

Detailed Description

The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.

Referring to fig. 1-2, the present invention is a method for implementing expiration invalidation and automatic refresh of cache, including the following steps;

caching general configuration, wherein an application program writes the requirement of data storage into a database;

annotating configuration, marking automatic refreshing time or data expiration time;

step three, the cache processor intercepts, and when the system runs, the cache processor intercepts and sets the cache expiration time and marks whether automatic refreshing is needed;

and step four, refreshing the cache by the timing task, and pulling the cache carrying the refreshing parameters when the timing task is up.

In this embodiment, in the first step, the general cache configuration includes whether to start automatic refresh, a cycle time of an automatic refresh task, and a task thread pool, where the task thread pool is used to configure the number of threads and the size of a queue, and the task thread pool is used to classify and configure different data tasks, sort cached data, and set automatic refresh time separately for the data tasks of different classifications or the sorted data tasks.

In the embodiment, the system comprises a user, a server and a database, wherein the user is used for sending a request instruction to the server, the server is used for sending a data caching instruction or a data expiration instruction to the database, and the caching or automatic refreshing of the data is classified and the data is monitored by setting a plurality of operation units, so that the data storage or caching is safer.

In this embodiment, the database includes a cache database and a redis database, a cache queue is arranged in the cache database, a ttl cache is arranged in the redis database, and after the data is stored in the redis database, the server detects whether cache data exists in the cache database, so that repeated data storage is avoided, and meanwhile, the influence on user experience caused by abnormal cache is prevented.

In this embodiment, a data distribution program is arranged in the server, when the redis database is found to perform data writing, changing or deleting operations, the operated data is written into the cache database, the cache data is sorted by the cache queue, after the cache data is sorted, if the required data is found, the data is immediately fed back to the user, and if the required data is not found, the next data queue is read, so that the cache update of the data is more reliable and faster.

In this embodiment, a data monitoring program is arranged in the data distribution program, and if it is monitored that reading of certain data becomes very frequent, the data is placed in front of a data queue in the cache database, and the cache data queue schedules the data to the data queue closer to the data queue for the data with the more frequent reading, and the frequent times can be defined by itself, for example, 100 times are read within 1 second, but the specific situation is not limited to 100 times, and the definition of the frequent times needs to be determined according to actual operations, thereby improving the efficiency of data caching.

In the embodiment, the mobile terminal comprises a mobile terminal, wherein a user login system, a data transmission system, a data receiving system and an internet system are arranged on the mobile terminal, the user logs in the mobile terminal through the user login system, the mobile terminal sends a request instruction to a server through the internet system, the mobile terminal sends cache data to the server through the data transmission system and the data receiving system, the server is used for sending a data cache instruction or a data expiration instruction to a database, and expired data information is fed back to the mobile terminal through the server, so that the user can receive the data expiration information in time, and remote data caching is realized.

In summary, in the method for achieving expiration and automatic refresh of the cache, when the method is used, as shown in fig. 1, a user sends a request instruction to a server, the server obtains a redis cache, the server allocates the redis cache to a cache database backup, the server requests the cache database to query the backup when the redis data cache does not exist, the server sets expiration time or automatic update time of cache data of the redis database, and the expiration data in the redis database is automatically cleared through a ttl cache, so that an application program is easier to write, maintain and expand; the method and the device have the advantages that the cache updating line is shortened, so that the cache updating of the data is more reliable and faster, the experience of a user is improved, and the security of a persistent storage database is protected; the cache can be automatically refreshed, and the insufficient timeliness of the cache is avoided.

Although embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that changes, modifications, substitutions and alterations can be made in these embodiments without departing from the principles and spirit of the invention, the scope of which is defined in the appended claims and their equivalents.

6页详细技术资料下载
上一篇:一种医用注射器针头装配设备
下一篇:一种数据处理方法及装置

网友询问留言

已有0条留言

还没有人留言评论。精彩留言会获得点赞!

精彩留言,会给你点赞!