A Process Cache Management Method Based on the dotNet Platform
By improving on the ConcurrentDictionary class, combining data classification, multi-zone synchronization locking and asynchronous task processing, cache management is optimized, and the problems of single communication overhead, memory overhead and cache failure strategy in the existing cache management solution are solved, achieving more efficient cache data management.
Patent Information
- Application Number
- CN202411612968.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-13
- Publication Date
- 2025-08-01
- Estimated Expiration
- 2044-11-13
AI Technical Summary
The existing process cache management solution has problems such as large communication overhead, object serialization and deserialization overhead, large memory overhead, single cache failure strategy, and defects in the LRU and LFU algorithms, resulting in inefficient cache management.
Based on the ConcurrentDictionary class, data classification, multi-zone synchronization lock control, capacity control and dynamic deletion strategies are added. By adding the last access time field to the cached data, partition traversal and asynchronous task processing are adopted to optimize the cache management method.
It realizes lightweight process cache management, improves the hit rate of cached data, simplifies the algorithm complexity of eliminating deleted elements, reduces the impact of high concurrency, and ensures that more than 80% of the scene performance is faster than the LRU algorithm.
Smart Images

Figure CN119537430B_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present invention relate to the field of computer technology, and in particular, to a process cache management method based on the dotNet platform. Background Art
[0002] Currently, in the.NET environment, the in-process cache solutions mainly include the following:
[0003] 1. MemoryCache: A lightweight in-memory cache mechanism in.NET Core, suitable for storing short-lived data. It can be used in ASP.NET Core applications through dependency injection.
[0004] 2. CacheManager: An open-source.NET cache abstraction layer that supports multiple cache providers such as MemoryCache, Redis, Memcached, etc., and provides advanced functions such as cache synchronization, concurrent updates, and event handling.
[0005] 3. EasyCaching: An open-source cache library that provides a unified abstract cache interface and supports multiple cache providers such as InMemory, Redis, Memcached, etc.
[0006] 4. FusionCache: A.NET framework for building efficient cache systems that supports multiple cache strategies such as in-memory cache and distributed cache.
[0007] 5. Microsoft.Extensions.Caching.Memor: This is the in-memory cache implementation provided by Microsoft officially, mainly used in ASP.NET Core projects.
[0008] 6. System.Runtime.Caching: A namespace introduced in.NET Framework 4.0 that provides a simple cache mechanism suitable for stand-alone applications.
[0009] 7. Microsoft Caching: A cache library provided by Microsoft officially that supports the implementation of in-memory cache and distributed cache, such as the distributed cache of SQL Server and Redis.
[0010] Each cache solution has its specific application scenarios, advantages, and disadvantages. Generally speaking, they are all a huge cache management system and have the following problems:
[0011] Problems with out-of-process caches such as Redis:
[0012] 2) There is communication overhead;
[0013] 2) Object serialization and deserialization overhead;
[0014] 3) Reliability, vulnerable to factors such as network jitter;
[0015] Problems of ordinary memory cache:
[0016] 1) Memory overhead: Generally, there is no effective control over the memory capacity, which easily leads to the cache system occupying too much system memory.
[0017] 2) Disadvantages of the LRU (Least recently used) algorithm: The LRU algorithm is easily affected by short-term cold data, which may cause hot data to be prematurely evicted. In addition, the problem of periodic data access may also lead to unnecessary overhead.
[0018] 3) Disadvantages of the LFU (Least Frequently Used) algorithm: The LFU algorithm overly focuses on the cumulative access times of data, which may cause some "historically popular data" to be retained for a long time, even if it has not been accessed for a long time. In addition, the implementation and storage logic of frequency statistics also require more computing resources, especially in high-concurrency scenarios with frequent accesses, which may bring performance overhead.
[0019] 3) Single cache invalidation strategy: A reasonable cache invalidation strategy is crucial for improving the cache hit rate. Appropriate survival time and active expiration strategies can be set to ensure the freshness and relevance of cache data. However, a single strategy can never perfectly solve all problems. Summary of the Invention
[0020] The embodiments of the present invention provide a process cache management method based on the dotNet platform, which implements a lightweight process cache management method and can solve at least one of the above problems.
[0021] In a first aspect, the embodiments of the present invention provide a process cache management method based on the dotNet platform, including:
[0022] Adding a last access time field to each piece of data in the cache;
[0023] In response to a data query request, modifying the last access time field of the queried data to the current time;
[0024] In response to a cache data addition request, inserting the newly added data into the cache, and in the case where the cache capacity exceeds the maximum limit after insertion, traversing the cache partition, and deleting some data according to the last access time field of the data being traversed and the total number of data that has been traversed;
[0025] Among them, the partition being traversed is locked, and other partitions can execute other threads.
[0026] In a second aspect, an embodiment of the present invention provides an electronic device, which includes:
[0027] One or more processors;
[0028] A memory for storing one or more programs,
[0029] When the one or more programs are executed by the one or more processors, the one or more processors implement the process cache management method based on the dotNet platform described in any embodiment.
[0030] In summary, the embodiment of the present invention provides a process cache management method based on the dotNet platform. Based on the improvement of the ConcurrentDictionary class code, strategies such as multi-region synchronization lock control, capacity control, and dynamic deletion are added. It can effectively control the cache capacity, improve the hit rate of cache data, and implement a lightweight process cache management mechanism. Specifically, this method simplifies the algorithm complexity of eliminating and deleting elements; the operation of eliminating and deleting elements adopts partition lock operation, which has very limited impact on high concurrency; the dynamically maintained data is as little as possible, and the performance impact on query operations is minimized (compared with algorithms such as LRU); it focuses on the lightweight and practicality of the method, and can ensure that the performance in more than 80% of the scenarios is faster than the LRU algorithm; the operation of eliminating and deleting elements adopts an asynchronous operation method, which does not affect the performance of adding elements. BRIEF DESCRIPTION OF THE DRAWINGS
[0031] In order to more clearly illustrate the specific embodiments of the present invention or the technical solutions in the prior art, the following will briefly introduce the drawings required for the description of the specific embodiments or the prior art. Obviously, the following drawings are some embodiments of the present invention. For those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative efforts.
[0032] Figure 1 is a flowchart of a process cache management method based on the dotNet platform provided by an embodiment of the present invention;
[0033] Figure 2 is a schematic structural diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0034] To make the objectives, technical solutions and advantages of the present invention clearer, the technical solutions of the present invention will be described clearly and completely below. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts shall fall within the scope of protection of the present invention.
[0035] In the description of the present invention, it should be noted that the orientation or positional relationship indicated by the terms "center", "upper", "lower", "left", "right", "vertical", "horizontal", "inner", "outer", etc. is based on the orientation or positional relationship shown in the drawings. It is only for the convenience of describing the present invention and simplifying the description, rather than indicating or implying that the device or element referred to must have a specific orientation, be constructed and operated in a specific orientation, and thus should not be construed as a limitation to the present invention. In addition, the terms "first", "second", "third" are only used for descriptive purposes and cannot be construed as indicating or implying relative importance.
[0036] In the description of the present invention, it should also be noted that unless otherwise clearly specified and limited, the terms "installed", "connected", "connected" should be understood in a broad sense. For example, it can be a fixed connection, a detachable connection, or an integral connection; it can be a mechanical connection or an electrical connection; it can be directly connected or indirectly connected through an intermediate medium, and it can be the communication inside two elements. For those of ordinary skill in the art, the specific meanings of the above terms in the present invention can be understood according to specific situations.
[0037] Figure 1 It is a flowchart of a process cache management method based on the dotNet platform provided by an embodiment of the present invention. This method is executed by an electronic device. As Figure 1 shown, this method specifically includes:
[0038] S110. Perform the following preparation steps for the cache and data:
[0039] Step 1. Set the maximum limit parameter of the cache capacity. Among them, when the maximum limit hyperparameter is greater than or equal to 1, it means that the capacity upper limit is specified, and when the maximum limit parameter is less than 1, it means that the capacity upper limit is not specified. Optionally, ConcurrentDictionary is a thread-safe dictionary collection in.NET, and the maximum capacity parameter: int maxCapacity can be added based on the ConcurrentDictionary constructor.
[0040] If the value of maxCapacity is greater than 1, it means that the capacity upper limit parameter is specified. Then the number of elements is no greater than this quantity. Initially, the cache memory is pre-allocated to the maximum capacity and will not be expanded later. When the subsequent inserted data reaches the maximum capacity, data discarding starts.
[0041] If the value of maxCapacity is less than 1, it means that the capacity upper limit is not specified, so the capacity can grow infinitely, following the same implementation logic as before.
[0042] Step 2: Classify each piece of data. The data types include ordinary data, hot data, and blacklist data. And add a type field to each piece of data to represent the data type. Optionally, hot data and blacklist data can be combined with manual annotation or other more complex offline statistical methods to label hot data and blacklist data. The reason for having blacklist data is to prevent frequently accessed invalid data from breaking through the cache and bringing additional database access pressure.
[0043] After classification, load hot data and blacklist data into memory in advance. Optionally, when hot data or blacklist data is accessed for the first time, load it into memory and keep it unchanged thereafter. Optionally, when judging the data type, based on the ConcurrentDictionary constructor, add an interface parameter for obtaining data classification: ICacheItemClassGetter cachItemClassGetter. This interface has only one method: CacheItemClass GetItemClass(TValue value). Pass in the cache value object and return the data classification of the cache: hot data, blacklist data, or general data. The interface implementation requires that the TValue value should contain an information field for data classification so as to distinguish the data classification (of course, if the data classification field is not included, the interface implementation can directly return the general data classification, and thus there will be no data classification processing function).
[0044] Step 3: Create key-value pairs (key, value) for each piece of data based on the ConcurrentDictionary class. Among them, the key of each key-value pair is used to store the data serial number, and the value is used to store the corresponding data and data fields.
[0045] Then, create a bulk array in the cache according to the number of keys in each key-value pair. Each element in the bulk array is a linked list, and each node in each linked list is used to store a key-value pair.
[0046] Finally, according to each key value, calculate the storage position of each data in the said bulk array. Optionally, for any key-value pair, calculate the hash value HashCode based on the key (such as int32 type); take the modulus of the said hash value by the size of the said bulk array, and the remainder m is the storage position of the data corresponding to the said key-value pair in the said bulk array.
[0047] When the number of key-value pairs stored in the said bulk array exceeds a set multiple of the array capacity (such as array capacity * 2), it is necessary to expand the bulk array. First, lock the entire bulk array, calculate the new capacity and allocate a new bulk array; migrate the elements in the said bulk array to the said new bulk array, and release all the locks of the said bulk array.
[0048] Step 4: Add a last access time field to each piece of data in the cache. This field is used to record the time when the corresponding data was last accessed. Optionally, based on the ConcurrentDictionary class code, add a last access time field, long_lastAccessTime, to the element Node object to represent the time when the element was last queried.
[0049] The above steps 1 to 4 are a series of improvements to the cache and data based on the ConcurrentDictionary class in this embodiment, which are used to provide a basis for subsequent cache data query, query and deletion operations. Among them, there is no sequence for each preparation step, and they can be executed sequentially or in parallel, and this embodiment does not make specific restrictions.
[0050] S120: In response to a data query request, modify the last access time field of the data to be queried to the current time.
[0051] In a specific embodiment, in response to a data query request, first, determine the data type according to the type field of the data to be queried.
[0052] If the data to be queried is hot data and blacklist data, directly query in the cache and modify the last access time of the data to be queried to the current time. That is, first query in the hot data and blacklist data. If the data is found, directly return. Before returning the data, modify the last access time of the data to the current time.
[0053] If the data to be queried is ordinary data, first query it in the cache; if the data to be queried exists in the cache, modify the last access time of the data to be queried to the current time; if the data to be queried does not exist in the cache, query it in the database. After querying, initiate a cache data addition request, add the data to be queried to the cache, and modify the last access time field of the data to be queried to the current time. That is, ordinary data is loaded on demand. If the ordinary data is not in the cache, it is read from the database, and if the read is successful, it is added to the cache. Similarly, before returning the data, modify the last access time of the data to the current time.
[0054] Optionally, when looking up data in the cache, first find the bulk object in the bulk array according to the HashCode of the key, and then traverse the linked list in the bulk object to find the matching key-value pair according to the key.
[0055] S130. In response to the cache data addition request, insert the new data into the cache.
[0056] Optionally, when inserting the new data into the cache, the new data is a key-value pair (key, value). The element position of the new data in the bulk array can be calculated according to the key and value; then, add the new data key-value pair to the linked list at the element position.
[0057] S140. In the case where the cache capacity exceeds the maximum limit after insertion, traverse the inserted cache by partitions, and delete some data according to the last access time field of the data being traversed and the total number of data that has been traversed. Among them, the partition being traversed is locked, and other partitions can execute other threads.
[0058] If the capacity is full after the insertion operation (the number of stored keys is greater than the maximum capacity), continue to execute the asynchronous task to discard some data until the capacity meets the requirements or the entire array has been traversed. Optionally, the inserted bulk array can be partitioned, and the bulk array is traversed using the asynchronous task method. Only one data area is locked, and deletable elements are found in this data area until the number of elements meets the requirements. Specifically, this asynchronous task in this embodiment is triggered when adding elements, and the asynchronous task method ensures that only one is executed at a time. Optionally, the bulk array can have a maximum of 1023 partitions, which is equivalent to supporting a maximum of 1023 threads for concurrent access.
[0059] In a specific embodiment, partition the inserted cache and traverse each partition, and perform the following operations on each data in the current partition in turn:
[0060] Step 1. Obtain the data classification of the element. If it is hot data or blacklist data, skip it directly and proceed to process the next data; if it is neither hot data nor blacklist data, then proceed to Step 2. Optionally, the data classification can be obtained through CacheItemClass GetItemClass(TValue value).
[0061] Step 2. Calculate the interval between the last access time and the current time based on the last access time field of the current data. If the interval meets any of the following conditions, delete the current data from the cache:
[0062] Condition 1. The interval is greater than the first threshold;
[0063] Condition 2. The interval is greater than the second threshold, and the proportion of the total number of traversed data in the total number of cache data is greater than the first proportion;
[0064] Condition 3. The interval is greater than the third threshold, and the proportion of the total number of traversed data in the total number of cache data is greater than the second proportion; or
[0065] Condition 4. The interval is greater than the fourth threshold, and the proportion of the total number of traversed data in the total number of cache data is greater than the third proportion;
[0066] Among them, the first threshold > the second threshold > the third threshold > the fourth threshold, and the first proportion < the second proportion < the third proportion.
[0067] Exemplarily, if the time interval value is greater than or equal to 30 minutes (the first threshold), then directly delete the current data;
[0068] If the time interval value is greater than or equal to 20 minutes (the second threshold), and half (the first proportion) of the elements have been traversed, then delete the current data;
[0069] If the time interval value is greater than or equal to 10 minutes (the third threshold), and 3 / 4 (the second proportion) of the elements have been traversed, then delete the current data;
[0070] If the time interval value is greater than or equal to 5 minutes (the fourth threshold), and 9 / 10 (the third proportion) of the elements have been traversed, then delete the current data.
[0071] If none of the above situations occur, abandon the operation of deleting the element and proceed to process the next partition.
[0072] In addition, if the cache capacity is full, some data that has not been used for a long time can be deleted; when there is data update, the corresponding data needs to be deleted from the cache.
[0073] Furthermore, through experiments and simulations, this embodiment meets the design requirements in terms of capacity, and at the same time, the hit rate of stored data is increased by 20% compared to general solutions.
[0074] In summary, the embodiment of the present invention provides a process cache management method based on the dotNet platform. It is improved on the basis of the ConcurrentDictionary class code, and strategies such as data classification, multi-region synchronization lock control, capacity control, and dynamic deletion are added. It can effectively control the cache capacity, improve the hit rate of cached data, and implement a lightweight process cache management mechanism. Specifically, this method simplifies the algorithm complexity of eliminating and deleting elements; the operation of eliminating and deleting elements uses partition locks, which has a very limited impact on high concurrency; the amount of dynamically maintained data is minimized, and the performance impact on query operations is as small as possible (compared to algorithms such as LRU); it focuses on the lightweight and practicality of the method, and can ensure that the performance in more than 80% of scenarios is faster than the LRU algorithm; data classification is added to ensure that hot data can always be effectively cached; the operation of eliminating and deleting elements uses an asynchronous operation method, which does not affect the performance of the operation of adding elements.
[0075] Figure 2 The following is a schematic structural diagram of an electronic device provided by an embodiment of the present invention. As Figure 2 shown, the device includes a processor 60, a memory 61, an input device 62, and an output device 63; the number of processors 60 in the device can be one or more, Figure 2 taking one processor 60 as an example; the processor 60, memory 61, input device 62, and output device 63 in the device can be connected through a bus or other means, Figure 2 taking connection through a bus as an example.
[0076] The memory 61, as a computer-readable storage medium, can be used to store software programs, computer-executable programs, and modules, such as program instructions / modules corresponding to the process cache management method based on the dotNet platform in the embodiment of the present invention. The processor 60 executes various functional applications and data processing of the device by running the software programs, instructions, and modules stored in the memory 61, that is, implements the above-mentioned process cache management method based on the dotNet platform.
[0077] The memory 61 may mainly include a program storage area and a data storage area. Among them, the program storage area may store an operating system and application programs required for at least one function; the data storage area may store data created according to the use of the terminal, etc. In addition, the memory 61 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one magnetic disk storage device, a flash memory device, or other non-volatile solid-state storage devices. In some instances, the memory 61 may further include a memory remotely provided with respect to the processor 60, and these remote memories may be connected to the device through a network. Examples of the above-mentioned network include but are not limited to the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0078] The input device 62 may be used to receive input digital or character information, and generate key signal inputs related to the user settings and function controls of the device. The output device 63 may include a display device such as a display screen.
[0079] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and are not intended to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that they can still modify the technical solutions described in the foregoing embodiments, or perform equivalent replacements for some or all of the technical features; and these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the technical solutions of the embodiments of the present invention.
Claims
1. A process cache management method based on the dotNet platform, characterized in that Including: Add a last access time field to each piece of data in the cache; among them, based on the ConcurrentDictionary class, each piece of data corresponds to a key-value pair respectively, where the key of each key-value pair is used to store the data serial number, and the value is used to store the data; each piece of data in the cache is stored in the bulk array, and each element in the bulk array is a linked list, and each node in each linked list is used to store a key-value pair; In response to a data query request, modify the last access time field of the data to be queried to the current time; In response to a cache data addition request, calculate the element position of the new data in the bulk array according to the key-value pair of the new data; add the key-value pair of the new data to the linked list at the element position, and when the capacity of the bulk array exceeds the maximum limit after insertion, perform a partition traversal on the inserted bulk array, and delete data according to the last access time field of the data being traversed and the total number of data that has been traversed, where the partition being traversed is locked and other partitions can execute other threads; The deleting data includes: Perform the following operations on each piece of data in the current partition in sequence: S1-1: Calculate the interval between the last access time and the current time according to the last access time field of the current data; S1-2: If the interval meets any of the following conditions, delete the current data from the cache: The interval is greater than the first threshold; or The interval is greater than the second threshold, and the proportion of the total number of data that has been traversed in the total number of cache data is greater than the first proportion; or The interval is greater than the third threshold, and the proportion of the total number of data that has been traversed in the total number of cache data is greater than the second proportion; or The interval is greater than the fourth threshold, and the proportion of the total number of data that has been traversed in the total number of cache data is greater than the third proportion; Among them, the first threshold > the second threshold > the third threshold > the fourth threshold, and the first proportion < the second proportion < the third proportion.
2. The method according to claim 1, wherein Before adding a last access time field to each piece of data in the cache, it also includes: Add a type field to each piece of data, where the data types include hot data, blacklist data, and ordinary data; Pre-load hot data and blacklist data in the cache, and load other data as needed.
3. The method according to claim 2, wherein The step of modifying the last access time field of the data to be queried to the current time in response to a data query request includes: In response to a data query request, judge the type of the data to be queried according to the type field of the data to be queried; If the data to be queried is hot data or blacklist data, directly query it in the cache and modify the last access time of the data to be queried to the current time; If the data to be queried is ordinary data, first query it in the cache; if the data to be queried exists in the cache, modify the last access time of the data to be queried to the current time; if the data to be queried does not exist in the cache, query it in the database. After querying, initiate a cache data addition request, add the data to be queried to the cache, and modify the last access time field of the data to be queried to the current time.
4. The method according to claim 1, wherein Before adding the key-value pairs of the new data to the linked list at the element position, it further includes: Create the bulk array in the cache according to the number of keys in each key-value pair; Calculate the storage positions of each data in the bulk array according to each key value.
5. The method according to claim 4, characterized in that, The calculating the storage positions of each data in the bulk array according to each key-value pair includes: Calculate the hash value of the key in any key-value pair; Take the modulus of the hash value by the size of the bulk array, and the remainder m is the storage position of the data corresponding to the key-value pair in the bulk array.
6. The method according to claim 4, characterized in that, It further includes: When the number of key-value pairs stored in the bulk array exceeds a set multiple of the array capacity, lock the bulk array, calculate a new capacity, and allocate a new bulk array; Migrate the elements in the bulk array to the new bulk array and release the lock of the bulk array.
7. The method according to claim 1, characterized in that, Before traversing the partition of the bulk array after insertion when the capacity of the bulk array exceeds the maximum limit, it further includes: Set the maximum limit parameter of the cache capacity, where the maximum limit parameter greater than or equal to 1 indicates that the capacity upper limit is specified, and the maximum limit parameter less than 1 indicates that the capacity upper limit is not specified.
8. An electronic device, characterized in that, It includes: One or more processors; A memory for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the process cache management method based on the dotNet platform according to any one of claims 1-7.
Citation Information
Patent Citations
Data processing method and device, equipment and medium
CN116257550A
Processing instructions to invalidate cached resultant data in a database system
US20240362167A1