I just wonder if there is any caching solution available in Scala. I'm looking for something like it is provided by Guava in Java.
Should I use Guava too in Scala? Is there a wrapper / pimp in Scalaz or something similar? Any alternative more appropriate for Scala devs?
What Guava provides:
LoadingCache<Key, Graph> CACHE= CacheBuilder.newBuilder()
.maximumSize(1000)
.expireAfterWrite(10, TimeUnit.MINUTES)
.removalListener(MY_LISTENER)
.build(
new CacheLoader<Key, Graph>() {
public Graph load(Key key) throws AnyException {
return createExpensiveGraph(key);
}
});
Supplier<Animal> singleAnimalCache = Suppliers.memoizeWithExpiration(animalFromDbSupplier(), 365, TimeUnit.DAYS);
I need some basic cache management like in Guava.
Just adding an answer to plug my own project, but I recommend ScalaCache.
https://github.com/cb372/scalacache
Using guava caching is straightforward in Scala.
This is producing
I've added this to
build.sbt
:Would be interesting to transform the coding above to cb372's scalacache-guava. Could be even simpler/more standardized.
We had the same requirements and ended up building wrappers around Guava. We recently open-sourced parts of the library called Mango. If you don’t mind the extra dependency you can use it like
In Scalaz 7 there's
Memo
, which I covered a bit in learning Scalaz day 16.It's the first thing Adam Rosien covered in scalaz "For the Rest of Us" talk, so check that out too. He's using Scalaz 6.