SpringCloud(10):Hystrix请求缓存

news/2023/6/6 5:14:11

1 类继承的方法来实现请求缓存

1.1 编写CacheCommand类

package com.study.service.hystrix;import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixRequestCache;
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategyDefault;
import org.springframework.web.client.RestTemplate;public class CacheCommand extends HystrixCommand<String> {private Long cacheKey;private RestTemplate restTemplate;private Integer uid;public CacheCommand(Long cacheKey, RestTemplate restTemplate, Integer uid) {super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("cache-group")).andCommandKey(HystrixCommandKey.Factory.asKey("cache-test")));this.restTemplate = restTemplate;this.uid = uid;this.cacheKey = cacheKey;}@Overrideprotected String run() throws Exception {System.out.println("没有缓存,查询数据~!");String url = "http://study-user/user/{id}";String info = restTemplate.getForObject(url, String.class, uid);return info;}@Overrideprotected String getCacheKey() {return String.valueOf(cacheKey);}//清空缓存public void clearRequestCache(){HystrixRequestCache.getInstance(HystrixCommandKey.Factory.asKey("cache-test"), HystrixConcurrencyStrategyDefault.getInstance()).clear(String.valueOf(cacheKey));}
}

1.2 在OrderService中添加getUser2方法

    // 继承类的方式public String getUser2(Integer id) {Long cacheKey = 9999L;CacheCommand cacheCommand = new CacheCommand(9999L, restTemplate, id);String val = cacheCommand.execute();return val;}

1.3 在OrderContoller中编写测试接口

    @RequestMapping("/cache")public String cache() {HystrixRequestContext context = HystrixRequestContext.initializeContext();// 调用用户,查询用户信息,String result1 = orderService.getUser2(1);String result2 = orderService.getUser2(2);context.close();return "result1:" + result1 + ",result2:" + result2;}

访问接口测试,可以发现,在cachekey相同的情况下,返回的值是相同的

如果需要不同时,直接修改cachekey可以获取,如:

修改getUser2为

    // 继承类的方式public String getUser2(Integer id) {Long cacheKey = 9999L+id;CacheCommand cacheCommand = new CacheCommand(9999L, restTemplate, id);String val = cacheCommand.execute();return val;}

也可以直接调用清理缓存的方法,如下:

    public String getUser2(Integer id) {Long cacheKey = 9999L;CacheCommand cacheCommand = new CacheCommand(9999L, restTemplate, id);String val = cacheCommand.execute();// 清空request缓存cacheCommand.clearRequestCache();return val;}

2 注解方式实现请求缓存

2.1 在OrderService中添加getUser3方法以及clearRequestCache方法

    //请求缓存注解@CacheResult@HystrixCommand(commandKey = "cache-user")public String getUser3(Integer id, @CacheKey Long cacheKey) {String url = "http://study-user/user/{id}";String info = restTemplate.getForObject(url, String.class, id);return info;}//缓存清除注解@CacheRemove(commandKey = "cache-user")@HystrixCommandpublic void clearRequestCache(@CacheKey Long cacheKey) {}

2.2 在OrderContoller中编写测试接口

    @RequestMapping("/cache2")public String cache2() {HystrixRequestContext context = HystrixRequestContext.initializeContext();// 调用用户,查询用户信息,String result1 = orderService.getUser3(1, 12345L);String result2 = orderService.getUser3(2, 12345L);context.close();return "cache2 result1:" + result1 + ", cache2 result2:" + result2;}

如果要清除缓存,直接调用clearRequestCache方法。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.exyb.cn/news/show-4561933.html

如若内容造成侵权/违法违规/事实不符,请联系郑州代理记账网进行投诉反馈,一经查实,立即删除!

相关文章

任正非:股权激励拯救了华为!

华为技术有限公司是一家生产销售通信设备的民营通信科技公司&#xff0c;于1987年在中国深圳正式注册成立&#xff0c;总部位于中国广东省深圳市。华为的产品和解决方案已经应用于全球170多个国家&#xff0c;服务全球1/3以上的人口&#xff0c;2018年全球销售业绩7000多亿&…

华为故事

2019独角兽企业重金招聘Python工程师标准>>> 在华为开始创业的20世纪80年代中后期&#xff0c;国内诞生了400多家通信制造类企业&#xff0c;但这个行业注定是场死亡竞赛&#xff0c;赢者一定是死得最晚的那个。华为活到了最后。华为为什么可以活到最后? 这确实是一…

【股权方案】公司股权分配员工持股方案学习资料(39份)

公司股权分配员工持股方案学习资料&#xff08;39份&#xff09; XX股份有限公司员工持股计划方案(构思稿).doc XX集团员工持股总体方案设计讨论稿(doc-15).doc 一张表看懂员工持股计划与股权激励的区别.doc 公司内部职员入股协议书.doc 员工入股-企业员工入股持股方案(三江集团…

SpringAMQP - Work Queue 工作消息队列

目录 介绍 案例 测试 改进&#xff1a; 介绍 Work queues也被称为&#xff08;Task queues&#xff09;&#xff0c;任务模型简单来说就是让多个消费者绑定到一个队列&#xff0c;共同消费队列中的消息当消息处理比较耗时的时候&#xff0c;可能生产消息的速度会远远大于消…

华为拿数百亿资金给员工分红,2021年每股可分1.58元

程序员求职面试&#xff08;ID&#xff1a;CoderJob&#xff09;整理内容参考自&#xff1a;快科技、雷锋网等2月6日&#xff0c;从华为内部论坛了解到&#xff0c;华为在1月底公布了分红数据&#xff0c;多位华为内部人士称&#xff0c;2021年&#xff0c;虽然华为仍处困难时期…

华为业绩继续逆势增长,给员工分红400亿

媒体引述来自华为内部财务系统数据指出华为在2020年取得营收、净利润增长超一成&#xff0c;同时华为公司高调宣布将给员工分红400亿&#xff0c;显示出这家企业在面临众所周知的困难之下依然取得了可喜的成绩。媒体引述的数据指出2020年华为的营收超过1300亿美元&#xff0c;净…

利率掉期 (IRS) 中的超级细节

最近有读者问人民币 FR007 掉期的估值总是和系统上对不上&#xff0c;加上 RFR 代替 IBOR 后有新的 RFR 掉期出现&#xff0c;其估值方法中有很多细节。以上两种产品都可以叫做利率掉期。笔者想趁着写《金融工程》一书的利率掉期这章时&#xff0c;顺便给大家做一个估值利率掉期…