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

What is the Java8-based caching framework Caffeine

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

Share

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

Java8-based caching framework Caffeine is how, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Caffeine is a Java8-based high-performance caching framework that claims to be perfect. Caffeine is inspired by Guava Cache's API, and the use of API and Guava is consistent. It draws on the design experience of Guava Cache and ConcurrentLinkedHashMap.

Performance comparison

The benchmark uses the Java microbenchmark tool to provide accurate analysis. Cache is configured as

Caffeine and ConcurrentLinkedHashMap determine the size of their internal structure based on the number of CPU. The concurrency level configuration of Guava is 64 (the default is 4 to reduce memory usage). Ehcache v2 is hard-coded internally into 100 segments, while v3 is not segmented

100% read operation

Read 75% and write 25%

Write 100%

The above three test patterns are from the official website of Caffeine. As can be seen from the figure, the performance of Caffeine is very popular with other caching frameworks.

Using Caffeine in Matrix-Web

Introduce the dependency of caffeine in the pom file of the project, as follows:

Com.github.ben-manes.caffeine

Caffeine

${caffeine.version}

Create an abstract class AbstractCaffineCache that uses paradigms to constrain cached data types and implements three methods, put, get, and clear.

Public abstract class AbstractCaffineCache {

Protected LoadingCache loadingCache

Abstract LoadingCache createLoadingCache ()

Public boolean put (String key, T value) {

If (loadingCache==null) {

LoadingCache=createLoadingCache ()

}

LoadingCache.put (key, value)

Return Boolean.TRUE

}

Public T get (String key) {

If (loadingCache==null) {

LoadingCache=createLoadingCache ()

}

Try {

Return loadingCache.get (key)

} catch (Exception e) {

Return null

}

}

Public boolean clear (String key) {

If (loadingCache==null) {

LoadingCache=createLoadingCache ()

}

LoadingCache.invalidate (key)

Return Boolean.TRUE

}

}

Create a cache class for UserRolePermissionCache that caches the user's information, the user's role information, and the user's permission information. Create a LoadingCache class that sets the cache expiration time and the maximum number of caches.

Public class UserRolePermissionCache extends AbstractCaffineCache {

@ Override

LoadingCache createLoadingCache () {

LoadingCache = Caffeine.newBuilder ()

.accounreAfterWrite (10, TimeUnit.MINUTES)

.initialCapacity (10)

.maximumSize (99999999)

.recordStats ()

.build (new CacheLoader () {)

@ Override

Public SysUser load (String key) throws Exception {

Return null

}

});

Return loadingCache

}

}

Inject UserRolePermissionCache into spring ioc as follows:

@ Configuration

Public class CaffineCacheConfig {

@ Bean

Public UserRolePermissionCache userRolePermissionCache () {

Return new UserRolePermissionCache ()

}

}

How to use, in UserPermissonService, query SysUser information, if there is data in the cache, take it in the cache, if not, read it in the database and do the cache.

@ Component

Public class UserPermissonService {

@ Autowired

SysUserMapper sysUserMapper

@ Autowired

UserRolePermissionCache userRolePermissionCache

Public SysUser getUserRolePerssion (String userId) {

SysUser sysUser = (SysUser) userRolePermissionCache.get (userId)

If (sysUser = = null) {

SysUser = sysUserMapper.selectUserRolePermission (userId)

UserRolePermissionCache.put (userId, sysUser)

}

Return sysUser

}

} is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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