专业从事成都网站建设,地方网站盈利,汽车用品网站规划,wordpress怎样修改备案号0. 引言
之前我们讲解了本地缓存ehcache组件#xff0c;在实际应用中#xff0c;并不是单一的使用本地缓存或者redis#xff0c;更多是组合使用来满足不同的业务场景#xff0c;于是如何优雅的组合本地缓存和远程缓存就成了我们要研究的问题#xff0c;而这一点#xff…0. 引言
之前我们讲解了本地缓存ehcache组件在实际应用中并不是单一的使用本地缓存或者redis更多是组合使用来满足不同的业务场景于是如何优雅的组合本地缓存和远程缓存就成了我们要研究的问题而这一点阿里开源的jetcache组件帮我们实现了
1. jetcache简介
jetcache是阿里开源的基于java开发的缓存框架支持多种缓存类型本地缓存、分布式缓存、多级缓存。能够满足不同业务场景的缓存需求。
jetcache具有上手简单、性能高效、拓展性强的特点。支持缓存预热 、缓存key前缀等功能。结合spring-cache使用可以实现十分优雅的缓存类型切换
官网地址https://github.com/alibaba/jetcache 官方文档https://github.com/alibaba/jetcache/tree/master/docs/CN
2. jetcache使用
1、引入依赖这里我们使用sringboot项目框架同时使用redis作为远程缓存。于是我们引入jetcache-starter-redis依赖这里我的springboot版本为2.6.13
如果是非springboot项目可以参考官网说明配置 dependencygroupIdcom.alicp.jetcache/groupIdartifactIdjetcache-starter-redis/artifactIdversion2.7.0/version
/dependency!-- jetcache2.7.x版本需要额外添加该依赖--
dependencygroupIdredis.clients/groupIdartifactIdjedis/artifactIdversion4.3.1/version
/dependency对应的版本说明如下springboot与jetcache版本关系 2、修改配置文件配置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更详细的参数配置可参考官网说明https://github.com/alibaba/jetcache/blob/master/docs/CN/Config.md 3、启动类添加注解EnableCreateCacheAnnotation开启缓存添加EnableMethodCache(basePackages com.example.jetcachedemo)注解配置缓存方法扫描路径
4、使用缓存可以通过三种方式
方式一推荐AOP模式通过Cached,CacheUpdate,CacheInvalidate注解
RestController
RequestMapping(user)
public class UserController {GetMapping(getRemote)Cached(nameuserCache:, key #id, expire 3600, timeUnit TimeUnit.SECONDS, cacheType CacheType.REMOTE)public User getRemote(Long id){// 直接新建用户模拟从数据库获取数据User user new User();user.setId(id);user.setName(用户remoteid);user.setAge(23);user.setSex(1);System.out.println(第一次获取数据未走缓存id);return user;}GetMapping(getLocal)Cached(nameuserCache:, key #id, expire 3600, timeUnit TimeUnit.SECONDS, cacheType CacheType.LOCAL)public User getLocal(Long id){// 直接新建用户模拟从数据库获取数据User user new User();user.setId(id);user.setName(用户localid);user.setAge(23);user.setSex(1);System.out.println(第一次获取数据未走缓存id);return user;}GetMapping(getBoth)Cached(nameuserCache:, key #id, expire 3600, timeUnit TimeUnit.SECONDS, cacheType CacheType.BOTH)public User getBoth(Long id){// 直接新建用户模拟从数据库获取数据User user new User();user.setId(id);user.setName(用户bothid);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;
}方式二 API模式通过CreateCache注在jetcache 2.7 版本CreateCache注解已废弃不推荐使用
RestController
RequestMapping(user2)
public class User2Controller {CreateCache(name userCache:, expire 3600, timeUnit TimeUnit.SECONDS, cacheType CacheType.BOTH)private CacheLong, 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(用户bothid);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;}}方式三 高级API模式通过CacheManager2.7 版本才可使用
1添加依赖
dependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion2.0.25/version
/dependency2书写配置类
Configuration
public class JetcacheConfig {Autowiredprivate CacheManager cacheManager;private CacheLong, 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 CacheLong, Object getUserCache(){return userCache;}
}3调用代码
RestController
RequestMapping(user3)
public class User3Controller {AutowiredJetcacheConfig jetcacheConfig;Autowiredprivate CacheLong, 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(用户bothid);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;}}多级缓存的形式会先从本地缓存获取数据本地获取不到会从远程缓存获取
5、启动redis启动演示项目 注意如果启动出现NoClassDefFoundError: redis/clients/util/Pool或NoClassDefFoundError: redis/clients/jedis/UnifiedJedis报错说明springboot与jetcache版本不一致对应关系可参考上述第一步中的说明 同时如果使用的是jetcache2.7.x版本因为该版本中有jedis包的依赖需要额外添加如下依赖或者将jetcache版本将至2.6.5以下 dependencygroupIdredis.clients/groupIdartifactIdjedis/artifactIdversion4.3.1/version
/dependency3. 测试
3.1 方式一测试
1、访问localhost:8088/user/getRemote?id1 因为配置的是远程缓存在redis中也能看到对应的key 2、访问localhost:8088/user/getLocal?id1这个方法是从本地缓存获取的现在只有远程缓存上有数据我们调用发现缓存数据还是拿到了这说明当我们在配置文件中配置了本地缓存和远程缓存后方式一中本地缓存和远程缓存会自动相互调用
比如本地缓存有这个keyredis中没有通过远程缓存方式访问时会先从redis获取如果没有会自动获取本地缓存但是数据还是存储在本地缓存并不会同步到redis上这样更加灵活的实现了多级缓存架构 3.2 方式二测试
1、再测试下CreateCache的形式localhost:8088/user2/get?id4 正常获取了并且redis中也有了对应的值 而当我们把缓存方式更改为LOCAL后再访问localhost:8088/user2/get?id5
CreateCache(name userCache:, expire 3600, timeUnit TimeUnit.SECONDS, cacheType CacheType.LOCAL)会发现redis中就没有对应缓存了只在本地缓存存在说明我们指定本地缓存的形式成功了 3.3 方式三测试
1、调用localhost:8088/user3/get?id11 redis中缓存设置成功 4. 常见报错
1、 ClassNotFoundException: com.alibaba.fastjson.JSON 解决添加依赖
dependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion2.0.25/version
/dependency2、NoClassDefFoundError: redis/clients/jedis/UnifiedJedis 解决 添加依赖
dependencygroupIdredis.clients/groupIdartifactIdjedis/artifactIdversion4.3.1/version
/dependency或者将jetcache版本降低至2.6.5以下
演示源码
https://gitee.com/wuhanxue/wu_study/tree/master/demo/jetcache-demo 文章转载自: http://www.morning.tytly.cn.gov.cn.tytly.cn http://www.morning.gyylt.cn.gov.cn.gyylt.cn http://www.morning.rqxch.cn.gov.cn.rqxch.cn http://www.morning.kmcfw.cn.gov.cn.kmcfw.cn http://www.morning.rrjzp.cn.gov.cn.rrjzp.cn http://www.morning.nkmw.cn.gov.cn.nkmw.cn http://www.morning.mqffm.cn.gov.cn.mqffm.cn http://www.morning.hqgkx.cn.gov.cn.hqgkx.cn http://www.morning.bnfrj.cn.gov.cn.bnfrj.cn http://www.morning.tpnx.cn.gov.cn.tpnx.cn http://www.morning.srjgz.cn.gov.cn.srjgz.cn http://www.morning.wqfzx.cn.gov.cn.wqfzx.cn http://www.morning.tmlhh.cn.gov.cn.tmlhh.cn http://www.morning.ljbpk.cn.gov.cn.ljbpk.cn http://www.morning.gfznl.cn.gov.cn.gfznl.cn http://www.morning.prmyx.cn.gov.cn.prmyx.cn http://www.morning.tzmjc.cn.gov.cn.tzmjc.cn http://www.morning.ppwdh.cn.gov.cn.ppwdh.cn http://www.morning.fhghy.cn.gov.cn.fhghy.cn http://www.morning.gqcd.cn.gov.cn.gqcd.cn http://www.morning.mfmrg.cn.gov.cn.mfmrg.cn http://www.morning.nrbcx.cn.gov.cn.nrbcx.cn http://www.morning.fncgw.cn.gov.cn.fncgw.cn http://www.morning.rongxiaoman.com.gov.cn.rongxiaoman.com http://www.morning.zmtrk.cn.gov.cn.zmtrk.cn http://www.morning.hhxwr.cn.gov.cn.hhxwr.cn http://www.morning.plqqp.cn.gov.cn.plqqp.cn http://www.morning.yubkwd.cn.gov.cn.yubkwd.cn http://www.morning.nkqrq.cn.gov.cn.nkqrq.cn http://www.morning.nfpgc.cn.gov.cn.nfpgc.cn http://www.morning.xsbhg.cn.gov.cn.xsbhg.cn http://www.morning.zlhbg.cn.gov.cn.zlhbg.cn http://www.morning.rszwc.cn.gov.cn.rszwc.cn http://www.morning.rgyts.cn.gov.cn.rgyts.cn http://www.morning.frpfk.cn.gov.cn.frpfk.cn http://www.morning.yzxhk.cn.gov.cn.yzxhk.cn http://www.morning.rydhq.cn.gov.cn.rydhq.cn http://www.morning.lkwyr.cn.gov.cn.lkwyr.cn http://www.morning.lwyqd.cn.gov.cn.lwyqd.cn http://www.morning.blfgh.cn.gov.cn.blfgh.cn http://www.morning.lgrkr.cn.gov.cn.lgrkr.cn http://www.morning.zhishizf.cn.gov.cn.zhishizf.cn http://www.morning.cqyhdy.cn.gov.cn.cqyhdy.cn http://www.morning.bhjyh.cn.gov.cn.bhjyh.cn http://www.morning.nxbsq.cn.gov.cn.nxbsq.cn http://www.morning.nhpmn.cn.gov.cn.nhpmn.cn http://www.morning.rttkl.cn.gov.cn.rttkl.cn http://www.morning.rfzzw.com.gov.cn.rfzzw.com http://www.morning.snygg.cn.gov.cn.snygg.cn http://www.morning.wrfk.cn.gov.cn.wrfk.cn http://www.morning.dhnqt.cn.gov.cn.dhnqt.cn http://www.morning.hwprz.cn.gov.cn.hwprz.cn http://www.morning.ntwfr.cn.gov.cn.ntwfr.cn http://www.morning.cbnlg.cn.gov.cn.cbnlg.cn http://www.morning.ztdlp.cn.gov.cn.ztdlp.cn http://www.morning.grlth.cn.gov.cn.grlth.cn http://www.morning.kzyr.cn.gov.cn.kzyr.cn http://www.morning.bhmnp.cn.gov.cn.bhmnp.cn http://www.morning.lmhcy.cn.gov.cn.lmhcy.cn http://www.morning.lkcqz.cn.gov.cn.lkcqz.cn http://www.morning.pwbps.cn.gov.cn.pwbps.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.jklns.cn.gov.cn.jklns.cn http://www.morning.xctdn.cn.gov.cn.xctdn.cn http://www.morning.lyhry.cn.gov.cn.lyhry.cn http://www.morning.bnfsw.cn.gov.cn.bnfsw.cn http://www.morning.cljpz.cn.gov.cn.cljpz.cn http://www.morning.dbfp.cn.gov.cn.dbfp.cn http://www.morning.xrrbj.cn.gov.cn.xrrbj.cn http://www.morning.flfdm.cn.gov.cn.flfdm.cn http://www.morning.ygmw.cn.gov.cn.ygmw.cn http://www.morning.mrskk.cn.gov.cn.mrskk.cn http://www.morning.mwzt.cn.gov.cn.mwzt.cn http://www.morning.zxcny.cn.gov.cn.zxcny.cn http://www.morning.prmbn.cn.gov.cn.prmbn.cn http://www.morning.tkxyx.cn.gov.cn.tkxyx.cn http://www.morning.hbywj.cn.gov.cn.hbywj.cn http://www.morning.npgwb.cn.gov.cn.npgwb.cn http://www.morning.qgmwt.cn.gov.cn.qgmwt.cn http://www.morning.qydgk.cn.gov.cn.qydgk.cn