Method and device for implementing local cache using off-heap memory

By implementing local cache in off-heap memory, the problems of cached data in the prior art resulting in high YGC frequency and Full GC pauses are solved, and the effect of reducing GC impact and improving system performance is achieved.

CN112631504BActive Publication Date: 2025-02-21BEIJING JINGDONG SHANGKE INFORMATION TECH CO LTD +1
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN201910906744.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2019-09-24
Publication Date
2025-02-21
Estimated Expiration
2039-09-24

AI Technical Summary

Technical Problem

In the prior art, the larger the cache data, the more likely it is to cause the YGC frequency to become higher, the longer the time to trigger Full GC, causing the overall application to pause, seriously affecting the normal response of the system, and possibly causing memory overflow.

Method used

By using off-heap memory to realize local cache, in response to data cache requests, the off-heap cache container is obtained from the off-heap cache container list, the data to be cached is saved in the off-heap cache container, and the data identification information and cache container are marked. Identification information is stored in the off-heap cache reference map.

Benefits of technology

It reduces the impact of cache on GC and service response, reduces the frequency of system GC and Full GC, improves system performance, and effectively avoids the risk of memory overflow.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN112631504B_ABST
    Figure CN112631504B_ABST
Patent Text Reader

Abstract

The present invention discloses a method and device for realizing local caching using off-heap memory, and relates to the field of computer technology. A specific implementation of the method includes: in response to a data cache request, obtaining an off-heap cache container from an off-heap cache container list; saving the data to be cached in the data cache request into the off-heap cache container; storing the identification information of the data to be cached and the identification information of the off-heap cache container into an off-heap cache reference mapping, so as to realize local caching using off-heap memory. This implementation method realizes off-heap storage of local cache using off-heap memory, which can reduce the impact of cache on GC and business response, and improve system performance.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a method and device for implementing local caching by using off-heap memory. Background Art

[0002] Local cache is relative to distributed cache. Generally speaking, it refers to caching high-frequency and less-changing data on local storage media, such as local memory, in high-concurrency and massive Internet request scenarios, to achieve the purpose of quickly responding to user requests and reducing the pressure on the database. In the JAVA local cache implementation, cached data is directly stored in the heap.

[0003] In the process of implementing the present invention, the inventors found that there are at least the following problems in the prior art:

[0004] (1) The larger the cached data is and the longer the cache time is, the more likely it is that the YGC (Garbage Collection) frequency (GC) of the new generation heap will increase and the time will increase. When the cached data reaches a certain threshold, Full GC (GC of all heaps) will be triggered, causing the entire application to pause.

[0005] (2) It seriously affects the normal response of the system. The response time of the request is even longer than the time it takes to directly request the database to obtain data and then return;

[0006] (3) When the cached data is too large and cannot be recycled, it may cause memory overflow. Summary of the invention

[0007] In view of this, an embodiment of the present invention provides a method and device for implementing local caching using off-heap memory, which can reduce the impact of cache on GC and business response and improve system performance.

[0008] To achieve the above object, according to one aspect of an embodiment of the present invention, a method for implementing local caching using off-heap memory is provided, comprising:

[0009] In response to the data cache request, obtaining an off-heap cache container from the off-heap cache container list;

[0010] Saving the data to be cached in the data cache request into the off-heap cache container;

[0011] The identification information of the data to be cached and the identification information of the off-heap cache container are stored in an off-heap cache reference map to implement local caching using off-heap memory.

[0012] Optionally, the method of the embodiment of the present invention further includes:

[0013] Monitor the average size of all cached data in the off-heap memory and the total number of writes to all off-heap cache containers to determine a fluctuation value of the off-heap memory; when the fluctuation value meets a preset fluctuation condition, adjust the number and size of the off-heap cache containers in the off-heap memory.

[0014] Optionally, the fluctuation value of the off-heap memory is determined according to the following formula:

[0015] C=(Am(1)-Am(2))×logM(Count)

[0016] In the formula, C represents the fluctuation value of the off-heap memory; Am(1) represents the average size of all cached data in the off-heap memory in the current period; Am(2) represents the average size of all cached data in the off-heap memory in the period before the current period; M represents a positive number; Count represents the total number of writes to all off-heap cache containers in the off-heap memory in the current period.

