Lagom:缓存外部服务电话

问题描述 投票:0回答:2

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

caching lagom
2个回答
0
投票

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


0
投票

像这样使用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);

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

© www.soinside.com 2019 - 2024. All rights reserved.