我正在使用Swift Stack的Joss客户端实现一个调用外部服务的Lagom服务。为了不在每次调用服务时都调用外部服务,我如何缓存这些信息?
发布于 2017-12-20 01:56:46
您可以使用任何缓存库,如ehcache/guava。当您第一次调用外部服务时,您将把数据放入缓存中,下一次,您将在缓存中找到数据,并从那里填充响应。
发布于 2018-04-10 04:14:51
像这样使用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);当然,您也可以重载缓存的方法来直接访问它们。
https://stackoverflow.com/questions/44256827
复制相似问题