DotNet platform-based process cache management method
By adding the last access time field and partition traversal deletion strategy to the process cache management method on the .NET platform, the communication overhead, memory overhead and single cache failure strategy of the in-process cache scheme in the .NET environment is solved, and efficient cache management and performance improvement is achieved.
Patent Information
- Application Number
- CN202411612968.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-13
- Publication Date
- 2025-02-28
- Estimated Expiration
- 2044-11-13
AI Technical Summary
The existing technology in-process caching schemes in the .NET environment have problems such as communication overhead, object serialization and deserialization overhead, reliability affected by network jitter, large memory overhead, LRU algorithms are susceptible to short-term cold data, LFU algorithms may lead to long-term retention of historical hot data, and single cache failure strategy.
A process cache management method based on the dotNet platform is provided. By adding the last access time field to the cache data, updating the field in response to the data query request, and partition traversal when the cache capacity exceeds the maximum limit when the cache capacity exceeds the maximum limit and deleting part of the data based on the last access time field and the total number of data. Multi-zone synchronization lock control and asynchronous operation are adopted.
Effectively control the cache capacity, improve the hit rate of cached data, simplify the algorithm complexity of eliminating and deleting elements, reduce the impact of high concurrency on performance, and ensure that the performance of more than 80% of scenarios is faster than that of the LRU algorithm.
Smart Images

