真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

SpringCloud(10):Hystrix請(qǐng)求緩存-創(chuàng)新互聯(lián)

1 類繼承的方法來實(shí)現(xiàn)請(qǐng)求緩存

1.1?編寫CacheCommand類

創(chuàng)新互聯(lián)專注于東臺(tái)網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供東臺(tái)營銷型網(wǎng)站建設(shè),東臺(tái)網(wǎng)站制作、東臺(tái)網(wǎng)頁設(shè)計(jì)、東臺(tái)網(wǎng)站官網(wǎng)定制、小程序設(shè)計(jì)服務(wù),打造東臺(tái)網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供東臺(tái)網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
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("沒有緩存,查詢數(shù)據(jù)~!");
        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中編寫測(cè)試接口

@RequestMapping("/cache")
    public String cache() {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();

        // 調(diào)用用戶,查詢用戶信息,
        String result1 = orderService.getUser2(1);
        String result2 = orderService.getUser2(2);

        context.close();

        return "result1:" + result1 + ",result2:" + result2;
    }

訪問接口測(cè)試,可以發(fā)現(xiàn),在cachekey相同的情況下,返回的值是相同的

如果需要不同時(shí),直接修改cachekey可以獲取,如:

修改getUser2為

// 繼承類的方式
    public String getUser2(Integer id) {

        Long cacheKey = 9999L+id;
        CacheCommand cacheCommand = new CacheCommand(9999L, restTemplate, id);
        String val = cacheCommand.execute();


        return val;
    }

也可以直接調(diào)用清理緩存的方法,如下:

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 注解方式實(shí)現(xiàn)請(qǐng)求緩存

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

//請(qǐng)求緩存注解
    @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中編寫測(cè)試接口

@RequestMapping("/cache2")
    public String cache2() {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();

        // 調(diào)用用戶,查詢用戶信息,
        String result1 = orderService.getUser3(1, 12345L);
        String result2 = orderService.getUser3(2, 12345L);

        context.close();

        return "cache2 result1:" + result1 + ", cache2 result2:" + result2;
    }

如果要清除緩存,直接調(diào)用clearRequestCache方法。

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧


網(wǎng)頁名稱:SpringCloud(10):Hystrix請(qǐng)求緩存-創(chuàng)新互聯(lián)
當(dāng)前鏈接:http://weahome.cn/article/dsdohi.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部