0

I am implementing a Lagom service that calls an external service using the Joss client of Swift Stack. How can I cache this info in order to don't call the external service every time my service is called?

gilcu2
  • 343
  • 3
  • 13

2 Answers2

0

You can use any caching library like ehcache/guava. When you call your external service for the first time you would put data into the cache and next time onwards you would find the data in cache and populate the response from there.

0

Use smth like this to cache objects of Class 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;
    }
}

You must register the service in your Module with this:

bind(ACache.class).asEagerSingleton();

Then inject it into your service:

private SplResultsCache cache;

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

And finally you can use it in methods of the AService, like this:

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

You may of course overload methods of the cache to access them directly.

monad
  • 125
  • 1
  • 7