zoukankan      html  css  js  c++  java
  • hystrix 结果缓存机制(5)

    hystrix支持将一个请求结果缓存起来,下一个具有相同key的请求将直接从缓存中取出结果,减少请求开销。要使用hystrix cache功能

    第一个要求是重写getCacheKey(),用来构造cache key;

    第二个要求是构建context,如果请求B要用到请求A的结果缓存,A和B必须同处一个context。

    通过HystrixRequestContext.initializeContext()context.shutdown()可以构建一个context,这两条语句间的所有请求都处于同一个context。
    测试代码如下:

    HystrixCommandCache.java

    public class HystrixCommandCache extends HystrixCommand<Boolean>{
    	private final int value;
        private final String value1;
    
        protected HystrixCommandCache(int value, String value1) {
            super(HystrixCommandGroupKey.Factory.asKey("RequestCacheCommandGroup"));
            this.value = value;
            this.value1 = value1;
        }
    
        // 返回结果是cache的value
        @Override
        protected Boolean run() {
            return value == 0 || value % 2 == 0;
        }
    
        // 构建cache的key
        @Override
        protected String getCacheKey() {
            return String.valueOf(value) + value1;
    	}
    
    }
    

     HystrixCommandCacheTest.java

    public class HystrixCommandCacheTest {
    	
      @Test
      public void testWithoutCacheHits() {
          HystrixRequestContext context = HystrixRequestContext.initializeContext();
          try {
              assertTrue(new HystrixCommandCache(2,"HLX").execute());
              assertFalse(new HystrixCommandCache(1,"HLX").execute());
              assertTrue(new HystrixCommandCache(0,"HLX").execute());
              assertTrue(new HystrixCommandCache(58672,"HLX").execute());
          } finally {
              context.shutdown();
          }
      }
    
      @Test
      public void testWithCacheHits() {
          HystrixRequestContext context = HystrixRequestContext.initializeContext();
          try {
        	  HystrixCommandCache command2a = new HystrixCommandCache(2,"HLX");
        	  HystrixCommandCache command2b = new HystrixCommandCache(2,"HLX");
        	  HystrixCommandCache command2c = new HystrixCommandCache(2,"HLX1");
    
              assertTrue(command2a.execute());
              // 第一次执行,不应该是从缓存中获取
              assertFalse(command2a.isResponseFromCache());
    
              assertTrue(command2b.execute());
              // 第二次执行,通过isResponseFromCache()方法判断是否是从缓存中获取的
              assertTrue(command2b.isResponseFromCache());
              
              //虽然是第三次执行,但是getCacheKey()的缓存key值不一样,依然无法从缓存中获取
              assertTrue(command2c.execute());
              assertFalse(command2c.isResponseFromCache());
          } finally {
              context.shutdown();
          }
    
          // 开启一个新的context
          context = HystrixRequestContext.initializeContext();
          try {
        	  HystrixCommandCache command3a = new HystrixCommandCache(2,"HLX");
        	  HystrixCommandCache command3b = new HystrixCommandCache(2,"HLX");
              assertTrue(command3a.execute());
              // 第一次请求,不应该从缓存中获取
              assertFalse(command3a.isResponseFromCache());
    
              //没有执行excute(),isResponseFromCache()永远返回是true
              assertFalse(command3b.isResponseFromCache());
          } finally {
              context.shutdown();
          }
      }
    }
    

    以测试demo的testWithCacheHits()为例,command2acommand2bcommand2c同处一个context,前两者的cache key都是2HLX(见getCacheKey()),所以command2a执行完后把结果缓存,command2b执行时就不走run()而是直接从缓存中取结果了,而command2c的cache key是2HLX1,无法从缓存中取结果。

    注:isResponseFromCache()方法用于检测是否是从缓存中获取;

    参考文献:http://www.jianshu.com/p/b9af028efebb

  • 相关阅读:
    Ubuntn16.04+OpenCV3.1+CUDA8.0+cudnn5.1+caffe配置及问题集锦
    理解最短路径-Dijkstra算法
    使用git命令从github上clone项目
    Vscode中问题
    windows和ubuntn互传文件
    Python中的一些模块用法
    机器学习中矩阵的求导知识
    训练集,验证集,测试集
    Javascript、Jquery获取浏览器和屏幕各种高度宽度
    DTCMS规格统一赋值
  • 原文地址:https://www.cnblogs.com/cowboys/p/7680766.html
Copyright © 2011-2022 走看看