Best memory cache framework caffeine

Caffeine is a high-performance cache library and the best (optimal) cache framework based on Java 8.

Cache (CACHE), based on Google guava, caffeine provides a memory cache, which greatly improves the experience of designing guava's cache and concurrentlinked HashMap.

Caching is similar to concurrentmap, but they are not exactly the same. The most basic difference is that concurrentmap saves all elements added to it until they are explicitly deleted. On the other hand, the cache is usually configured to automatically delete entries to limit its memory footprint. In some cases, loadingcache or asyncloadingcache may be useful because it is loaded automatically.

Caffeine provides a flexible structure to create cache, and has the following features:

1. Load / fill

Caffeine provides the following four types of loading policies:

1.1. Manual

The cache interface can explicitly control the retrieval, updating, and deletion of entries.

1.2. Loading

Loadingcache builds a cache by associating a cacheloader

You can batch query through the getall method of loadingcache

1.3. Asynchronous (Manual)

Asynccache is another type of cache that calculates entries based on the executor and returns a completable future.

1.4. Asynchronously Loading

Asyncloadingcache is an asynccache associated with an asynccacheloader

2. Rejection

Caffeine provides three culling methods: size based, time-based and reference based

2.1. Size-based

If the number of cached entries should not exceed a certain value, you can use cafe maximumSize(long)。 If this value is exceeded, the entry that has not been accessed for a long time or is not often used will be eliminated.

If different items have different weight values, you can use caffeine Weight (weigher) to specify a weight function, and use caffeine Maximumweight (long) to set the maximum weight value.

Simply put, either limit the number of cache entries or limit the weight value of cache entries, whichever is the other. It is easy to understand the limit quantity. To limit the weight, first you have to provide a function to set the weight value of each item, and then you can display the maximum weight.

2.2. Time-based

It is recommended to actively maintain the entries in the cache, rather than wait until the cache entries are found to be invalid when accessing. It means to load in advance and maintain regularly.

You can caffeine Scheduler (scheduler) to specify the scheduling thread

2.3. Reference-based

caffeine. Weakkeys () uses weak references to store keys. If the key is not strongly referenced, the garbage collector is allowed to recycle the entry. Note that = = is used to determine the key.

caffeine. Weakvalues () uses weak references to store value. If the value is not strongly referenced, the garbage collector is allowed to recycle the entry. Note that = = is used to determine the key.

caffeine. Softvalues() uses soft references to store value.

3. Delete

Terminology:

3.1. Explicitly delete

3.2. monitor

4. Refresh

Through loadingcache Refresh (k) performs asynchronous refresh by overriding cacheloader Reload (k, V) can customize the refresh logic

5. Statistics

Use caffeine Recordstats(), you can turn on the statistics function. Cache. The stats () method returns a cachestats object that provides the following statistics:

6. Examples

Finally, let's get to the point

Generally speaking, redis is used as the primary cache and caffeine is used as the secondary cache

6.1. Example 1: use alone

pom. xml

config

service

One more point: you have used the local cache. You must have used the L1 cache. If the L1 cache fails to achieve the expected performance, local cache will be selected.

controller

6.2. Example 2: used with springboot

Spring boot supports caffeine, which can simplify some steps, but it also has many limitations

application. yml

service

It's convenient to use annotations, but it's not easy to control. It's better to customize them

7. Engineering structure

Complete POM xml

https://github.com/chengjiansheng/cjs-caffeine-example

8. Documentation

https://github.com/ben-manes/caffeine/wiki

https://github.com/ben-manes/caffeine

https://www.itcodemonkey.com/article/9498.html

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>