【问题标题】:Netflix hystrix shared request cachingNetflix hystrix 共享请求缓存
【发布时间】:2016-03-25 07:07:42
【问题描述】:

我正在使用基于 Netflix OSS 架构的 Spring Cloud。我需要缓存我使用 Get-Set-Get 模式的方法之一的结果。

 public class UserService {    
    @CacheResult
    @HystrixCommand
    public User getUserById(@CacheKey String id) { // GET
        return storage.get(id);
    }

    @CacheRemove(commandKey = "getUserById")
    @HystrixCommand
    public void update(@CacheKey("id") User user) { // SET
        storage.put(user.getId(), user);
    }
}    

现在我的问题很简单。我需要知道如果我运行了该服务的多个实例,是否可以使用分布式缓存?如果是,如果大小增加,我可以使用 Redis 之类的东西吗?

【问题讨论】:

    标签: caching spring-cloud microservices netflix hystrix


    【解决方案1】:

    是的,见docs

    如何实现示例:

    public class UserService extends HystrixCommand<Boolean> {
         private final User user;
    
         public User getUserById(String id) {
            super(HystrixCommandGroupKey.Factory.asKey("AnyUser"));
            user = storage.get(id);
            return user;
        }
    
        public void update(User user) { // SET
            super(HystrixCommandGroupKey.Factory.asKey("AnyUser"));
            this.user = user;
            storage.put(user.getId(), user);
        }
    
        @Override
        protected Boolean run() {
            return user == null || (user != null && user.id == 0);
        }
    
        @Override
        protected String getCacheKey() {            
            return (user != null ) ?String.valueOf(user.id):"";
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-22
      • 2016-02-06
      • 2020-11-27
      • 2014-05-20
      • 2018-08-08
      • 2017-09-10
      • 1970-01-01
      • 2011-01-04
      • 2023-03-07
      相关资源
      最近更新 更多