缓存框架jetcache

2024-02-03 00:36
文章标签 框架 缓存 jetcache

本文主要是介绍缓存框架jetcache,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在实际应用中,并不是单一的使用本地缓存或者redis,更多是组合使用来满足不同的业务场景。
jetcache组件实现了优雅的组合本地缓存和远程缓存。

支持多种缓存类型:本地缓存、分布式缓存、多级缓存。

官网地址:https://github.com/alibaba/jetcache

官方文档:https://github.com/alibaba/jetcache/tree/master/docs/CN

一、依赖

非SpringBoot项目参考官网配置
在这里插入图片描述
SpringBoot依赖

<dependency><groupId>com.alicp.jetcache</groupId><artifactId>jetcache-starter-redis</artifactId><version>2.7.0</version>
</dependency><!--  jetcache2.7.x版本需要额外添加该依赖-->
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>4.3.1</version>
</dependency>

在这里插入图片描述

二、修改配置文件,配置redis地址和线程数

jetcache:## 统计间隔,0表示不统计,开启后定期在控制台输出缓存信息statIntervalMinutes: 15## 是否把cacheName作为远程缓存key前缀areaInCacheName: false## 本地缓存配置local:default: ## default表示全部生效,也可以指定某个cacheName## 本地缓存类型,其他可选:caffeine/linkedhashmaptype: linkedhashmapkeyConvertor: fastjson## 远程缓存配置remote:default: ## default表示全部生效,也可以指定某个cacheNametype: redis## key转换器方式nkeyConvertor: fastjsonbroadcastChannel: projectA## redis序列化方式valueEncoder: javavalueDecoder: java## redis线程池poolConfig:minIdle: 5maxIdle: 20maxTotal: 50## redis地址与端口host: 127.0.0.1port: 6379

在这里插入图片描述

三、启动类添加注解@EnableCreateCacheAnnotation开启缓存

@EnableMethodCache(basePackages = “com.example.jetcachedemo”)注解,配置缓存方法扫描路径

四、使用缓存,通过三种方式

方式一:AOP模式,通过@Cached,@CacheUpdate,@CacheInvalidate

