【问题标题】:Lagom: Caching external service callLagom:缓存外部服务调用
【发布时间】:2018-04-10 04:14:50
【问题描述】:

我正在实现一个 Lagom 服务,该服务使用 Swift Stack 的 Joss 客户端调用外部服务。如何缓存此信息,以免每次调用我的服务时都调用外部服务?

【问题讨论】:

    标签: caching lagom


    【解决方案1】:

    您可以使用任何缓存库,例如 ehcache/guava。当您第一次调用外部服务时,您会将数据放入缓存中,下次您会在缓存中找到数据并从那里填充响应。

    【讨论】:

      【解决方案2】:

      像这样使用 smth 来缓存 A 类的对象:

      @Singleton
      public class ACache {
      
          public final Cache<String, A> cache;
      
          public SplResultsCache() {
              this.cache = CacheBuilder.newBuilder().expireAfterWrite(60, TimeUnit.MINUTES).build();
          }
      
          public Cache<String, A> get(){
              return this.cache;
          }
      }
      

      您必须在您的模块中注册服务:

      bind(ACache.class).asEagerSingleton();
      

      然后将其注入到您的服务中:

      private SplResultsCache cache;
      
      public AService(ACache cache) {
          this.cache = cache;
      }
      

      最后你可以在 AService 的方法中使用它,像这样:

      A a = this.cache.get().getIfPresent(cacheKey);
      

      你当然可以重载缓存的方法来直接访问它们。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-21
        • 2015-12-28
        • 1970-01-01
        • 2021-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-15
        相关资源
        最近更新 更多