Figure CN119537430A_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 a dotNet platform. Background Art
[0002] Currently in the .NET environment, the in-process caching solutions mainly include the following:
[0003] 1.MemoryCache: It is a lightweight memory cache mechanism in .NET Core, suitable for storing short-term valid data. It can be used in ASP.NET Core applications through dependency injection.
[0004] 2.CacheManager: is an open source .NET cache abstraction layer that supports multiple cache providers such as MemoryCache, Redis, Memcached, etc., and provides advanced features such as cache synchronization, concurrent updates, and event handling.
[0005] 3. EasyCaching: is 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: It is a .NET framework used to build an efficient cache system, supporting multiple cache strategies such as memory cache and distributed cache.
[0007] 5.Microsoft.Extensions.Caching.Memor: This is the memory cache implementation officially provided by Microsoft, mainly used in ASP.NET Core projects.
[0008] 6. System.Runtime.Caching: This is a namespace introduced in .NET Framework 4.0, which provides a simple caching mechanism suitable for stand-alone applications.
[0009] 7.Microsoft Caching: A cache library officially provided by Microsoft that supports the implementation of memory cache and distributed cache, such as SQL Server and Redis distributed cache.
[0010] Each cache solution has its own specific application scenarios and advantages and disadvantages, but in general, they are all a huge cache management system with the following problems:
[0011] Problems with out-of-process caches such as Redis:
[0012] 2) There are communication expenses;
[0013] 2) Object serialization and deserialization overhead;
[0014] 3) Reliability, which is easily affected by factors such as network jitter;
[0015] Problems with ordinary memory cache:
[0016] 1) Memory overhead: Generally, the memory capacity is not effectively controlled, which can easily cause the cache system to occupy 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 eliminated prematurely. In addition, periodic data access problems may also cause unnecessary overhead.
[0018] 3) Disadvantages of the LFU (Least Frequently Used) algorithm: The LFU algorithm focuses too much on the cumulative number of accesses to data, which may cause some "historical hot 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 access, which may bring performance overhead.
[0019] 3) Single cache invalidation strategy: A reasonable cache invalidation strategy is crucial to improving cache hit rate. You can set appropriate lifetime and activity expiration strategies to ensure the freshness and relevance of cached data. Just one strategy cannot always perfectly solve all problems. Summary of the invention
[0020] The embodiment of the present invention provides a process cache management method based on the dotNet platform, implements a lightweight process cache management method, and can solve at least one of the above problems.
[0021] In a first aspect, an embodiment of the present invention provides a process cache management method based on a dotNet platform, comprising:
[0022] Add the last access time field to each 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 request to add new cache data, the new data is inserted into the cache, and if the cache capacity exceeds the maximum limit after the insertion, the cache after the insertion is partitioned and some data is deleted according to the last access time field of the data being traversed and the total number of data that has been traversed;
[0025] 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, the electronic device comprising:
[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, which is improved on the basis of the ConcurrentDictionary class code, and adds multi-zone synchronous lock control, capacity control, dynamic deletion and other strategy methods, which can effectively control the cache capacity and improve the hit rate of cache data, thereby realizing a lightweight process cache management mechanism. Specifically, the method simplifies the algorithm complexity of eliminating and deleting elements; the elimination and deletion of elements adopts partition lock operation, which has very limited impact on high concurrency; the data dynamically maintained is as small as possible, and the performance impact on query operations is as small as possible (relative to LRU and other algorithms); the method focuses on lightweight and practicality, and can ensure that the performance of more than 80% of the scenarios is faster than the LRU algorithm; the elimination and deletion of elements adopts asynchronous operation mode, which does not affect the performance of adding elements. BRIEF DESCRIPTION OF THE DRAWINGS
[0031] In order to more clearly illustrate the specific implementation methods of the present invention or the technical solutions in the prior art, the drawings required for use in the specific implementation methods or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some implementation methods of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0032] Figure 1 It is a flow chart of a process cache management method based on the dotNet platform provided by an embodiment of the present invention;
[0033] Figure 2 A schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0034] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be described clearly and completely below. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work belong to the scope of protection of the present invention.
[0035] In the description of the present invention, it should be noted that the terms "center", "upper", "lower", "left", "right", "vertical", "horizontal", "inner", "outer", etc., indicating the orientation or positional relationship, are based on the orientation or positional relationship shown in the drawings, and are 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 therefore cannot be understood as limiting the present invention. In addition, the terms "first", "second", and "third" are used for descriptive purposes only, and cannot be understood as indicating or implying relative importance.
[0036] In the description of the present invention, it is also necessary to explain that, unless otherwise clearly specified and limited, the terms "installed", "connected", and "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 a direct connection, or it can be indirectly connected through an intermediate medium, or it can be the internal communication of two components. For ordinary technicians in this field, the specific meanings of the above terms in the present invention can be understood according to specific circumstances.
[0037] Figure 1 1 is a flowchart of a process cache management method based on the dotNet platform provided by an embodiment of the present invention. The method is executed by an electronic device. Figure 1 As shown, the method specifically includes:
[0038] S110, perform the following preparation steps for cache and data:
[0039] Step 1: Set the maximum limit parameter of the cache capacity, wherein the maximum limit parameter is greater than or equal to 1, indicating that the capacity upper limit is specified, and the maximum limit parameter is less than 1, indicating that the capacity upper limit is not specified. Optionally, ConcurrentDictionary is a thread-safe dictionary collection in .NET, and the maximum capacity parameter can be added based on the ConcurrentDictionary constructor: int maxCapacity.
[0040] If the maxCapacity value is greater than 1, it means that the capacity upper limit parameter is specified, and the sum of the number of elements is not greater than this number. At the beginning, the cache memory is pre-allocated to the maximum capacity and no further expansion is done. When the subsequent inserted data reaches the maximum capacity, data is discarded.
[0041] If the maxCapacity value is less than 1, it means that there is no specified capacity upper limit, and the capacity can grow indefinitely, which is the same as the original implementation logic.
[0042] Step 2: Classify each data type, including common data, hot data, and blacklist data; and add a type field for each data type to indicate the data type. Optionally, you can combine manual labeling of hot data and blacklist data, or use other more complex offline statistical methods to label hot data and blacklist data. The reason for blacklist data is to prevent frequently accessed invalid data from penetrating the cache and bringing additional database access pressure.
[0043] After the classification is completed, the hot data and blacklist data are loaded into the memory in advance. Optionally, when the hot data or blacklist data is accessed for the first time, it is loaded into the memory and remains unchanged afterwards. Optionally, when judging the data type, based on the ConcurrentDictionary constructor, you can add a data classification acquisition interface parameter: ICacheItemClassGettercachItemClassGetter. This interface has only one method: CacheItemClass GetItemClass(TValuevalue). Pass in the cache value object and return the cached data classification: hot data, blacklist data or general data. The interface implementation requires that the TValue value should contain the data classification information field so that the data classification can be distinguished (of course, if the data classification field is not included, the interface implementation can directly return the general data classification, so that there is no data classification processing function).
[0044] Step 3: Create key-value pairs (key, value) for each data based on the ConcurrentDictionary class, where the key of each key-value pair is used to store the data sequence number, and the value is used to store the corresponding data and data field.
[0045] Then, according to the number of keys in each key-value pair, a bulk array is created in the cache, 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, the storage location of each data in the bulk array is calculated based on each key value. Optionally, for any key-value pair, a hash value HashCode (such as int32 type) is calculated based on the key; the hash value is modulo the size of the bulk array, and the remainder m is the storage location of the data corresponding to the key-value pair in the bulk array.
[0047] When the number of key-value pairs stored in the bulk array exceeds the set multiple of the array capacity (for example, array capacity * 2), the bulk array needs to be expanded. First, the entire bulk array is locked, and the new capacity is calculated to allocate a new bulk array; the elements in the bulk array are migrated to the new bulk array, and all locks of the bulk array are released.
[0048] Step 4: Add a last access time field to each 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, you can add a last access time field, long_lastAccessTime, to the element Node object to indicate the time when the element was last queried.
[0049] The above steps 1 to 4 are a series of improvements to 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 delete operations. Among them, there is no order between the preparation steps, which can be executed sequentially or in parallel, and this embodiment does not make specific restrictions.
[0050] S120 . In response to the data query request, modify the last access time field of the queried data to the current time.
[0051] In a specific implementation, in response to a data query request, first, the data type is determined based on the type field of the queried data.
[0052] If the queried data is hot data or blacklist data, query directly in the cache, and modify the last access time of the queried data to the current time. That is, query in the hot data and blacklist data first, and if the data is found, return directly, and modify the last access time of the data to the current time before returning the data.
[0053] If the queried data is ordinary data, query it in the cache first; if the queried data exists in the cache, modify the last access time of the queried data to the current time; if the queried data does not exist in the cache, query it in the database, and after the query, initiate a request to add cache data, add the queried data to the cache, and modify the last access time field of the queried data to the current time. That is, ordinary data is loaded on demand. If the ordinary data does not exist in the cache, it is read from the database, and added to the cache if the read is successful. Similarly, before returning the data, modify the last access time of the data to the current time.
[0054] Optionally, when searching for data in the cache, first find the bulk object in the bulk array based on the HashCode of the key, then traverse the linked list in the bulk object to find the matching key-value pair based on the key.
[0055] S130: In response to the cache data addition request, insert the new data into the cache.
[0056] Optionally, when inserting new data in the cache, the new data is a key-value pair (key, value), and the element position of the new data in the bulk array can be calculated based on the key and value; then, the new data key-value pair is added to the linked list at the element position.
[0057] S140: When the capacity of the cache after insertion exceeds the maximum limit, partition traverse the cache after insertion, and delete part of the data according to the last access time field of the data being traversed and the total number of data that has been traversed. 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 and discard some data until the capacity reaches the requirement or the entire array is traversed. Optionally, the bulk array after insertion can be partitioned, and the asynchronous task method can be used to traverse the bulk array, locking only one data area at a time, and searching for elements that can be deleted in this data area until the number of elements reaches the requirement. In particular, the asynchronous task in this embodiment is triggered when an element is added, 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 concurrently accessing.
[0059] In a specific implementation, the inserted cache is partitioned, and each partition is traversed, and the following operations are performed in sequence for each data in the current partition:
[0060] Step 1: Get the data classification of the element. If it is hot data or blacklist data, skip it and proceed to process the next data. If it is not hot data or blacklist data, proceed to step 2. Optionally, you can get the data classification 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 satisfies any of the following conditions, delete the current data from the cache:
[0062] Condition 1: the interval is greater than a first threshold;
[0063] Condition 2: the interval is greater than the second threshold, and the total number of traversed data accounts for a greater proportion than the first proportion in the total number of cached data;
[0064] Condition three: the interval is greater than the third threshold, and the total number of traversed data accounts for a greater proportion of the total number of cached data than the second proportion; or
[0065] Condition 4: the interval is greater than the fourth threshold, and the total number of traversed data accounts for a greater proportion than the third proportion in the total number of cached data;
[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), the current data is directly deleted;
[0068] If the time interval is greater than or equal to 20 minutes (the second threshold), and half (the first percentage) of the elements have been traversed, the current data is deleted;
[0069] If the time interval is greater than or equal to 10 minutes (the third threshold), and 3 / 4 (the second proportion) of the elements have been traversed, 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, the current data is deleted.
[0071] If none of the above situations occurs, abandon the element deletion operation 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 with the general solution.
[0074] In summary, the embodiment of the present invention provides a process cache management method based on the dotNet platform, which is improved on the basis of the ConcurrentDictionary class code, and adds data classification, multi-zone synchronous lock control, capacity control, dynamic deletion and other strategic methods, which can effectively control the cache capacity and improve the hit rate of cache data, and realize a lightweight process cache management mechanism. Specifically, the method simplifies the algorithm complexity of eliminating and deleting elements; the elimination and deletion of elements adopts partition lock operation, which has a very limited impact on high concurrency; the data dynamically maintained is as small as possible, and the performance impact on query operations is as small as possible (relative to LRU and other algorithms); the method focuses on lightweight and practicality, and can ensure that the performance of more than 80% of the scenarios is faster than the LRU algorithm; increase data classification to ensure that hot data can always be effectively cached; the elimination and deletion of elements adopts asynchronous operation mode, which does not affect the performance of adding elements.
[0075] Figure 2 A schematic diagram of the structure of an electronic device provided by an embodiment of the present invention is shown in FIG. Figure 2 As 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 A processor 60 is taken as an example; the processor 60, the memory 61, the input device 62 and the output device 63 in the device can be connected by a bus or other means. Figure 2 The example of connecting through bus is taken in the following.
[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, realizing 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, wherein the program storage area may store an operating system and at least one application required for a 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 disk storage device, a flash memory device, or other non-volatile solid-state storage device. In some instances, the memory 61 may further include a memory remotely arranged relative to the processor 60, and these remote memories may be connected to the device via 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 input related to user settings and function control 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, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some or all of the technical features therein by equivalents. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the technical solutions of the embodiments of the present invention.
Claims
1. A process cache management method based on dotNet platform, characterized in that: include: Add the last access time field to each data in the cache; In response to a data query request, modifying the last access time field of the queried data to the current time; In response to a request to add new cache data, the new data is inserted into the cache, and if the cache capacity exceeds the maximum limit after the insertion, the cache after the insertion is partitioned and some data is deleted according to the last access time field of the data being traversed and the total number of data that has been traversed; The partition being traversed is locked, and other partitions can execute other threads.
2. The method according to claim 1, characterized in that Before adding the last access time field to each data in the cache, the method further includes: Add a type field for each data, where the data type includes hot data, blacklist data and common data; Hotspot data and blacklist data are loaded in the cache in advance, and other data are loaded on demand.
3. The method according to claim 2, characterized in that In response to the data query request, modifying the last access time field of the queried data to the current time includes: In response to the data query request, determining the type of the queried data according to the type field of the queried data; If the queried data is hot data or blacklist data, directly query in the cache, and modify the last access time of the queried data to the current time; If the queried data is ordinary data, first query in the cache; if the queried data exists in the cache, modify the last access time of the queried data to the current time; if the queried data does not exist in the cache, query in the database, and after the query, initiate a request to add cache data, add the queried data to the cache, and modify the last access time field of the queried data to the current time.
4. The method according to claim 1, characterized in that Before inserting the newly added data into the cache, the method further includes: Create key-value pairs for each data based on the ConcurrentDictionary class, where the key of each key-value pair is used to store the data sequence number, and the value is used to store the data; According to the number of keys in each key-value pair, a bulk array is created in the cache, 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; According to each key value, the storage position of each data in the bulk array is calculated.
5. The method according to claim 4, characterized in that Calculating the storage location 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; The hash value is modulo 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 Also includes: When the number of key-value pairs stored in the bulk array exceeds a set multiple of the array capacity, the bulk array is locked, and a new capacity is calculated to 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 Based on the ConcurrentDictionary class, each data corresponds to a key-value pair, where the key of each key-value pair is used to store the data sequence number, and the value is used to store the data; each data in the cache is stored in a bulk array, 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; Accordingly, inserting the new data into the cache includes: Calculate the element position of the newly added data in the bulk array according to the key-value pair of the newly added data; Add the key-value pair of the newly added data to the linked list at the element position.
8. The method according to claim 1, characterized in that In the case where the capacity of the cache after insertion exceeds the maximum limit, before partition traversing the cache after insertion, the method further includes: A maximum limit parameter for the cache capacity is set, wherein the maximum limit parameter is greater than or equal to 1, indicating that an upper limit on the capacity is specified, and the maximum limit parameter is less than 1, indicating that an upper limit on the capacity is not specified.
9. The method according to claim 1, characterized in that: The partitioned traversal of the inserted cache and the deletion of part of the data according to the last access time field of the data being traversed and the total number of data that has been traversed include: The inserted cache is partitioned, and each partition is traversed. The following operations are performed on each data in the current partition in sequence: S1-1, according to the last access time field of the current data, calculate the interval between the last access time and the current time; S1-2. If the interval satisfies any one of the following conditions, the current data is deleted from the cache: The interval is greater than a first threshold; The interval is greater than a second threshold, and the total amount of traversed data accounts for a greater proportion than the first proportion in the total amount of cached data; The interval is greater than the third threshold, and the total amount of traversed data accounts for a greater proportion than the second proportion in the total amount of cached data; or The interval is greater than a fourth threshold, and the total amount of traversed data accounts for a greater proportion than the third proportion in the total amount of cached data; Among them, the first threshold>the second threshold>the third threshold>the fourth threshold, and the first proportion<the second proportion<the third proportion.
10. An electronic device, characterized in that: include: 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 as described in any one of claims 1-8.
Citation Information
Patent Citations
Data processing method and device, equipment and medium
CN116257550A
Data processing method and device, electronic equipment and medium
CN116795878A
Processing instructions to invalidate cached resultant data in a database system
US20240362167A1