[0017] Optionally, the method of the embodiment of the present invention further includes: determining whether the data stored in the off-heap cache container meets a preset cache condition; if not, updating the cache data stored in the off-heap container.

[0018] Optionally, when reading data,

[0019] Determine whether there is identification information of the data to be read in the off-heap cache according to the off-heap cache reference mapping;

[0020] If so, determining the off-heap container corresponding to the identification information in the off-heap memory according to the off-heap cache reference mapping, and obtaining the cache data corresponding to the identification information from the off-heap container;

[0021] If not, data corresponding to the identification information is obtained from the database, the data corresponding to the identification information is saved in the off-heap memory, and the off-heap cache reference mapping is updated.

[0022] Optionally, before the step of obtaining the cached data corresponding to the identification information from the off-heap container, it also includes: determining that the cached data satisfies a preset reading condition; and when the cached data does not satisfy the preset reading condition, obtaining the data corresponding to the identification information from the database, and saving the data corresponding to the identification information to the off-heap memory.

[0023] According to a second aspect of an embodiment of the present invention, there is provided a device for implementing local caching using off-heap memory, comprising:

[0024] A response module, in response to a data cache request, obtains an off-heap cache container from an off-heap cache container list;

[0025] A saving module saves the to-be-cached data in the data cache request into the off-heap cache container;

[0026] The mapping module stores the identification information of the data to be cached and the identification information of the off-heap cache container into the off-heap cache reference mapping to realize local caching using off-heap memory.

[0027] Optionally, the device of an embodiment of the present invention also includes a monitoring module, which is used to: monitor the average size of all cached data in the off-heap memory and the total number of writes to all off-heap cache containers to determine the fluctuation value of the off-heap memory; when the fluctuation value meets a preset fluctuation condition, adjust the number and size of the off-heap cache containers in the off-heap memory.

[0028] Optionally, the fluctuation value of the off-heap memory is determined according to the following formula:

[0029] C=(Am(1)-Am(2))×logM(Count)

[0030] In the formula, C represents the fluctuation value of the off-heap memory; Am(1) represents the average size of all cached data in the off-heap memory in the current period; Am(2) represents the average size of all cached data in the off-heap memory in the period before the current period; M represents a positive number; Count represents the total number of writes to all off-heap cache containers in the off-heap memory in the current period.

[0031] Optionally, the device of the embodiment of the present invention further includes an update module, which is used to: determine whether the data stored in the off-heap cache container meets a preset cache condition; if not, update the cache data stored in the off-heap container.

[0032] Optionally, the device of the embodiment of the present invention further includes a reading module, which is used to: when reading data, determine whether identification information of the data to be read exists in the off-heap cache according to the off-heap cache reference mapping;

[0033] If so, determining the off-heap container corresponding to the identification information in the off-heap memory according to the off-heap cache reference mapping, and obtaining the cache data corresponding to the identification information from the off-heap container;

[0034] If not, data corresponding to the identification information is obtained from the database, the data corresponding to the identification information is saved in the off-heap memory, and the off-heap cache reference mapping is updated.

[0035] Optionally, the reading module is also used to: before the step of obtaining the cached data corresponding to the identification information from the off-heap container, determine that the cached data meets a preset reading condition; and when the cached data does not meet the preset reading condition, obtain the data corresponding to the identification information from the database, and save the data corresponding to the identification information to the off-heap memory.

[0036] According to a third aspect of an embodiment of the present invention, there is provided an electronic device for implementing local cache using off-heap memory, including:

[0037] one or more processors;

[0038] a storage device for storing one or more programs,

[0039] When the one or more programs are executed by the one or more processors, the one or more processors implement the method provided in the first aspect of the embodiment of the present invention.

[0040] According to a fourth aspect of an embodiment of the present invention, a computer-readable medium is provided, on which a computer program is stored, and when the program is executed by a processor, the method provided by the first aspect of the embodiment of the present invention is implemented.

[0041] An embodiment of the above invention has the following advantages or beneficial effects: using off-heap memory to implement local cache can reduce system GC and Full GC, reduce the impact of cache on GC and business response, and improve system performance.

[0042] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with the specific implementation manner. BRIEF DESCRIPTION OF THE DRAWINGS

[0043] The accompanying drawings are used to better understand the present invention and do not constitute an improper limitation of the present invention.

