Method, device, equipment and storage medium for setting the retention period of cached data
By judging the call frequency and update of data in the cache, and dynamically updating its shelf life, the limitations of the data elimination mechanism in the existing cache are solved and the efficiency of cache usage is improved.
Patent Information
- Application Number
- CN202210601484.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-05-30
- Publication Date
- 2025-01-14
- Estimated Expiration
- 2042-05-30
AI Technical Summary
The existing cached invalidation strategy may eliminate data that has been stored for the longest time but is frequently called, resulting in limitations in the data elimination mechanism.
When storing data in the cache, it is determined whether the data is stored for the first time. If it is not the first time, it is determined whether its calling frequency is greater than the frequency threshold. If it is greater, it is updated according to the update interval time and the number of updates.
By combining the data call frequency, number of updates and update intervals, a more reasonable shelf life is set, making data elimination more reasonable and improving cache usage efficiency.
Smart Images

Figure CN114968108B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of data preservation technology, and in particular to a method, device, equipment and storage medium for setting the preservation period of cached data. Background Art
[0002] During project development, developers often use cache to store infrequently changing data to improve system response speed and relieve pressure on servers and databases. At the same time, in order to ensure that the cache has sufficient storage space, developers need to eliminate invalid data in the cache in a timely manner. The existing cache invalidation strategy usually requires the system to automatically eliminate the data that has been stored for the longest time, which may also eliminate the data that has been stored for the longest time but is frequently called, which has certain limitations. Summary of the invention
[0003] The main purpose of the present application is to provide a method, device, equipment and storage medium for setting the retention period of cache data, aiming to solve the disadvantage that the elimination mechanism of invalid data in the existing cache has limitations.
[0004] To achieve the above purpose, the present application provides a method for setting the storage period of cached data, the method comprising:
[0005] When storing data in the cache, determining whether the data is stored for the first time;
[0006] If the data is not stored for the first time, determining whether the calling frequency of the data is greater than a frequency threshold;
[0007] If the calling frequency of the data is greater than the frequency threshold, the retention period corresponding to the data is updated according to the update interval and the number of updates of the data.
[0008] The present application also provides a device for setting a storage period of cached data, the device comprising:
[0009] A first judgment module, used to judge whether the data is stored for the first time when storing the data in the cache;
[0010] A second judgment module is used to judge whether the calling frequency of the data is greater than a frequency threshold if the data is not stored for the first time;
[0011] The first updating module is used to update the storage period corresponding to the data according to the update interval and the number of updates of the data if the calling frequency of the data is greater than the frequency threshold.
[0012] The present application also provides a computer device, comprising a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of any one of the above methods when executing the computer program.
[0013] The present application also provides a computer-readable storage medium having a computer program stored thereon, and when the computer program is executed by a processor, the steps of any of the above-mentioned methods are implemented.
[0014] A method, device, equipment and storage medium for setting the shelf life of cached data provided in the present application, when storing data in the cache, the system first determines whether the data is stored for the first time. If the data is not stored for the first time, it is further determined whether the calling frequency of the data is greater than the frequency threshold. If the calling frequency of the data is greater than the frequency threshold, the corresponding shelf life of the data is updated according to the update interval and the number of updates of the data. The present application sets the shelf life of the data in multiple dimensions by combining the calling frequency, the number of updates and the update interval of the data, so that the shelf life of the data in the cache matches the actual calling situation, and thus it is more reasonable for the system to automatically eliminate the data according to the set shelf life. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] Figure 1 This is a schematic diagram of the steps of a method for setting the retention period of cached data in one embodiment of the present application;
[0016] Figure 2 It is a schematic diagram of the calculation steps of the data retention period in one embodiment of the present application;
[0017] Figure 3 This is an overall structural block diagram of a device for setting a retention period for cached data in one embodiment of the present application;
[0018] Figure 4 It is a schematic block diagram of the structure of a computer device according to an embodiment of the present application.
[0019] The realization of the purpose, functional features and advantages of this application will be further explained in conjunction with embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION
[0020] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0021] Reference Figure 1 In one embodiment of the present application, a method for setting a storage period of cached data is provided, and the storage period setting method includes steps S1-S3:
[0022] S1: When storing data in the cache, determining whether the data is stored for the first time;
[0023] S2: If the data is not stored for the first time, determining whether the calling frequency of the data is greater than a frequency threshold;
[0024] S3: If the calling frequency of the data is greater than the frequency threshold, the retention period corresponding to the data is updated according to the update interval and the number of updates of the data.
[0025] In this embodiment, when storing data in the cache, the system checks whether the data has been stored in the cache according to the key, so as to determine whether the data is stored for the first time. If the data is not in the cache, it is determined that the data is stored for the first time, and the system puts the data into the cache, and defines various options of the storage information of the data according to the default settings, such as the initial storage time, update time, number of updates, and storage period of the data. If the data exists in the cache, the system determines that the data is not stored for the first time; the system retrieves the update number, initial storage time, and last update time of the data from the storage information corresponding to the data, first calculates the time interval between the last update time and the initial storage time, and obtains the storage interval length. Then, the number of updates is divided by the storage interval length to obtain the ratio between the two, which represents the call frequency corresponding to the current data. The system retrieves the preset frequency threshold, and compares the call frequency of the data with the frequency threshold to determine the size relationship between the two. If the call frequency of the data is greater than the frequency threshold, it means that the data is called more frequently, and the storage period should be set according to its actual call situation so that the storage period of the data is adapted to the actual call situation. Specifically, the system obtains all update moments from the update time set corresponding to the data (each time the data is called, the calling moment will be written into the update time set as the update moment according to the time sequence), and then calculates the time intervals between two adjacent update moments based on the time sequence to obtain several update interval durations. The system selects the maximum update interval duration and the minimum interval duration from each update interval duration, and calculates the difference between the maximum update interval duration and the minimum interval duration to obtain the interval duration difference. The system calls the preset difference threshold, and compares the current interval duration difference with the difference threshold to determine the size relationship between the two. If the interval duration difference is less than the difference threshold, the storage period of the data is updated to the default period. If the interval duration difference is not less than the difference threshold, the system counts the sum of each update interval duration to obtain the total interval duration. Then the total interval duration is divided by the number of updates to obtain the ratio between the two, and the storage period of the data is updated to the ratio.
[0026] This embodiment sets the data retention period in multiple dimensions by combining the data call frequency, update times and update interval length, so that the data retention period in the cache matches the actual call situation, thereby making it more reasonable for the system to automatically eliminate data according to the set retention period, and effectively improving the efficiency of cache use.
[0027] Furthermore, when storing data in the cache, after the step of determining whether the data is stored for the first time, steps S4-S5 are included:
[0028] S4: if the data is stored for the first time, the data is stored in a cache, and storage information associated with the data is constructed, the storage information including an initial storage time, an update time, a number of updates, and a retention period;
[0029] S5: Get the current time, set the initial storage time as the current time; add the current time as the update time to the update time set; set the number of updates to the initial value; and set the storage period to the default period.
[0030] In this embodiment, if the system recognizes that the data is stored for the first time, the data is written into the preset area of the cache, and storage information associated with the data is constructed. The storage information of the data includes the initial storage time, update time, number of updates, storage period, data type, etc., wherein the update time exists in the form of a set. Each time the data is called, the current time to be called is written into the set as the new update time, and the previous update time in the set will not be deleted or replaced, but arranged in sequence in time. For example, data A corresponds to the update time set a{update time 1, update time 2, update time 3,..., update time n}, update time 1 occurs before update time 2, update time 2 occurs before update time 3, and so on. The system obtains the current time corresponding to the first storage, and sets the initial storage time of the data as the current time. When the storage information of the subsequent data is updated, the initial storage time remains unchanged; at the same time, the system adds the current time as the current update time to the update time set, and the update time will not be replaced when the storage information is updated subsequently. In addition, the system sets the number of data updates to the initial value (the initial value is 1), and the number of updates increases gradually when the information is subsequently updated. Finally, the system calls the default period preset by the developer and sets the current storage period of the data to the default period (for example, if the default period is 90 days, the current storage period is 90 days).
[0031] Furthermore, after the step of updating the storage period corresponding to the data according to the update interval and the update number of the data, steps S6-S7 are included:
[0032] S6: writing the data into the cache and updating the update times;
[0033] S7: Retrieve the current time, and write the current time as the new update time into the last position of the update time set.
[0034] In this embodiment, after the system resets the storage period corresponding to the data according to the update interval and update number of the data, it writes the data into the preset area of the cache and updates the current update number so that the update number increases (for example, if the update number before the update is 4, the update number after the update is 5). In addition, the system retrieves the current time corresponding to the data storage, and writes the current time as the new update time in the time sequence to the last bit of the update time set, that is, as the most recent update time.
[0035] Furthermore, the step of determining whether the calling frequency of the data is greater than a frequency threshold comprises steps S201-S204:
[0036] S201: Retrieve the update times, initial storage time and last update time corresponding to the data;
[0037] S202: Calculate the storage interval between the last update time and the initial storage time, and calculate the ratio between the number of updates and the storage interval;
[0038] S203: Determine whether the ratio is greater than the frequency threshold;
[0039] S204: If the ratio is greater than the frequency threshold, it is determined that the calling frequency of the data is greater than the frequency threshold.
[0040] In this embodiment, the system finds the number of updates, the initial storage time and the last update time of the data according to the corresponding relationship between the data and the storage information (the last update time is the update time that ranks last in the update time set, that is, the update time closest to the current time). The system first calculates the time interval between the last update time and the initial storage time, thereby obtaining the storage interval length between the last update of the data and the initial storage. Then the number of updates is divided by the storage interval length to obtain the ratio between the two, which is the calling frequency of the data. Therefore, the system compares the ratio with the frequency threshold to determine the relationship between the two, which is equivalent to determining whether the calling frequency of the data is greater than the frequency threshold. If the ratio between the number of updates and the storage interval length is greater than the frequency threshold, it means that the data is called more frequently, and then it is determined that the calling frequency of the data is greater than the frequency threshold.
[0041] Reference Figure 2Further, the step of updating the storage period corresponding to the data according to the update interval and the number of updates of the data includes steps S301-S306:
[0042] S301: Obtain all update times from the update time set corresponding to the data;
[0043] S302: Calculate the time intervals between two adjacent update moments based on the time sequence to obtain a plurality of update interval durations;
[0044] S303: Filtering out a maximum update interval duration and a minimum update interval duration from each of the update interval durations, and calculating a difference between the maximum update interval duration and the minimum update interval duration to obtain an interval duration difference;
[0045] S304: Determine whether the interval duration difference is less than a difference threshold;
[0046] S305: If the interval time difference is less than the difference threshold, updating the storage period to a default period;
[0047] S306: If the interval duration difference is not less than the difference threshold, the total interval duration of each update interval duration is counted, and the retention period is updated using the ratio of the total interval duration to the number of updates.
[0048] In this embodiment, the system first retrieves all update moments from the update time set corresponding to the data. Then, based on the time sequence (each update moment in the update time set itself is based on the time sequence, and is arranged in sequence according to the time of occurrence), the time intervals between two adjacent update moments are calculated respectively to obtain several update interval durations. For example, the update time set includes update time 1, update time 2, and update time 3. The system calculates the time interval between update time 1 and update time 2 respectively to obtain update interval duration a; calculates the time interval between update time 2 and update time 3 to obtain update interval duration b. The system selects the maximum update interval duration with the maximum value and the minimum update interval duration with the minimum value from each update interval duration; then uses the maximum update interval duration to subtract the minimum update interval duration, calculates the difference between the two, and obtains the interval duration difference. The system retrieves the preset difference threshold, and compares the interval duration difference with the difference threshold to determine the size relationship between the two. If the interval duration difference is less than the difference threshold, it means that the change in the interval duration of each call of the data is not large. Therefore, the data retention period is directly updated to the default period set by the developer without complex calculations. It is convenient and quick. If the interval duration difference is not less than the difference threshold, it means that the change in the interval duration of each call of the data is large, and the data retention period needs to be appropriately set according to the actual call situation. Specifically, the system counts the sum of all update interval durations to obtain the total interval duration; then the total interval duration is divided by the number of data updates, and the ratio between the total interval duration and the number of updates is used as the new retention period, that is, the data retention period is updated using the ratio of the total interval duration to the number of updates.
[0049] Furthermore, the data storage information includes data type and data application service, and the step of updating the preservation period by using the ratio of the total interval duration to the number of updates further includes steps S30601-S30602:
[0050] S30601: Respectively retrieve the first correction coefficient corresponding to the data type and the second correction coefficient corresponding to the data application service;
[0051] S30602: Use the ratio of the total interval duration to the number of updates, and multiply it by the first correction coefficient and the second correction coefficient to obtain the preservation period.
[0052] In this embodiment, the storage information of the data includes the data type and the data application business. The developer sets different first correction coefficients according to different data types, and sets different second correction coefficients according to different data application businesses. The first correction coefficient and the second correction coefficient corresponding to the data with higher priority and higher importance have larger values, and the first correction coefficient and the second correction coefficient corresponding to the data with lower priority and lower importance have smaller values. Specifically, in order to match the data preservation period with the specific application scenario and make the preservation period of the data in the cache more reasonable, the system retrieves the corresponding first correction coefficient according to the data type of the current data; and retrieves the corresponding second correction coefficient according to the data application business of the data. Then, the ratio of the total value of the interval duration to the number of updates is used to multiply the first correction coefficient and the second correction coefficient (assuming that the ratio of the total value of the interval duration to the number of updates is x, the first correction coefficient is y, the second correction coefficient is z, and the preservation period is s, then s = x*y*z), so as to obtain a more reasonable and more accurate preservation period.
[0053] Furthermore, after the step of updating the storage period corresponding to the data according to the update interval and the update number of the data, steps S8-S9 are also included:
[0054] S8: monitoring whether the data is rewritten after the updated preservation period;
[0055] S9: If the data is not rewritten after the updated retention period, the data is removed from the cache.
[0056] In this embodiment, after setting the data retention period, the system monitors the data according to the updated retention period. If the data is not rewritten or called after the updated retention period (the data retention period remains unchanged and will not be updated), in order to ensure efficient use of the cache, the system will eliminate and remove the data from the cache. For example, if the retention period is from March 1, 2021 to June 1, 2021, if the retention period is not updated (indicating that the data has not been rewritten or called), the data will be removed from the cache after June 1, 2021.
[0057] Reference Figure 3 In one embodiment of the present application, a device for setting the storage period of cached data is provided, the device comprising:
[0058] A first judging module 1, used to judge whether the data is stored for the first time when storing the data in the cache;
[0059] The second judgment module 2 is used to judge whether the calling frequency of the data is greater than a frequency threshold if the data is not stored for the first time;
[0060] The first updating module 3 is used to update the storage period corresponding to the data according to the update interval and the number of updates of the data if the calling frequency of the data is greater than the frequency threshold.
[0061] In this embodiment, when storing data in the cache, the system checks whether the data has been stored in the cache according to the key, so as to determine whether the data is stored for the first time. If the data is not in the cache, it is determined that the data is stored for the first time, and the system puts the data into the cache, and defines various options of the storage information of the data according to the default settings, such as the initial storage time, update time, number of updates, and storage period of the data. If the data exists in the cache, the system determines that the data is not stored for the first time; the system retrieves the update number, initial storage time, and last update time of the data from the storage information corresponding to the data, first calculates the time interval between the last update time and the initial storage time, and obtains the storage interval length. Then, the number of updates is divided by the storage interval length to obtain the ratio between the two, which represents the call frequency corresponding to the current data. The system retrieves the preset frequency threshold, and compares the call frequency of the data with the frequency threshold to determine the size relationship between the two. If the call frequency of the data is greater than the frequency threshold, it means that the data is called more frequently, and the storage period should be set according to its actual call situation so that the storage period of the data is adapted to the actual call situation. Specifically, the system obtains all update moments from the update time set corresponding to the data (each time the data is called, the calling moment will be written into the update time set as the update moment according to the time sequence), and then calculates the time intervals between two adjacent update moments based on the time sequence to obtain several update interval durations. The system selects the maximum update interval duration and the minimum interval duration from each update interval duration, and calculates the difference between the maximum update interval duration and the minimum interval duration to obtain the interval duration difference. The system calls the preset difference threshold, and compares the current interval duration difference with the difference threshold to determine the size relationship between the two. If the interval duration difference is less than the difference threshold, the storage period of the data is updated to the default period. If the interval duration difference is not less than the difference threshold, the system counts the sum of each update interval duration to obtain the total interval duration. Then the total interval duration is divided by the number of updates to obtain the ratio between the two, and the storage period of the data is updated to the ratio.
[0062] This embodiment sets the data retention period in multiple dimensions by combining the data call frequency, update times and update interval length, so that the data retention period in the cache matches the actual call situation, thereby making it more reasonable for the system to automatically eliminate data according to the set retention period, and effectively improving the efficiency of cache use.
[0063] Furthermore, the storage period setting device further includes:
[0064] Construction module 4, for storing the data in a cache if the data is stored for the first time, and constructing storage information associated with the data, the storage information including an initial storage time, an update time, a number of updates, and a storage period;
[0065] The setting module 5 is used to obtain the current time, set the initial storage time as the current time; add the current time as the update time to the update time set; set the number of updates to the initial value; and set the storage period to the default period.
[0066] In this embodiment, if the system recognizes that the data is stored for the first time, the data is written into the preset area of the cache, and storage information associated with the data is constructed. The storage information of the data includes the initial storage time, update time, number of updates, storage period, data type, etc., wherein the update time exists in the form of a set. Each time the data is called, the current time to be called is written into the set as the new update time, and the previous update time in the set will not be deleted or replaced, but arranged in sequence in time. For example, data A corresponds to the update time set a{update time 1, update time 2, update time 3,..., update time n}, update time 1 occurs before update time 2, update time 2 occurs before update time 3, and so on. The system obtains the current time corresponding to the first storage, and sets the initial storage time of the data as the current time. When the storage information of the subsequent data is updated, the initial storage time remains unchanged; at the same time, the system adds the current time as the current update time to the update time set, and the update time will not be replaced when the storage information is updated subsequently. In addition, the system sets the number of data updates to the initial value (the initial value is 1), and the number of updates increases gradually when the information is subsequently updated. Finally, the system calls the default period preset by the developer and sets the current storage period of the data to the default period (for example, if the default period is 90 days, the current storage period is 90 days).
[0067] Furthermore, the storage period setting device further includes:
[0068] A second updating module 6, used for writing the data into the cache and updating the update times;
[0069] The writing module 7 is used to retrieve the current time and write the current time as the new update time into the last position of the update time set.
[0070] In this embodiment, after the system resets the storage period corresponding to the data according to the update interval and update number of the data, it writes the data into the preset area of the cache and updates the current update number so that the update number increases (for example, if the update number before the update is 4, the update number after the update is 5). In addition, the system retrieves the current time corresponding to the data storage, and writes the current time as the new update time in the time sequence to the last bit of the update time set, that is, as the most recent update time.
[0071] Furthermore, the second judgment module 2 includes:
[0072] A retrieving unit, used to retrieve the update times, initial storage time and last update time corresponding to the data;
[0073] A first calculation unit, used to calculate the storage interval between the last update time and the initial storage time, and calculate the ratio between the number of updates and the storage interval;
[0074] A first judging unit, configured to judge whether the ratio is greater than the frequency threshold;
[0075] A determination unit is configured to determine that the calling frequency of the data is greater than the frequency threshold if the ratio is greater than the frequency threshold.
[0076] In this embodiment, the system finds the number of updates, the initial storage time and the last update time of the data according to the corresponding relationship between the data and the storage information (the last update time is the update time that ranks last in the update time set, that is, the update time closest to the current time). The system first calculates the time interval between the last update time and the initial storage time, thereby obtaining the storage interval length between the last update of the data and the initial storage. Then the number of updates is divided by the storage interval length to obtain the ratio between the two, which is the calling frequency of the data. Therefore, the system compares the ratio with the frequency threshold to determine the relationship between the two, which is equivalent to determining whether the calling frequency of the data is greater than the frequency threshold. If the ratio between the number of updates and the storage interval length is greater than the frequency threshold, it means that the data is called more frequently, and then it is determined that the calling frequency of the data is greater than the frequency threshold.
[0077] Furthermore, the first updating module 3 includes:
[0078] An acquisition unit, used to acquire all update times from an update time set corresponding to the data;
[0079] A second calculation unit is used to calculate the time interval between two adjacent update moments based on the time sequence to obtain a plurality of update interval durations;
[0080] A third calculation unit is used to select a maximum update interval duration and a minimum update interval duration from each of the update interval durations, and calculate a difference between the maximum update interval duration and the minimum update interval duration to obtain an interval duration difference;
[0081] A second judgment unit is used to judge whether the interval duration difference is less than a difference threshold;
[0082] A first updating unit, configured to update the storage period to a default period if the interval duration difference is less than the difference threshold;
[0083] The second updating unit is used to count the total interval durations of the update interval durations if the interval duration difference is not less than the difference threshold, and use the ratio of the total interval duration to the number of updates to update the preservation period.
[0084] In this embodiment, the system first retrieves all update moments from the update time set corresponding to the data. Then, based on the time sequence (each update moment in the update time set itself is based on the time sequence, and is arranged in sequence according to the time of occurrence), the time intervals between two adjacent update moments are calculated respectively to obtain several update interval durations. For example, the update time set includes update time 1, update time 2, and update time 3. The system calculates the time interval between update time 1 and update time 2 respectively to obtain update interval duration a; calculates the time interval between update time 2 and update time 3 to obtain update interval duration b. The system selects the maximum update interval duration with the maximum value and the minimum update interval duration with the minimum value from each update interval duration; then uses the maximum update interval duration to subtract the minimum update interval duration, calculates the difference between the two, and obtains the interval duration difference. The system retrieves the preset difference threshold, and compares the interval duration difference with the difference threshold to determine the size relationship between the two. If the interval duration difference is less than the difference threshold, it means that the change in the interval duration of each call of the data is not large. Therefore, the data retention period is directly updated to the default period set by the developer without complex calculations. It is convenient and quick. If the interval duration difference is not less than the difference threshold, it means that the change in the interval duration of each call of the data is large, and the data retention period needs to be appropriately set according to the actual call situation. Specifically, the system counts the sum of all update interval durations to obtain the total interval duration; then the total interval duration is divided by the number of data updates, and the ratio between the total interval duration and the number of updates is used as the new retention period, that is, the data retention period is updated using the ratio of the total interval duration to the number of updates.
[0085] Furthermore, the storage information of the data includes data type and data application service, and the second updating unit further includes:
[0086] A calling subunit, used to call the first correction coefficient corresponding to the data type and the second correction coefficient corresponding to the data application service respectively;
[0087] The calculation subunit is used to use the ratio of the total interval duration to the number of updates, and multiply it by the first correction coefficient and the second correction coefficient to obtain the preservation period.
[0088] In this embodiment, the storage information of the data includes the data type and the data application business. The developer sets different first correction coefficients according to different data types, and sets different second correction coefficients according to different data application businesses. The first correction coefficient and the second correction coefficient corresponding to the data with higher priority and higher importance have larger values, and the first correction coefficient and the second correction coefficient corresponding to the data with lower priority and lower importance have smaller values. Specifically, in order to match the data preservation period with the specific application scenario and make the preservation period of the data in the cache more reasonable, the system retrieves the corresponding first correction coefficient according to the data type of the current data; and retrieves the corresponding second correction coefficient according to the data application business of the data. Then, the ratio of the total value of the interval duration to the number of updates is used to multiply the first correction coefficient and the second correction coefficient (assuming that the ratio of the total value of the interval duration to the number of updates is x, the first correction coefficient is y, the second correction coefficient is z, and the preservation period is s, then s = x*y*z), so as to obtain a more reasonable and more accurate preservation period.
[0089] Furthermore, the storage period setting device further includes:
[0090] A monitoring module 8, used to monitor whether the data is rewritten after the updated preservation period;
[0091] The removal module 9 is configured to remove the data from the cache if the data is not rewritten after the updated preservation period.
[0092] In this embodiment, after setting the data retention period, the system monitors the data according to the updated retention period. If the data is not rewritten or called after the updated retention period (the data retention period remains unchanged and will not be updated), in order to ensure efficient use of the cache, the system will eliminate and remove the data from the cache. For example, if the retention period is from March 1, 2021 to June 1, 2021, if the retention period is not updated (indicating that the data has not been rewritten or called), the data will be removed from the cache after June 1, 2021.
[0093] Reference Figure 4 In an embodiment of the present application, a computer device is also provided. The computer device may be a server, and its internal structure may be as follows: Figure 4As shown. The computer device includes a processor, a memory, a network interface and a database connected through a system bus. Among them, the processor designed by the computer is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and a cache memory. The non-volatile storage medium stores an operating system, a computer program and a database. The cache memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database of the computer device is used to store data such as storage information. The network interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a method for setting the preservation period of cached data is implemented.
[0094] The processor executes the following steps to set the cache data retention period:
[0095] S1: When storing data in the cache, determining whether the data is stored for the first time;
[0096] S2: If the data is not stored for the first time, determining whether the calling frequency of the data is greater than a frequency threshold;
[0097] S3: If the calling frequency of the data is greater than the frequency threshold, the retention period corresponding to the data is updated according to the update interval and the number of updates of the data.
[0098] Furthermore, when storing data in the cache, after the step of determining whether the data is stored for the first time, steps S4-S5 are included:
[0099] S4: if the data is stored for the first time, the data is stored in a cache, and storage information associated with the data is constructed, the storage information including an initial storage time, an update time, a number of updates, and a retention period;
[0100] S5: Get the current time, set the initial storage time as the current time; add the current time as the update time to the update time set; set the number of updates to the initial value; and set the storage period to the default period.
[0101] Furthermore, after the step of updating the storage period corresponding to the data according to the update interval and the update number of the data, steps S6-S7 are included:
[0102] S6: writing the data into the cache and updating the update times;
[0103] S7: Retrieve the current time, and write the current time as the new update time into the last position of the update time set.
[0104] Furthermore, the step of determining whether the calling frequency of the data is greater than a frequency threshold comprises steps S201-S204:
[0105] S201: Retrieve the update times, initial storage time and last update time corresponding to the data;
[0106] S202: Calculate the storage interval between the last update time and the initial storage time, and calculate the ratio between the number of updates and the storage interval;
[0107] S203: Determine whether the ratio is greater than the frequency threshold;
[0108] S204: If the ratio is greater than the frequency threshold, it is determined that the calling frequency of the data is greater than the frequency threshold.
[0109] Furthermore, the step of updating the storage period corresponding to the data according to the update interval and the number of updates of the data includes steps S301-S306:
[0110] S301: Obtain all update times from the update time set corresponding to the data;
[0111] S302: Calculate the time intervals between two adjacent update moments based on the time sequence to obtain a plurality of update interval durations;
[0112] S303: Filtering out a maximum update interval duration and a minimum update interval duration from each of the update interval durations, and calculating a difference between the maximum update interval duration and the minimum update interval duration to obtain an interval duration difference;
[0113] S304: Determine whether the interval duration difference is less than a difference threshold;
[0114] S305: If the interval time difference is less than the difference threshold, updating the storage period to a default period;
[0115] S306: If the interval duration difference is not less than the difference threshold, the total interval duration of each update interval duration is counted, and the retention period is updated using the ratio of the total interval duration to the number of updates.
[0116] Furthermore, the data storage information includes data type and data application service, and the step of updating the preservation period by using the ratio of the total interval duration to the number of updates further includes steps S30601-S30602:
[0117] S30601: Respectively retrieve the first correction coefficient corresponding to the data type and the second correction coefficient corresponding to the data application service;
[0118] S30602: Use the ratio of the total interval duration to the number of updates, and multiply it by the first correction coefficient and the second correction coefficient to obtain the preservation period.
[0119] Furthermore, after the step of updating the storage period corresponding to the data according to the update interval and the update number of the data, steps S8-S9 are also included:
[0120] S8: monitoring whether the data is rewritten after the updated preservation period;
[0121] S9: If the data is not rewritten after the updated retention period, the data is removed from the cache.
[0122] An embodiment of the present application further provides a computer-readable storage medium having a computer program stored thereon. When the computer program is executed by a processor, a method for setting a retention period of cached data is implemented. The method for setting a retention period of cached data includes:
[0123] S1: When storing data in the cache, determining whether the data is stored for the first time;
[0124] S2: If the data is not stored for the first time, determining whether the calling frequency of the data is greater than a frequency threshold;
[0125] S3: If the calling frequency of the data is greater than the frequency threshold, the retention period corresponding to the data is updated according to the update interval and the number of updates of the data.
[0126] Furthermore, when storing data in the cache, after the step of determining whether the data is stored for the first time, steps S4-S5 are included:
[0127] S4: if the data is stored for the first time, the data is stored in a cache, and storage information associated with the data is constructed, the storage information including an initial storage time, an update time, a number of updates, and a retention period;
[0128] S5: Get the current time, set the initial storage time as the current time; add the current time as the update time to the update time set; set the number of updates to the initial value; and set the storage period to the default period.
[0129] Furthermore, after the step of updating the storage period corresponding to the data according to the update interval and the update number of the data, steps S6-S7 are included:
[0130] S6: writing the data into the cache and updating the update times;
[0131] S7: Retrieve the current time, and write the current time as the new update time into the last position of the update time set.
[0132] Furthermore, the step of determining whether the calling frequency of the data is greater than a frequency threshold comprises steps S201-S204:
[0133] S201: Retrieve the update times, initial storage time and last update time corresponding to the data;
[0134] S202: Calculate the storage interval between the last update time and the initial storage time, and calculate the ratio between the number of updates and the storage interval;
[0135] S203: Determine whether the ratio is greater than the frequency threshold;
[0136] S204: If the ratio is greater than the frequency threshold, it is determined that the calling frequency of the data is greater than the frequency threshold.
[0137] Furthermore, the step of updating the storage period corresponding to the data according to the update interval and the number of updates of the data includes steps S301-S306:
[0138] S301: Obtain all update times from the update time set corresponding to the data;
[0139] S302: Calculate the time intervals between two adjacent update moments based on the time sequence to obtain a plurality of update interval durations;
[0140] S303: Filtering out a maximum update interval duration and a minimum update interval duration from each of the update interval durations, and calculating a difference between the maximum update interval duration and the minimum update interval duration to obtain an interval duration difference;
[0141] S304: Determine whether the interval duration difference is less than a difference threshold;
[0142] S305: If the interval time difference is less than the difference threshold, updating the storage period to a default period;
[0143] S306: If the interval duration difference is not less than the difference threshold, the total interval duration of each update interval duration is counted, and the retention period is updated using the ratio of the total interval duration to the number of updates.
[0144] Furthermore, the data storage information includes data type and data application service, and the step of updating the preservation period by using the ratio of the total interval duration to the number of updates further includes steps S30601-S30602:
[0145] S30601: Respectively retrieve the first correction coefficient corresponding to the data type and the second correction coefficient corresponding to the data application service;
[0146] S30602: Use the ratio of the total interval duration to the number of updates, and multiply it by the first correction coefficient and the second correction coefficient to obtain the preservation period.
[0147] Furthermore, after the step of updating the storage period corresponding to the data according to the update interval and the update number of the data, steps S8-S9 are also included:
[0148] S8: monitoring whether the data is rewritten after the updated preservation period;
[0149] S9: If the data is not rewritten after the updated retention period, the data is removed from the cache.
[0150] Those of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing related hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media provided in this application and used in the embodiments may include non-volatile and / or volatile memory. Non-volatile memory may include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory may include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in a variety of forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (SSRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), etc.
[0151] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, device, article or method including a series of elements includes not only those elements, but also includes other elements not explicitly listed, or also includes elements inherent to such process, device, article or method. In the absence of further restrictions, an element defined by the sentence "includes a ..." does not exclude the presence of other identical elements in the process, device, article or method including the element.
[0152] The above description is only a preferred embodiment of the present application, and does not limit the patent scope of the present application. Any equivalent structure or equivalent process transformation made using the contents of the present application specification and drawings, or directly or indirectly used in other related technical fields, are also included in the patent protection scope of the present application.
Claims
1. A method for setting a storage period of cached data, characterized in that: The method comprises: When storing data in the cache, determining whether the data is stored for the first time; If the data is not stored for the first time, determining whether the calling frequency of the data is greater than a frequency threshold; If the calling frequency of the data is greater than the frequency threshold, updating the retention period corresponding to the data according to the update interval and the number of updates of the data; The step of updating the storage period corresponding to the data according to the update interval and the number of updates of the data includes: Obtain all update times from the update time set corresponding to the data; Calculate the time intervals between two adjacent update moments based on the time sequence to obtain a plurality of update interval durations; Filtering out a maximum update interval duration and a minimum update interval duration from each of the update interval durations, and calculating a difference between the maximum update interval duration and the minimum update interval duration to obtain an interval duration difference; Determine whether the interval duration difference is less than a difference threshold; If the interval time difference is less than the difference threshold, the preservation period is updated to a default period; If the interval duration difference is not less than the difference threshold, the total interval duration of each update interval duration is counted, and the retention period is updated using the ratio of the total interval duration to the number of updates; The data storage information includes data type and data application service, and the step of updating the preservation period by using the ratio of the total interval duration to the number of updates further includes: Respectively retrieve a first correction coefficient corresponding to the data type and a second correction coefficient corresponding to the data application service; The retention period is calculated by multiplying the ratio of the total interval duration to the number of updates by the first correction coefficient and the second correction coefficient.
2. The method for setting the cache data preservation period according to claim 1, characterized in that: After the step of determining whether the data is stored for the first time when storing the data in the cache, the method includes: If the data is stored for the first time, the data is stored in the cache, and storage information associated with the data is constructed, the storage information including the initial storage time, update time, update times and retention period; Get the current time, set the initial storage time as the current time; add the current time as the update time to the update time set; set the number of updates to the initial value; and set the storage period to the default period.
3. The method for setting the storage period of cache data according to claim 2, characterized in that: After the step of updating the storage period corresponding to the data according to the update interval and the number of updates of the data, the method includes: Writing the data into the cache and updating the update times; The current time is retrieved and written as a new update time at the end of the update time set.
4. The method for setting the storage period of cache data according to claim 1, characterized in that: The step of determining whether the calling frequency of the data is greater than a frequency threshold comprises: Retrieve the update times, initial storage time and last update time corresponding to the data; Calculate the storage interval between the last update time and the initial storage time, and calculate the ratio between the number of updates and the storage interval; Determining whether the ratio is greater than the frequency threshold; If the ratio is greater than the frequency threshold, it is determined that the calling frequency of the data is greater than the frequency threshold.
5. The method for setting the storage period of cache data according to claim 1, characterized in that: After the step of updating the storage period corresponding to the data according to the update interval and the number of updates of the data, the method further includes: monitoring whether the data is rewritten after the updated retention period; If the data is not rewritten after the updated retention period, the data is removed from the cache.
6. A device for setting the storage period of cached data, characterized in that: The device comprises: A first judgment module, used to judge whether the data is stored for the first time when storing the data in the cache; A second judgment module is used to judge whether the calling frequency of the data is greater than a frequency threshold if the data is not stored for the first time; A first updating module, configured to update the storage period corresponding to the data according to the update interval and the number of updates of the data if the call frequency of the data is greater than the frequency threshold; The first update module includes: An acquisition unit, used to acquire all update times from an update time set corresponding to the data; A second calculation unit is used to calculate the time interval between two adjacent update moments based on the time sequence to obtain a plurality of update interval durations; A third calculation unit is used to select a maximum update interval duration and a minimum update interval duration from each of the update interval durations, and calculate a difference between the maximum update interval duration and the minimum update interval duration to obtain an interval duration difference; A second judgment unit is used to judge whether the interval duration difference is less than a difference threshold; A first updating unit, configured to update the storage period to a default period if the interval duration difference is less than the difference threshold; A second updating unit is configured to count the total interval durations of the update interval durations if the interval duration difference is not less than the difference threshold, and update the preservation period using the ratio of the total interval duration to the number of updates; The storage information of the data includes data type and data application service, and the second updating unit further includes: A calling subunit, used to call a first correction coefficient corresponding to the data type and a second correction coefficient corresponding to the data application service respectively; The calculation subunit is used to use the ratio of the total interval duration to the number of updates, and multiply it by the first correction coefficient and the second correction coefficient to obtain the preservation period.
7. A computer device comprising a memory and a processor, wherein a computer program is stored in the memory, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 5 are implemented.
8. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 5 are implemented.
Citation Information
Patent Citations
Dynamic cache data invalidity dispatching method and device and cache system
CN108287878A
Time interval determination method and device, electronic equipment and medium
CN110321298A