@RestController
@RequestMapping("user")
public class UserController{@GetMapping("getRemote")@Cached(name="userCache:",key="#id",expire=3600,timeUnit=TimeUnit.SECONDS,cacheType = CacheType.REMOTE)public User getRemote(Long id){//直接新建用户,模拟从数据库获取数据User user = new User();user.setId(id);user.setName("用户remote"+id);user.setAge(23);user.setSex(1);System.out.println("第一次获取数据,未走缓存:"+id);return user;}@GetMapping("getLocal")@Cached(name="userCache:", key = "#id", expire = 3600, timeUnit = TimeUnit.SECONDS, cacheType = CacheType.LOCAL)public User getLocal(Long id){// 直接新建用户,模拟从数据库获取数据User user = new User();user.setId(id);user.setName("用户local"+id);user.setAge(23);user.setSex(1);System.out.println("第一次获取数据,未走缓存:"+id);return user;}@GetMapping("getBoth")@Cached(name="userCache:", key = "#id", expire = 3600, timeUnit = TimeUnit.SECONDS, cacheType = CacheType.BOTH)public User getBoth(Long id){// 直接新建用户,模拟从数据库获取数据User user = new User();user.setId(id);user.setName("用户both"+id);user.setAge(23);user.setSex(1);System.out.println("第一次获取数据,未走缓存:"+id);return user;}@PostMapping("updateUser")@CacheUpdate(name = "userCache:", key = "#user.id", value = "#user")public Boolean updateUser(@RequestBody User user){// TODO 更新数据库return true;}@PostMapping("deleteUser")@CacheInvalidate(name = "userCache:", key = "#id")public Boolean deleteUser(Long id){// TODO 从数据库删除return true;}
}

实体类User一定要实现序列化,即声明Serializable

@Data
public class User implements Serializable {private Long id;private String name;private Integer age;private Integer sex;
}

访问localhost:8088/user/getRemote?id=1
在这里插入图片描述
因为配置的是远程缓存,在redis中也能看到对应的key
在这里插入图片描述
访问localhost:8088/user/getLocal?id=1,这个方法是从本地缓存获取的,现在只有远程缓存上有数据,我们调用发现缓存数据还是拿到了,这说明当我们在配置文件中配置了本地缓存和远程缓存后,方式一中本地缓存和远程缓存会自动相互调用
比如本地缓存有这个key,redis中没有,通过远程缓存方式访问时,会先从redis获取,如果没有会自动获取本地缓存,但是数据还是存储在本地缓存,并不会同步到redis上,这样更加灵活的实现了多级缓存架构
在这里插入图片描述

方式二,API模式,通过@CreateCache,注:在jetcache 2.7 版本CreateCache注解已废弃,不推荐使用

@RestController
@RequestMapping("user2")
public class User2Controller {@CreateCache(name= "userCache:", expire = 3600, timeUnit = TimeUnit.SECONDS, cacheType = CacheType.BOTH)private Cache<Long, Object> userCache;@GetMapping("get")public User get(Long id){if(userCache.get(id) != null){return (User) userCache.get(id);}User user = new User();user.setId(id);user.setName("用户both"+id);user.setAge(23);user.setSex(1);userCache.put(id, user);System.out.println("第一次获取数据,未走缓存:"+id);return user;}@PostMapping("updateUser")public Boolean updateUser(@RequestBody User user){// TODO 更新数据库userCache.put(user.getId(), user);return true;}@PostMapping("deleteUser")public Boolean deleteUser(Long id){// TODO 从数据库删除userCache.remove(id);return true;}}

测试下CreateCache的形式:localhost:8088/user2/get?id=4
在这里插入图片描述
正常获取了,并且redis中也有了对应的值
在这里插入图片描述
而当我们把缓存方式更改为LOCAL后,再访问localhost:8088/user2/get?id=5

@CreateCache(name= "userCache:", expire = 3600, timeUnit = TimeUnit.SECONDS, cacheType = CacheType.LOCAL)

会发现redis中就没有对应缓存了,只在本地缓存存在,说明我们指定本地缓存的形式成功了
在这里插入图片描述

方式三,高级API模式:通过CacheManager,2.7 版本才可使用

①、添加依赖

<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>2.0.25</version>
</dependency>

②、配置类

@Configuration
public class JetcacheConfig{@Autowiredprivate CacheManager cacheManager;private Cache<Long,Object> userCache;@PostConstructpublic void init(){QuickConfig qc = QuickConfig.newBuilder("userCache:").expire(Duration.ofSeconds(3600)).cacheType(CacheType.BOTH)//本地缓存更新后,将在所有的节点中删除缓存,以保持强一致性.syncLocal(false).build();userCache = cacheManager.getOrCreateCache(qc);}@Beanpublic Cache<Long,Object> getUserCache(){return userCache;}
}

③、调用

@RestController
@RequestMapping("user")
public class UserController{@AutowiredJetcheConfig jetcacheConfig;@Autowiredprivate Cache<Long,Object> userCache;@GetMapping("get")public User get(Long id){if(userCache.get(id) != null){return (User) userCache.get(id);}User user = new User();user.setId(id);user.setName("用户both"+id);user.setAge(23);user.setSex(1);userCache.put(id, user);System.out.println("第一次获取数据,未走缓存:"+id);return user;}@PostMapping("updateUser")public Boolean updateUser(@RequestBody User user){// TODO 更新数据库userCache.put(user.getId(), user);return true;}@PostMapping("deleteUser")public Boolean deleteUser(Long id){// TODO 从数据库删除userCache.remove(id);return true;}}

多级缓存的形式,会先从本地缓存获取数据,本地获取不到会从远程缓存获取;

④、启动redis

如果启动出现NoClassDefFoundError: redis/clients/util/Pool或NoClassDefFoundError: redis/clients/jedis/UnifiedJedis报错,说明springboot与jetcache版本不一致,对应关系可参考上述第一步中的说明 同时如果使用的是jetcache2.7.x版本,因为该版本中有jedis包的依赖,需要额外添加如下依赖,或者将jetcache版本将至2.6.5以下

<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>4.3.1</version>
</dependency>

调用localhost:8088/user/get?id=11
在这里插入图片描述
redis中缓存设置成功!
在这里插入图片描述

常见打的报错

1、ClassNotFoundException: com.alibaba.fastjson.JSON 解决:添加依赖

<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>2.0.25</version>
</dependency>

2、NoClassDefFoundError: redis/clients/jedis/UnifiedJedis 解决:添加依赖

<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>4.3.1</version>
</dependency>

或者将jetcache版本降低至2.6.5以下。

这篇关于缓存框架jetcache的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/672496

相关文章

Java实现本地缓存的常用方案介绍

《Java实现本地缓存的常用方案介绍》本地缓存的代表技术主要有HashMap,GuavaCache,Caffeine和Encahche,这篇文章主要来和大家聊聊java利用这些技术分别实现本地缓存的方... 目录本地缓存实现方式HashMapConcurrentHashMapGuava CacheCaffe

如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)

《如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)》:本文主要介绍如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)问题,具有很好的参考价值,希望对大家有所帮助,如有... 目录先在你打算存放的地方建四个文件夹更改这四个路径就可以修改默认虚拟内存分页js文件的位置接下来从高级-

PyCharm如何更改缓存位置

《PyCharm如何更改缓存位置》:本文主要介绍PyCharm如何更改缓存位置的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录PyCharm更改缓存位置1.打开PyCharm的安装编程目录2.将config、sjsystem、plugins和log的路径

C++ HTTP框架推荐(特点及优势)

《C++HTTP框架推荐(特点及优势)》:本文主要介绍C++HTTP框架推荐的相关资料,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. Crow2. Drogon3. Pistache4. cpp-httplib5. Beast (Boos

JSR-107缓存规范介绍

《JSR-107缓存规范介绍》JSR是JavaSpecificationRequests的缩写,意思是Java规范提案,下面给大家介绍JSR-107缓存规范的相关知识,感兴趣的朋友一起看看吧... 目录1.什么是jsR-1072.应用调用缓存图示3.JSR-107规范使用4.Spring 缓存机制缓存是每一

Spring 缓存在项目中的使用详解

《Spring缓存在项目中的使用详解》Spring缓存机制,Cache接口为缓存的组件规范定义,包扩缓存的各种操作(添加缓存、删除缓存、修改缓存等),本文给大家介绍Spring缓存在项目中的使用... 目录1.Spring 缓存机制介绍2.Spring 缓存用到的概念Ⅰ.两个接口Ⅱ.三个注解(方法层次)Ⅲ.

Spring Boot 整合 Redis 实现数据缓存案例详解

《SpringBoot整合Redis实现数据缓存案例详解》Springboot缓存,默认使用的是ConcurrentMap的方式来实现的,然而我们在项目中并不会这么使用,本文介绍SpringB... 目录1.添加 Maven 依赖2.配置Redis属性3.创建 redisCacheManager4.使用Sp

springboot项目redis缓存异常实战案例详解(提供解决方案)

《springboot项目redis缓存异常实战案例详解(提供解决方案)》redis基本上是高并发场景上会用到的一个高性能的key-value数据库,属于nosql类型,一般用作于缓存,一般是结合数据... 目录缓存异常实践案例缓存穿透问题缓存击穿问题(其中也解决了穿透问题)完整代码缓存异常实践案例Red

SpringBoot基础框架详解

《SpringBoot基础框架详解》SpringBoot开发目的是为了简化Spring应用的创建、运行、调试和部署等,使用SpringBoot可以不用或者只需要很少的Spring配置就可以让企业项目快... 目录SpringBoot基础 – 框架介绍1.SpringBoot介绍1.1 概述1.2 核心功能2

Spring框架中@Lazy延迟加载原理和使用详解

《Spring框架中@Lazy延迟加载原理和使用详解》:本文主要介绍Spring框架中@Lazy延迟加载原理和使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、@Lazy延迟加载原理1.延迟加载原理1.1 @Lazy三种配置方法1.2 @Component