[0044] Figure 1 It is a schematic diagram of the main process of a method for implementing local caching using off-heap memory according to an embodiment of the present invention;

[0045] Figure 2 is a schematic diagram of the principle of a method for implementing local caching using off-heap memory according to an embodiment of the present invention;

[0046] Figure 3 is a schematic diagram of the principle of reading data in a method for implementing local caching by using off-heap memory according to an embodiment of the present invention;

[0047] Figure 4 It is a schematic diagram of the principle of a method for implementing local caching by using off-heap memory in an optional embodiment of the present invention;

[0048] Figure 5 is a schematic diagram of main modules of a device for implementing local caching using off-heap memory according to an embodiment of the present invention;

[0049] Figure 6 is an exemplary system architecture diagram to which embodiments of the present invention may be applied;

[0050] Figure 7 It is a schematic diagram of the structure of a computer system of a terminal device or a server suitable for implementing an embodiment of the present invention. DETAILED DESCRIPTION

[0051] The following is a description of exemplary embodiments of the present invention in conjunction with the accompanying drawings, including various details of the embodiments of the present invention to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for clarity and conciseness, the description of well-known functions and structures is omitted in the following description.

[0052] According to one aspect of an embodiment of the present invention, a method for implementing local caching by using off-heap memory is provided.

[0053] The memory includes on-heap memory and off-heap memory. The present invention allocates and stores the local cache in the off-heap memory. Figure 2 Schematic diagram of the principle of a method for implementing local caching using off-heap memory according to an embodiment of the present invention. Figure 2 As shown, the present invention stores the off-heap cache reference mapping in the off-heap cache and stores the cached data in the off-heap cache. When the system is initialized, the configuration information is first read. When the system starts, the number and size (CacheEntityNum) of the off-heap cache container ByteBuffer to be created are calculated according to the configuration information.

[0054] For example, the user configures the upper limit of the single cache size (MaxSingleCacheSize) and the upper limit of the number of caches (MaxCacheEntityNum) in the configuration information. When the system starts, the number and size of the off-heap memory ByteBuffers to be created are calculated based on the configuration information and the JVM startup off-heap space limit parameter (MaxDirectMemorySize). The calculation formula is as follows:

[0055] SingleCacheSize = (MaxSingleCacheSize) is preferred, otherwise the default value is (1024×10)

[0056] CacheEntityNum = (MaxCacheEntityNum) is preferred, otherwise the default value is (1000) Based on the above formula, the ByteBuffer initialization method can be called: ByteBuffer.allocateDirect(SingleCacheSize) to put the initialized off-heap cache container ByteBuffer into the off-heap cache container list List <bytebuffer>; Initialize an off-heap cache reference map LinkedHashMap<String,ByteBuffer> . The mapping between the identification information of the data stored in LinkedHashMap and the identification information of the off-heap cache container storing the data. Generally, LinkedHashMap is stored in the form of key-value pairs, where the key represents the identification information of the data and the value represents the identification information of the off-heap cache container storing the data. String is a Java generic declaration to indicate that the key type of LinkedHashMap is String.

[0057] The off-heap space limit parameter (MaxDirectMemorySize) provides a capacity limit for off-heap memory operations. By setting this parameter before the system starts, you can prevent the system memory from being over-consumed and prevent the Java process from being terminated by the operating system.

[0058] The off-heap memory is managed and operated by the operating system, and its recycling is not controlled by the JVM (short for Java Virtual Machine), so the off-heap memory has the risk of memory leakage. The present invention uses a method of fixing the size and number of off-heap cache containers to control the risk of memory overflow.

[0059] Figure 1 is a schematic diagram of the main process of a method for implementing local caching using off-heap memory according to an embodiment of the present invention. Figure 1 As shown, including:

[0060] Step S101: Obtain an off-heap cache container from an off-heap cache container list;

[0061] Step S102: Save the data to be cached in the data cache request into the off-heap cache container;

[0062] Step S103: Store the identification information of the data to be cached and the identification information of the off-heap cache container into an off-heap cache reference mapping to implement local caching using off-heap memory.

[0063] The present invention utilizes off-heap memory to implement local caching, which can reduce system GC and Full GC, reduce the impact of cache on GC and business response, and improve system performance.

