'scalacache: Multiple regions/caches
I have a repository class and I want to create 2 different cache regions for 2 different types of queries. Let's say that we have typea
andtypeab
. I am using scalacache
with caffeine
as the underlying implementation. Based on the docs, one has to configure it like so:
lazy val underlyingTypeACaffeineCache = Caffeine
.newBuilder()
.maximumSize(CACHE_SIZE)
.build[String, Entry[List[Event]]]
implicit lazy val customisedTypeACaffeineCache: CaffeineCache[List[Event]] =
CaffeineCache(underlyingTypeACaffeineCache)
and you should call it like
sync.caching("key")(ttl=None){...}
This sync
interface picks up the implicit and caches the values appropriately. What happens when you want to have different regions though under the same roof/class. For example
lazy val underlyingTypeACaffeineCache = Caffeine
.newBuilder()
.maximumSize(CACHE_SIZE)
.build[String, Entry[List[Event]]]
implicit lazy val customisedTypeACaffeineCache: CaffeineCache[List[Event]] =
CaffeineCache(underlyingTypeACaffeineCache)
lazy val underlyingTypeBCaffeineCache = Caffeine
.newBuilder()
.maximumSize(CACHE_SIZE)
.build[String, Entry[List[Event]]]
implicit lazy val customisedTypeBCaffeineCache: CaffeineCache[List[String]] =
CaffeineCache(underlyingTypeBCaffeineCache)
then the sync
interface does not work since it gets confused as to which implicit to use. How do you overcome this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|