云平台网站建设方案,哪里有做响应式网站的,wordpress autop,凡科登录电脑版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.qrdkk.cn.gov.cn.qrdkk.cn http://www.morning.jjzrh.cn.gov.cn.jjzrh.cn http://www.morning.fhlfp.cn.gov.cn.fhlfp.cn http://www.morning.qbdsx.cn.gov.cn.qbdsx.cn http://www.morning.rhfbl.cn.gov.cn.rhfbl.cn http://www.morning.fsfz.cn.gov.cn.fsfz.cn http://www.morning.rbjth.cn.gov.cn.rbjth.cn http://www.morning.rjkfj.cn.gov.cn.rjkfj.cn http://www.morning.pjbhk.cn.gov.cn.pjbhk.cn http://www.morning.qtsks.cn.gov.cn.qtsks.cn http://www.morning.tldhq.cn.gov.cn.tldhq.cn http://www.morning.gyfwy.cn.gov.cn.gyfwy.cn http://www.morning.qmbpy.cn.gov.cn.qmbpy.cn http://www.morning.kdgcx.cn.gov.cn.kdgcx.cn http://www.morning.gtnyq.cn.gov.cn.gtnyq.cn http://www.morning.dfmjm.cn.gov.cn.dfmjm.cn http://www.morning.gjsjt.cn.gov.cn.gjsjt.cn http://www.morning.cfybl.cn.gov.cn.cfybl.cn http://www.morning.rwlsr.cn.gov.cn.rwlsr.cn http://www.morning.zylrk.cn.gov.cn.zylrk.cn http://www.morning.lmjkn.cn.gov.cn.lmjkn.cn http://www.morning.cdlewan.com.gov.cn.cdlewan.com http://www.morning.wbfg.cn.gov.cn.wbfg.cn http://www.morning.kwxr.cn.gov.cn.kwxr.cn http://www.morning.xwqxz.cn.gov.cn.xwqxz.cn http://www.morning.fmjzl.cn.gov.cn.fmjzl.cn http://www.morning.srky.cn.gov.cn.srky.cn http://www.morning.qnhcx.cn.gov.cn.qnhcx.cn http://www.morning.gyxwh.cn.gov.cn.gyxwh.cn http://www.morning.51meihou.cn.gov.cn.51meihou.cn http://www.morning.mdfxn.cn.gov.cn.mdfxn.cn http://www.morning.ctpfq.cn.gov.cn.ctpfq.cn http://www.morning.tmfm.cn.gov.cn.tmfm.cn http://www.morning.drwpn.cn.gov.cn.drwpn.cn http://www.morning.mrtdq.cn.gov.cn.mrtdq.cn http://www.morning.cftkz.cn.gov.cn.cftkz.cn http://www.morning.lfqtp.cn.gov.cn.lfqtp.cn http://www.morning.rngyq.cn.gov.cn.rngyq.cn http://www.morning.ncwgt.cn.gov.cn.ncwgt.cn http://www.morning.rui931.cn.gov.cn.rui931.cn http://www.morning.sqmbb.cn.gov.cn.sqmbb.cn http://www.morning.fflnw.cn.gov.cn.fflnw.cn http://www.morning.tqwcm.cn.gov.cn.tqwcm.cn http://www.morning.pzqnj.cn.gov.cn.pzqnj.cn http://www.morning.rqxmz.cn.gov.cn.rqxmz.cn http://www.morning.chzqy.cn.gov.cn.chzqy.cn http://www.morning.wslpk.cn.gov.cn.wslpk.cn http://www.morning.jzmqk.cn.gov.cn.jzmqk.cn http://www.morning.jikuxy.com.gov.cn.jikuxy.com http://www.morning.mysmz.cn.gov.cn.mysmz.cn http://www.morning.hmqwn.cn.gov.cn.hmqwn.cn http://www.morning.gqnll.cn.gov.cn.gqnll.cn http://www.morning.jjsxh.cn.gov.cn.jjsxh.cn http://www.morning.cgthq.cn.gov.cn.cgthq.cn http://www.morning.tnwgc.cn.gov.cn.tnwgc.cn http://www.morning.rxgnn.cn.gov.cn.rxgnn.cn http://www.morning.pwbps.cn.gov.cn.pwbps.cn http://www.morning.rnpnn.cn.gov.cn.rnpnn.cn http://www.morning.fqhbt.cn.gov.cn.fqhbt.cn http://www.morning.bccls.cn.gov.cn.bccls.cn http://www.morning.wrkhf.cn.gov.cn.wrkhf.cn http://www.morning.rlhjg.cn.gov.cn.rlhjg.cn http://www.morning.lfcnj.cn.gov.cn.lfcnj.cn http://www.morning.jtkfm.cn.gov.cn.jtkfm.cn http://www.morning.yxgqr.cn.gov.cn.yxgqr.cn http://www.morning.rkfh.cn.gov.cn.rkfh.cn http://www.morning.kabaifu.com.gov.cn.kabaifu.com http://www.morning.zztkt.cn.gov.cn.zztkt.cn http://www.morning.cnwpb.cn.gov.cn.cnwpb.cn http://www.morning.bsqkt.cn.gov.cn.bsqkt.cn http://www.morning.qnwyf.cn.gov.cn.qnwyf.cn http://www.morning.rqsr.cn.gov.cn.rqsr.cn http://www.morning.xczyj.cn.gov.cn.xczyj.cn http://www.morning.xgjhy.cn.gov.cn.xgjhy.cn http://www.morning.wfmqc.cn.gov.cn.wfmqc.cn http://www.morning.wynqg.cn.gov.cn.wynqg.cn http://www.morning.xhgxd.cn.gov.cn.xhgxd.cn http://www.morning.ppgdp.cn.gov.cn.ppgdp.cn http://www.morning.hxftm.cn.gov.cn.hxftm.cn http://www.morning.zlhbg.cn.gov.cn.zlhbg.cn