[0064] The size and number of off-heap cache containers are calculated according to the allocatable memory size. For example, in an 8G operating environment, the JVM heap occupies 2G, the system occupies 2G, and 1G is left for other applications, leaving 3G for off-heap memory. If the average value of the data size stored in the local cache (covering the most data types) is 1K, then 3G / 1K=3145728 containers can be fixed. Optionally, the method of an embodiment of the present invention also includes: monitoring the average size of all cached data in the off-heap memory and the total number of writes to all off-heap cache containers to determine the fluctuation value of the off-heap memory; when the fluctuation value meets the preset fluctuation condition, adjusting the number and size of off-heap cache containers in the off-heap memory. By dynamically adjusting the number and size of off-heap cache containers in the off-heap memory, the system performance can be further improved.

[0065] The fluctuation value is an indicator of the fluctuation of the storage load in the buffer container outside the reactor. Its specific calculation method can be selectively determined according to the actual situation. For example, the difference between the average size of all cached data in the off-heap memory in the current period and the previous period is used as the fluctuation value. When the fluctuation value is positive, the apparent stored data size becomes larger; when the fluctuation value is negative, the apparent stored data size becomes smaller. Optionally, the fluctuation value of the off-heap memory is determined according to the following formula:

[0066] C=(Am(1)-Am(2))×logM(Count)

[0067] In the formula, C represents the fluctuation value of the off-heap memory; Am(1) represents the average size of all cached data in the off-heap memory in the current period; Am(2) represents the average size of all cached data in the off-heap memory in the period before the current period; M represents a positive number, and its value can be selectively determined, such as 10, 8, etc.; Count represents the total number of writes to all off-heap cache containers in the off-heap memory in the current period.

[0068] The length of the time period can be selectively set according to the actual situation. For example, a shorter time period, such as 5 minutes or 3 minutes, is selected to pay attention to the sudden growth of data and deal with emergencies. Another example is to select a longer time period, such as 1 hour or 5 hours, to pay attention to the change in data volume within a longer time range (such as a day or a week) to improve the utilization rate of cached data. In an optional embodiment, time periods of multiple dimensions can be selected, and the fluctuation values ​​corresponding to the time periods of each dimension can be calculated separately, so as to understand the change in data volume in multiple dimensions and improve the utilization rate of cached data.

[0069] The adjustment logic can be selectively determined according to the actual situation. For example, when the fluctuation value of the off-heap memory and the average size of all cached data in the off-heap memory are greater than the preset threshold, the size of the off-heap cache container is increased; when the fluctuation value of the off-heap memory is greater than the preset fluctuation threshold and the average size of all cached data in the off-heap memory is less than the preset size threshold, the size of the off-heap cache container is reduced. The number of off-heap cache containers is determined according to the off-heap space limit parameter and the size of the off-heap cache container. The number of off-heap cache containers = off-heap space limit / size of the off-heap cache container (returns an integer value).

[0070] For example, using a scheduled periodic task, statistics are collected every 5 minutes (pay attention to sudden growth of data and deal with emergencies), every hour (pay attention to changes in data volume within a day and improve data utilization), and every day (pay attention to changes in data based on days and improve data utilization). The fluctuation value of off-heap memory is calculated from three dimensions, and based on the comparison between the fluctuation value and the threshold, it is decided whether to dynamically adjust the cache. The values ​​of each indicator are calculated according to the following formula:

[0071] Available off-heap cache size Total() = system free memory - XG (X represents a constant, indicating that dynamic adjustment is enabled only when the free memory is greater than XG, otherwise the initial configuration is used to avoid memory overflow caused by dynamic adjustment);

[0072] The average size of cached data in 5 minutes, Am(m) = the sum of the data sizes in all off-heap cache containers within 5 minutes / the number of used off-heap cache containers;

[0073] Average size of cached data in one hour Ah(h) = sum of average size of cached data in each 5-minute period in one hour / number of cache statistics in each 5-minute period in one hour;

[0074] Average data size per day Ad(d) = sum of average cache data size per hour within one day / number of hourly statistics within one day;

[0075] Count(5minute) represents the number of cache writes within 5 minutes, Count(1hour) represents the number of cache writes within 1 hour, and Count(1day) represents the number of cache writes within a day;

[0076] 5-minute cache fluctuation value Cm = (Am(m)-Am(m-5))×log10(Count(5minute));

[0077] Cache fluctuation value within 1 hour Ch = (Ah(h)-Ah(h-1))×log100(Count(1hour));

[0078] Cache fluctuation value within one day Cd = (Ad(d)-Ad(d-1))×log1000(Count(1day));

[0079] 5-minute fluctuation threshold ThrethholdM = (-100 ~ 100);

[0080] 1-hour fluctuation threshold ThrethholdH = (-1000 ~ 1000);

[0081] One-day fluctuation threshold ThrethholdD = (-10000 ~ 10000);

[0082] Adjustment logic that triggers automatic adjustment:

[0083] When a scheduled task (every 5 minutes, every hour, every day) is triggered, the fluctuation value (Cm, Ch, Cd) at that time is calculated according to the task type; the fluctuation value is compared with the threshold value corresponding to each task type (ThrethholdM, ThrethholdH, ThrethholdD). If it exceeds the threshold range, the cache is changed, otherwise no change is made;

[0084] When the cache is changed, the size of the target off-heap cache container TargetCacheSize() and the number of target off-heap cache containers TargetCacheNumber() are calculated and adjusted. Among them, the size of the target off-heap cache container TargetCacheSize() = (Am or Ah or Ad) × 1.1 (the average value (Am or Ah or Ad) calculated by the formula corresponding to the trigger period, increased by 10%, this value can be selectively determined, for example, it can also be set to increase by 20% or 25%, etc.); the number of target off-heap cache containers TargetCacheNumber() = Total() / TargetCacheSize() (returns an integer);

[0085] If the calculated TargetCacheNumber is less than 1, it will not be changed. This adjustment is only effective for newly added off-heap cache containers, and existing off-heap cache containers will not be affected.

[0086] Optionally, the method of the embodiment of the present invention further includes: determining whether the data stored in the off-heap cache container meets a preset cache condition; if not, updating the cache data stored in the off-heap container. By dynamically updating the data stored in the off-heap cache container, cache data utilization can be improved.

[0087] The preset cache condition can be selectively determined according to the actual situation, for example, the preset cache condition is that the number of times the cache data is read is less than the preset number threshold, or the preset cache condition is that the cache time of the cache data is less than or equal to the preset duration threshold. Of course, the preset cache condition can also be a combination of the above two conditions.

[0088] The method of updating the cache data stored in the off-heap container can be selected according to the actual situation. In some embodiments, the update method is as follows: 1. First return the current cache data normally; 2. Then delete the mapping pair from the off-heap cache reference mapping; 3. Clear the data stored in the off-heap cache container; 4. Then put the off-heap cache container back into the off-heap cache container list. In other embodiments, the update method is as follows: skip the cache and directly read the data from the database, and re-cache the database results in the off-heap memory.

[0089] In order to avoid cache avalanche caused by cache invalidation hitting the database during concurrent data requests, you can use asynchronous startup threads to obtain the latest data from the database and save it to off-heap memory. This ensures that there is always data in the cache.

[0090] In an optional embodiment, when caching data, if the off-heap cache container list is not empty, that is, there is currently no available off-heap cache container, the mapping corresponding to the least recently used Key in the LinkedHashMap can be deleted; the data in the off-heap cache container corresponding to the key is cleared, so that the off-heap cache container corresponding to the key is provided for new data to be cached.

[0091] The above describes in detail the application scenario in which the method of the embodiment of the present invention is applied to save data to off-heap memory. In addition, the method of the embodiment of the present invention can also be applied to read data from off-heap memory. Optionally, when reading data, determine whether there is identification information of the data to be read in the off-heap based on the off-heap cache reference mapping; if so, determine the off-heap container corresponding to the identification information in the off-heap memory based on the off-heap cache reference mapping, and obtain the cache data corresponding to the identification information from the off-heap container; if not, obtain the data corresponding to the identification information from the database, save the data corresponding to the identification information in the off-heap memory, and update the off-heap cache reference mapping.

[0092] Figure 3 Schematic diagram of the principle of reading data in a method for implementing local cache using off-heap memory according to an embodiment of the present invention. Figure 3 As shown, after receiving the user request, it is first determined whether there is a key corresponding to the user request in the cache; if so, the data is obtained from the cache; otherwise, the data is obtained from the database and placed in the cache. Figure 3 The response here refers to the feedback information generated in response to the user's request.

[0093] Optionally, before the step of obtaining the cached data corresponding to the identification information from the off-heap container, it also includes: determining that the cached data satisfies a preset reading condition; and when the cached data does not satisfy the preset reading condition, obtaining the data corresponding to the identification information from the database, and saving the data corresponding to the identification information to the off-heap memory.

[0094] Figure 4 Schematic diagram of the principle of a method for implementing local caching using off-heap memory in an optional embodiment of the present invention. Figure 4 As shown,

[0095] After receiving the user request, the cache is obtained, and then it is determined whether the cache contains the data; if not, the data is read from the database and the cache operation is performed; if so, the judgment logic is executed, and if the judgment result passes, the data is read from the cache and returned; if the judgment result fails, the cache is cleared, the data is read from the database, and the cache operation is performed.

[0096] Cache operations include:

[0097] Determine whether the cached data meets the cache size limit; if so, proceed to the next step, otherwise end the process;

[0098] Determine whether there is off-heap space; if so, write the data read from the database into the cache. Otherwise, delete the mapping corresponding to the least recently used key in LinkedHashMap; clear the data in the off-heap cache container corresponding to the key, so that the off-heap cache container corresponding to the key is provided for new data to be cached, and then write the data read from the database into the cache.

[0099] The judgment logic includes:

[0100] 1. Determine whether the cached data has reached the upper limit of the number of reads; if so, clear the cache, read the data from the database, and perform the cache operation. If not, enter the second logic;

[0101] 2. Determine whether the cache expiration time has been reached. If so, clear the cache, read data from the database, and perform cache operations; otherwise, read data from the cache and return.

[0102] The present invention uses off-heap memory to implement local caching, which can reduce system GC and Full GC, reduce the impact of cache on GC and business response, and improve system performance. When reading data, the timeliness of the read data can be guaranteed through judgment logic and cache operation.

[0103] According to a second aspect of an embodiment of the present invention, a device for implementing the method provided in the first aspect of an embodiment of the present invention is provided.

[0104] Figure 5 Schematic diagram of main modules of a device for implementing local caching using off-heap memory according to an embodiment of the present invention. Figure 5 As shown, the device 500 for implementing local caching using off-heap memory includes:

[0105] A response module 501 obtains an off-heap cache container from an off-heap cache container list in response to a data cache request;

[0106] A saving module 502 saves the to-be-cached data in the data cache request into the off-heap cache container;

[0107] The mapping module 503 stores the identification information of the data to be cached and the identification information of the off-heap cache container into an off-heap cache reference mapping to implement local caching using off-heap memory.

[0108] Optionally, the device of an embodiment of the present invention also includes a monitoring module 504, which is used to: monitor the average size of all cached data in the off-heap memory and the total number of writes to all off-heap cache containers to determine the fluctuation value of the off-heap memory; when the fluctuation value meets a preset fluctuation condition, adjust the number and size of the off-heap cache containers in the off-heap memory.

[0109] Optionally, the fluctuation value of the off-heap memory is determined according to the following formula:

[0110] C=(Am(1)-Am(2))×logM(Count)

[0111] In the formula, C represents the fluctuation value of the off-heap memory; Am(1) represents the average size of all cached data in the off-heap memory in the current period; Am(2) represents the average size of all cached data in the off-heap memory in the period before the current period; M represents a positive number; Count represents the total number of writes to all off-heap cache containers in the off-heap memory in the current period.

[0112] Optionally, the device of the embodiment of the present invention further includes an update module 505, which is used to: determine whether the data stored in the off-heap cache container meets a preset cache condition; if not, update the cache data stored in the off-heap container.

[0113] Optionally, the apparatus of the embodiment of the present invention further includes a reading module 506, which is used to: when reading data, determine whether identification information of the data to be read exists in the off-heap cache according to the off-heap cache reference mapping;

[0114] If so, determining the off-heap container corresponding to the identification information in the off-heap memory according to the off-heap cache reference mapping, and obtaining the cache data corresponding to the identification information from the off-heap container;

[0115] If not, data corresponding to the identification information is obtained from the database, the data corresponding to the identification information is saved in the off-heap memory, and the off-heap cache reference mapping is updated.

