Skip to content

Allow to dynamically provide TTL configuration #2587

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
wants to merge 5 commits into from

Conversation

Koooooo-7
Copy link
Contributor

@Koooooo-7 Koooooo-7 commented May 24, 2023

Fix issue: #1433 .

Give users a chance to change the ttl dynamically and give them the flexibility to do so.

  • You have read the Spring Data contribution guidelines.
  • You use the code formatters provided here and have them applied to your changes. Don’t submit any formatting related changes.
  • You submit test cases (unit or integration tests) that back your changes.
  • You added yourself as author in the headers of the classes you touched. Amend the date range in the Apache license header if needed. For new types, add the license header (copy from another file and set the current year only).

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 24, 2023
Copy link
Member

@mp911de mp911de left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a context-less supplier doesn't add much value here. I could well think of a case, where the TTL could be computed from the actual object.

How about a design of a TTL function (Function<Object, Duration>) where the object argument is provided by the actual cache value?

@@ -119,17 +121,17 @@ public static RedisCacheConfiguration defaultCacheConfig(@Nullable ClassLoader c

private final ConversionService conversionService;

private final Duration ttl;
private final Supplier<Duration> ttlProvider;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A Supplier doesn't make much sense as it just defers the decision on TTL without adding much contextual value.

@mp911de mp911de self-assigned this May 25, 2023
@Koooooo-7
Copy link
Contributor Author

Koooooo-7 commented May 26, 2023

Hi @mp911de , I agree with that we should provide some context for user to get the ttl and I did the updates.
And the changes:

  1. For now I provide the original key and value to users instead of createAndConvertCacheKey.
  2. About the public Duration getTtl() {} for backward compatibility, I user the Object.class as the default key instead.

@Koooooo-7 Koooooo-7 requested a review from mp911de May 26, 2023 07:08
@mp911de mp911de changed the title Allow to dynamically provide TTL configuration. Allow to dynamically provide TTL configuration May 26, 2023
@mp911de mp911de linked an issue May 26, 2023 that may be closed by this pull request
mp911de pushed a commit that referenced this pull request May 26, 2023
We now support a TtlFunction to compute the time to live for cache entries.

Closes #1433
Original pull request: #2587
mp911de added a commit that referenced this pull request 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
@mp911de mp911de added in: cache RedisCache and CacheManager type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels May 26, 2023
@mp911de
Copy link
Member

mp911de commented May 26, 2023

Thank you for your contribution. That's merged and polished now.

@mp911de mp911de closed this May 26, 2023
jxblum pushed a commit to jxblum/spring-data-redis that referenced this pull request 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 this pull request may close these issues.

Add TtlFunction to RedisCacheConfiguration for dynamic Time to Live
3 participants