Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use GuavaCache

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge of this article "how to use GuavaCache", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can gain something after reading this article. Let's take a look at this "how to use GuavaCache" article.

Jvm caching is actually a cache of heap memory, which is what we often call a local cache. Commonly used like map,set,list and so on. However, these caches have poor concurrency, and there is no good deletion expiration mechanism, so it is difficult to maintain. Now let's introduce a local cache: Guava Cache.

Guava Cache is a high-performance local cache designed from CurrentHashMap, which can cope with high concurrency and has a rich policy mechanism to clear the cache. Let's introduce GuavaCache in detail.

One: basic usage

1. Create GuavaCache:

The above method is to create a cache using CacheLoader in build, or if it is not specified in build, you can write a separate method and use Callable callback to obtain it:

As shown in the figure, when cache.get is called in the method, a Callable is created. When there is no value in the local cache, the call () method is called to query the origin, and then manually placed in the cache to return.

2. Delete cache:

Passive deletion:

1. Delete based on data size:

Delete rule: LRU + FIFO (LRU: the most recently accessed priority deletion. If the number of visits is the same, the one saved first will be deleted first)

2. How long will it expire after the last visit:

3. How long will it take to write and expire?

4. Deletion based on reference:

Proactive deletion:

Delete separately: cache.invalidate ("1");-delete the one with a key of 1

Batch deletion: cache.invalidateAll (Arrays.asList ("1", "2"));-- Delete those with key of 1 and 2

Clear all: cache.invalidateAll ()

Second: GuavaCache principle:

Data structure:

Localcache structure is similar to ConcurrentHashMap, divided into multiple segments, can be locked in segments, as shown in the figure, the internal structure of a cache, the whole is a Segment array (red), the array length determines the number of concurrency. The structure of each segment is the right part, inherits the ReentrantLock, and the write operation needs to acquire the lock. Contains five queues and a table (an array of type atomic). The role of queues and table: we mentioned several ways to delete them. These queues are the specific implementation rules, from top to bottom: the key that needs to be cleaned after weakly referencing the object gc. After weakly referencing the object gc, the value that needs to be cleaned. When the segment reaches the critical value, it is removed from the queue header. Queues sorted by write time, with the same time and new writes coming later. Queues sorted by access time, with the same time and new visits coming later.

Table: stores data. The process is to hash the key, find the corresponding segment, hash the key, find the specific nodes in the table, store them as ReferenceEntry objects, and prevent hash conflicts in the form of linked lists. In addition, V is not a real value, but rather encapsulates a real value in ValueReference.

The above is about the content of this article on "how to use GuavaCache". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report