[0116] Optionally, the reading module is also used to: before the step of obtaining the cached data corresponding to the identification information from the off-heap container, determine that the cached data meets a preset reading condition; and when the cached data does not meet the preset reading condition, obtain the data corresponding to the identification information from the database, and save the data corresponding to the identification information to the off-heap memory.

[0117] According to a third aspect of an embodiment of the present invention, there is provided an electronic device for implementing local cache using off-heap memory, including:

[0118] one or more processors;

[0119] a storage device for storing one or more programs,

[0120] When the one or more programs are executed by the one or more processors, the one or more processors implement the method provided in the first aspect of the embodiment of the present invention.

[0121] According to a fourth aspect of an embodiment of the present invention, a computer-readable medium is provided, on which a computer program is stored, and when the program is executed by a processor, the method provided by the first aspect of the embodiment of the present invention is implemented.

[0122] Figure 6 An exemplary system architecture 600 is shown to which the method for implementing local caching using off-heap memory or the apparatus for implementing local caching using off-heap memory according to an embodiment of the present invention can be applied.

[0123] like Figure 6 As shown, system architecture 600 may include terminal devices 601, 602, 603, network 604 and server 605. Network 604 is used to provide a medium for communication links between terminal devices 601, 602, 603 and server 605. Network 604 may include various connection types, such as wired, wireless communication links or optical fiber cables, etc.

[0124] Users can use terminal devices 601, 602, and 603 to interact with server 605 through network 604 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 601, 602, and 603, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social platform software, etc. (only examples).

[0125] The terminal devices 601 , 602 , and 603 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, and desktop computers, etc.

[0126] The server 605 may be a server that provides various services, such as a backend management server (only an example) that provides support for shopping websites browsed by users using the terminal devices 601, 602, and 603. The backend management server may analyze and process the received data such as product information query requests, and feed back the processing results (such as target push information, product information - only an example) to the terminal device.

[0127] It should be noted that the method for implementing local caching using off-heap memory provided in the embodiment of the present invention is generally executed by the server 605 , and accordingly, the device for implementing local caching using off-heap memory is generally set in the server 605 .

[0128] It should be understood that Figure 6 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to implementation requirements.

[0129] Reference below Figure 7 , which shows a schematic diagram of the structure of a computer system 700 of a terminal device suitable for implementing an embodiment of the present invention. Figure 7 The terminal device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.

[0130] like Figure 7 As shown, the computer system 700 includes a central processing unit (CPU) 701, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 702 or a program loaded from a storage part 708 into a random access memory (RAM) 703. In the RAM 703, various programs and data required for the operation of the system 700 are also stored. The CPU 701, the ROM 702, and the RAM 703 are connected to each other via a bus 704. An input / output (I / O) interface 705 is also connected to the bus 704.

[0131] The following components are connected to the I / O interface 705: an input section 706 including a keyboard, a mouse, etc.; an output section 707 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 708 including a hard disk, etc.; and a communication section 709 including a network interface card such as a LAN card, a modem, etc. The communication section 709 performs communication processing via a network such as the Internet. A drive 710 is also connected to the I / O interface 705 as needed. A removable medium 711, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 710 as needed, so that a computer program read therefrom is installed into the storage section 708 as needed.

[0132] In particular, according to the embodiments disclosed in the present invention, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present invention include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 709, and / or installed from the removable medium 711. When the computer program is executed by the central processing unit (CPU) 701, the above-mentioned functions defined in the system of the present invention are executed.

[0133] It should be noted that the computer-readable medium shown in the present invention may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present invention, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present invention, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination of the above.

[0134] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present invention. In this regard, each box in the flow chart or block diagram can represent a module, a program segment, or a part of a code, and the above-mentioned module, program segment, or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.

[0135] The modules involved in the embodiments of the present invention may be implemented in software or hardware. The modules described may also be set in a processor, for example, they may be described as: a processor comprising: a response module, which obtains an off-heap cache container from an off-heap cache container list in response to a data cache request; a storage module, which stores the data to be cached in the data cache request into the off-heap cache container; a mapping module, which stores the identification information of the data to be cached and the identification information of the off-heap cache container into an off-heap cache reference mapping, so as to realize local caching using off-heap memory. . Among them, the names of these modules do not constitute a limitation on the modules themselves in some cases. For example, the storage module may also be described as "a module that stores the identification information of the data to be cached and the identification information of the off-heap cache container into an off-heap cache reference mapping".

