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

Dynamic TTL #18

Open
caeus opened this issue Apr 25, 2022 · 5 comments
Open

Dynamic TTL #18

caeus opened this issue Apr 25, 2022 · 5 comments
Labels
enhancement New feature or request

Comments

@caeus
Copy link

caeus commented Apr 25, 2022

Is it possible to set the expiration time during execution of the loader?

Trying to cache some oauth tokens whose expiration time is known when the loader gets them. Not before.

@ychescale9 ychescale9 added the enhancement New feature or request label Apr 25, 2022
@glockbender
Copy link

It is such a good and useful enhancement and it would be awesome to have it in this lib.

@Shabinder
Copy link

we even have kotlinx.datetime which should make this simpler to impl for all platforms in commonMain only.

@ychescale9
Copy link
Member

Thanks everyone, I might take a look at this soon.

Can you share a few examples of your use cases and how you think the APIs should look like? Is this a feature you've used in other caching libraries in the past?

@glockbender
Copy link

glockbender commented Apr 25, 2023

Example of function description:

public data class LoaderContext internal constructor(
    var expireAfterAccessDuration: Duration,
    var expireAfterWriteDuration: Duration
)

public suspend fun get(key: Key, loader: suspend LoaderContext.() -> Value): Value
// looks like it should be a new 'put' function with 'customizing'
public suspend fun put(key: Key, loader: suspend LoaderContext.() -> Value)

Example of usage:

suspend fun cacheToken(key: String, ttl: Duration): Any {
   cache.put(key) {
      // by default cache-level configured values are set
      this.expireAfterAccessDuration = ttl
      // ...
   }
}

suspend fun someFun(key: String, ttl: Duration): Any {
   cache.get(key) {
      this.expireAfterWriteDuration = ttl
      // ...
   }
}

Is this a feature you've used in other caching libraries in the past?

Using it a lot with Redis

@caeus
Copy link
Author

caeus commented Feb 23, 2024

I'd add the feature as part of the builder:

dynamicExpireAfterWrite{ it.expires_in.seconds }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants