Skip to content

scalacache: Create multiple cache regions #658

@nikoplusx

Description

@nikoplusx

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions