SpringCloud(10):Hystrix请求缓存-创新互联

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{

    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;
    }

    @Override
    protected String run() throws Exception {
        System.out.println("没有缓存,查询数据~!");
        String url = "http://study-user/user/{id}";
        String info = restTemplate.getForObject(url, String.class, uid);
        return info;
    }

    @Override
    protected 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")
    @HystrixCommand
    public 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方法。

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


标题名称:SpringCloud(10):Hystrix请求缓存-创新互联
网页网址:http://pwwzsj.com/article/dsdohi.html