[0136] As another aspect, the present invention also provides a computer-readable medium, which may be included in the device described in the above embodiment; or it may exist independently without being assembled into the device. The above computer-readable medium carries one or more programs, and when the above one or more programs are executed by a device, the device includes: in response to a data cache request, obtaining an off-heap cache container from an off-heap cache container list; saving the data to be cached in the data cache request to the off-heap cache container; storing the identification information of the data to be cached and the identification information of the off-heap cache container in the off-heap cache reference map, so as to realize local caching using off-heap memory.

[0137] According to the technical solution of the embodiment of the present invention, the off-heap memory is used to implement the local cache off-heap storage, which can reduce the impact of the cache on GC and business response and improve system performance.

[0138] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.< / bytebuffer>

Claims

1. A method for implementing local caching using off-heap memory, characterized in that: include: In response to the data cache request, obtaining an off-heap cache container from the off-heap cache container list; The data to be cached in the data cache request is saved in the off-heap cache container; the average size of all cached data in the off-heap memory and the total number of writes to all off-heap cache containers are monitored, and the fluctuation value of the off-heap memory in at least one dimension is determined according to the following formula: C = (Am(1)-Am(2))×logM(Count); wherein C represents the fluctuation value of the off-heap memory; Am(1) represents the average size of all cached data in the off-heap memory in the current period; Am(2) represents the average size of all cached data in the off-heap memory in the period before the current period; and M represents a positive number; Count represents the total number of writes to all off-heap cache containers in the off-heap memory in the current period; when the fluctuation value meets the preset fluctuation condition, the number and size of the off-heap cache containers in the off-heap memory are adjusted; The identification information of the data to be cached and the identification information of the off-heap cache container are stored in an off-heap cache reference map to implement local caching using off-heap memory.

2. The method according to claim 1, characterized in that Also includes: Determine whether the data stored in the off-heap cache container meets a preset cache condition; If not, the cache data stored in the off-heap container is updated.

3. The method according to claim 1, characterized in that When reading data, According to the off-heap cache reference mapping, determine whether there is identification information of the data to be read in the off-heap memory; If so, determining the off-heap container corresponding to the identification information in the off-heap memory according to the off-heap cache reference mapping, and obtaining the cache data corresponding to the identification information from the off-heap container; If not, data corresponding to the identification information is obtained from the database, the data corresponding to the identification information is saved in the off-heap memory, and the off-heap cache reference mapping is updated.

4. The method according to claim 3, characterized in that Before the step of obtaining the cached data corresponding to the identification information from the off-heap container, it also includes: determining that the cached data meets a preset reading condition; and when the cached data does not meet the preset reading condition, obtaining the data corresponding to the identification information from the database, and saving the data corresponding to the identification information to the off-heap memory.

5. A device for implementing local caching using off-heap memory, characterized in that: include: A response module, in response to a data cache request, obtains an off-heap cache container from an off-heap cache container list; A saving module saves the to-be-cached data in the data cache request into the off-heap cache container; A monitoring module is used to monitor the average size of all cached data in the off-heap memory and the total number of writes to all off-heap cache containers, and determine the fluctuation value of the off-heap memory in at least one dimension according to the following formula: C = (Am(1)-Am(2))×logM(Count); wherein C represents the fluctuation value of the off-heap memory; Am(1) represents the average size of all cached data in the off-heap memory in the current period; Am(2) represents the average size of all cached data in the off-heap memory in the period before the current period; and M represents a positive number; Count represents the total number of writes to all off-heap cache containers in the off-heap memory in the current period; when the fluctuation value meets the preset fluctuation condition, the number and size of the off-heap cache containers in the off-heap memory are adjusted; The mapping module stores the identification information of the data to be cached and the identification information of the off-heap cache container into the off-heap cache reference mapping to realize local caching using off-heap memory.

6. An electronic device that uses off-heap memory to implement local caching, characterized in that: include: one or more processors; a storage device 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 method according to any one of claims 1 to 4.

7. A computer readable medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 4 is implemented.

Citation Information

Patent Citations

  • Method and system for responding to data requests by utilizing caches

    CN108804447A

  • Data cache system, method and computer terminal for java platform

    CN109101554A