介绍

Caffeine是一个基于Java8开发的提供了近乎最佳命中率的高性能的缓存库。

缓存和ConcurrentMap有点相似,但还是有所区别。最根本的区别是ConcurrentMap将会持有所有加入到缓存当中的元素,直到它们被从缓存当中手动移除。

但是,Caffeine的缓存Cache 通常会被配置成自动驱逐缓存中元素,以限制其内存占用。在某些场景下,LoadingCache和AsyncLoadingCache 因为其自动加载缓存的能力将会变得非常实用。

基本使用

GitHub 官方文档:https://github.com/ben-manes/caffeine/wiki/Home-zh-CN

项目集成

使用 Caffeine 作为一级缓存,Redis 作为二级缓存。先从 Caffeine 读取缓存,如果读不到则到 Redis 中读取,如果还没有则返回 null.

模块使用了 Redis 作为二级缓存,使用 stringRedisTemplate 模板,Jedis 作为客户端。

Spring 配置

@Configuration
public class Config {
@Bean
public AbstractStringFirstCache stringFirstCache() {
// 可以随意更换底层实现,比如我们可以使用 Guava Cache,只需要 new GuavaCache() 并继承 AbstractStringFirstCache 即可
return new CaffeineCache();
} @Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
return new StringRedisTemplate(factory);
} @Bean
public JedisConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration("localhost", 6379);
return new JedisConnectionFactory(config);
}
}

核心代码


// 核心接口
public interface FirstCache<K, V> {
V get(@NonNull K key); void set(@NonNull K key, V value); void delete(@NonNull K key);
} public abstract class AbstractFirstCache<K, V> implements FirstCache<K, V> {
} public abstract class AbstractStringFirstCache extends AbstractFirstCache<String, String> {
abstract void set(@NonNull String key, String value, Long expertTime, TimeUnit timeUnit);
} public class CaffeineCache extends AbstractStringFirstCache { Log log = LogFactory.get(); //Hutool api @Autowired
private StringRedisTemplate stringRedisTemplate; LoadingCache<String, String> caffeineCache; public CaffeineCache() {
LoadingCache<String, String> cache = Caffeine.newBuilder()
.recordStats()
.initialCapacity(100)
.maximumSize(1000)
.writer(new CacheWriter<String, String>() {
@Override
public void write(String key, String value) {
log.info("caffeineCache write key=" + key + ", value=" + value);
} @Override
public void delete(String key, String value, RemovalCause cause) {
log.info("caffeineCache delete key=" + key);
}
})
.expireAfterWrite(30, TimeUnit.SECONDS) //一个元素将会在其创建或者最近一次被更新之后的一段时间后被认定为过期项
.build(new CacheLoader<String, String>() {
// 查询二级缓存
@Override
public @Nullable String load(@NonNull String s) throws Exception {
String value = stringRedisTemplate.opsForValue().get(s);
if (StrUtil.isEmpty(value)) {
return null;
}
return value;
}
});
caffeineCache = cache;
} @Override
public String get(String key) {
//查找缓存,如果缓存不存在则生成缓存元素
String value = caffeineCache.get(key);
log.info("get key from caffeineCache, key: " + key);
return value;
} @Override
public void set(String key, String value) {
//放入二级缓存
stringRedisTemplate.opsForValue().set(key, value);
stringRedisTemplate.expire(key, 15, TimeUnit.MINUTES);
} @Override
void set(String key, String value, Long expertTime, TimeUnit timeUnit) {
stringRedisTemplate.opsForValue().set(key, value);
stringRedisTemplate.expire(key, expertTime, timeUnit);
} @Override
public void delete(String key) {
caffeineCache.invalidate(key);
stringRedisTemplate.delete(key);
log.info("delete key from caffeineCache, key: " + key);
}
}

工具类

@Component
public class CaffeineCacheUtils { static AbstractStringFirstCache cache; @Autowired
public void setCache(AbstractStringFirstCache cache) {
CaffeineCacheUtils.cache = cache;
} /**
* 查找缓存,如果缓存不存在则生成缓存元素
*
* @return value or null
*/
public static String get(@NonNull String key) {
return cache.get(key);
} /**
* 设置缓存,默认 15min
*/
public static void set(@NonNull String key, String value) {
cache.set(key, value);
} /**
* 设置缓存,提供时间和时间单位
*/
public static void set(@NonNull String key, String value, @NonNull Long expertTime, @NonNull TimeUnit timeUnit) {
cache.set(key, value, expertTime, timeUnit);
} /**
* 删除缓存
*/
public static void delete(@NonNull String key) {
cache.delete(key);
}
}

最新文章

  1. PC机上的COM1口和COM2口
  2. [Tools] Vim插件管理
  3. 关于/etc/hosts文件
  4. 使用SQL Server存储ASP.NET Session变量
  5. 二十二、OGNL的一些其他操作
  6. linux下usb驱动接口中端点介绍
  7. MyBatis(3.2.3) - Multiple results as a map
  8. Call to undefined function pg_
  9. 武汉科技大学ACM:1005: 华科版C语言程序设计教程(第二版)例题5.8
  10. jquery中checkbox全选失效的解决方法
  11. HDU 2136 Largest prime factor
  12. Tomcat 部署项目的三种方法
  13. IIC接口下的24C02 驱动分析
  14. Web开发——Photoshop(PSD格式截取)
  15. 启动tomcat时报错:http-nio-8080-exec-10
  16. day_11 py 名片管理系统
  17. redis和memcached选择,对比分析
  18. 序列化 json和pickle
  19. github常用项目汇总
  20. 【Yii系列】Yii2.0的安装与调试

热门文章

  1. PHP简单的计算位数的函数
  2. 痞子衡嵌入式:恩智浦i.MX RT1xxx系列MCU硬件那些事(2.6)- 串行NOR Flash下载算法(MCUXpresso IDE篇)
  3. kill的使用
  4. 数据库零基础之---了解数据库的事务[ACID]
  5. JAVA NIO Selector Channel
  6. Spring3 MVC 注解(一)---注解基本配置及@controller和 @RequestMapping 常用解释(转)
  7. 一个坑,bootstrap selectpicker 重置下拉列表时遇到的问题
  8. BCC和libbpf的转换
  9. [新手教程]申请https泛域名解析
  10. redhat_快捷键和shell命令操作.md