Skip to content

Add TtlFunction to RedisCacheConfiguration for dynamic Time to Live #1433

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

Closed
spring-projects-issues opened this issue Aug 3, 2018 · 2 comments
Assignees
Labels
in: cache RedisCache and CacheManager type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link

Kyle Cronin opened DATAREDIS-858 and commented

RedisCache should allow sub classes to define the TTL duration based on the cache key or value for use cases where the TTL is not know at configuration time or could change depending on the value being stored. For example access tokens where the token provider specifies how long the token is valid for.

 

public class RedisCache {
   // default impementation delegates to cache config
   @NonNull protected Duration getTtl(Object key, Object value) { 
      return cacheConfig.getTtl(); 
   }
}

public class AccessTokenRedisCache extends RedisCache {
   @Override
   @NonNull
   protected Duration getTtl(Object key, Object value) {
      if (value instanceof AccessToken) {
         return ((AccessToken) value).getTtl();
      } else {
         return super.getTtl(key, value);
      }
   }
}

If there was a way to specify a TTL policy in the RedisCacheConfiguration that would be bonus


No further details from DATAREDIS-858

@spring-projects-issues
Copy link
Author

Mark Paluch commented

TTL can be configured via RedisCacheConfiguration and RedisCacheManagerBuilder on a per-cache basis. We currently do not support dynamic policies based on keys and values

@spring-projects-issues spring-projects-issues added in: cache RedisCache and CacheManager type: enhancement A general enhancement labels Dec 30, 2020
@nicdard
Copy link

nicdard commented Sep 22, 2022

Hi, is this issue still active? I have multiple use cases were I would like to randomise the TTL at each put operation, and I would like to rise a PR for this :)

@mp911de mp911de assigned mp911de and unassigned christophstrobl May 26, 2023
@mp911de mp911de added this to the 3.2 M1 (2023.1.0) milestone May 26, 2023
@mp911de mp911de linked a pull request May 26, 2023 that will close this issue
4 tasks
@mp911de mp911de changed the title Override cache TTL configuration [DATAREDIS-858] Add TtlFunction to RedisCacheConfiguration for dynamic Time to Live May 26, 2023
mp911de added a commit that referenced this issue May 26, 2023
Replace BiFunction with TtlFunction for a more concise definition. Deprecate RedisCacheConfiguration.getTtl in favor of Ttlfunction.
Enhance tests.

See #1433
Original pull request: #2587
jxblum pushed a commit to jxblum/spring-data-redis that referenced this issue Jun 8, 2023
We now support a TtlFunction to compute the time to live for cache entries.

Closes spring-projects#1433
Original pull request: spring-projects#2587
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: cache RedisCache and CacheManager type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants