Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow queries to be excluded from particular caches #3540

Closed
davidalbers opened this issue Nov 11, 2021 · 4 comments
Closed

Allow queries to be excluded from particular caches #3540

davidalbers opened this issue Nov 11, 2021 · 4 comments

Comments

@davidalbers
Copy link

davidalbers commented Nov 11, 2021

Is your feature request related to a problem? Please describe.
I have an in-memory LRU cache chained to an on-disk SQL cache:

LruNormalizedCacheFactory(evictionPolicy = evictionPolicy).chain(SqlNormalizedCacheFactory(applicationContext))

Most of the time I want this because I want most queries saved to disk. However, there are some queries I only want in the in-memory cache.

As an example, we show informational banners in our app. It makes sense to cache those in-memory so they don't constantly reload when navigating between screens. However, it's not necessary to keep them on disk since the information changes frequently. In fact, it could be a bug if the user sees a cached notification from 6 months ago.

Describe the solution you'd like
I'd like a cache header that skips the SqlNormalizedCacheFactory. I'm thinking along the lines of DO_NOT_STORE but more specific

val cacheHeaders = CacheHeaders.builder()
    .addHeader(DO_NOT_STORE, SqlNormalizedCache.key)
    .build()
client.query(exampleQuery).toBuilder()
    .cacheHeaders(cacheHeaders)
    .build()

Though I could see a few different solutions here and open to anything that helps the original problem.

@BoD
Copy link
Contributor

BoD commented Nov 12, 2021

I'm thinking out loud here but I'm wondering if the ability to set an expiration date on cached models would also be a possible path to achieve your use-case?

@davidalbers
Copy link
Author

Yes, that could work too. This is a good point since it clarifies what I'm looking for.
The root issue is these "timely" notifications shouldn't be kept permanently while other queries should be kept.

@martinbonnin
Copy link
Contributor

Expiration definitely sounds like something that would help here. That's on the roadmap but requires some work though. In the short term, you can always implement your own NormalizedCache that has custom headers. Something like:

  class MyCache(val delegate: NormalizedCache): NormalizedCache() {
    override fun merge(record: Record, cacheHeaders: CacheHeaders): Set<String> {
      if (cacheHeaders.hasHeader("DO_NOT_STORE_SQLITE")) {
        return emptySet()
      }
      return delegate.merge(record, cacheHeaders)
    }

    override fun merge(records: Collection<Record>, cacheHeaders: CacheHeaders): Set<String> {
      if (cacheHeaders.hasHeader("DO_NOT_STORE_SQLITE")) {
        return emptySet()
      }
      return delegate.merge(records, cacheHeaders)
    }
  // ...
}

@martinbonnin
Copy link
Contributor

martinbonnin commented Nov 17, 2021

I'm going to close this one as the solution above should work for the specific problem of exclude some queries from the SQLite cache.

For the more general expiration problem, I just opened #3566

@davidalbers let us know if you want to dig more into excluding queries and I'll reopen this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants