Skip to content

Reset Caches by flushing DB#3314

Closed
christophstrobl wants to merge 3 commits intomainfrom
issue/3290
Closed

Reset Caches by flushing DB#3314
christophstrobl wants to merge 3 commits intomainfrom
issue/3290

Conversation

@christophstrobl
Copy link
Copy Markdown
Member

No description provided.

Copy link
Copy Markdown
Contributor

@onobc onobc left a comment

Choose a reason for hiding this comment

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

Thanks @christophstrobl . I have added some comments / questions.

/**
* @author Christoph Strobl
*/
interface CacheWriterOperation<T> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can be a @FunctionalInterface.

/**
* @author Christoph Strobl
*/
public abstract class ResetCachesStrategies {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this may read better as CacheResetStrategy/ies - wdyt?

Uggh, naming... always hard.


private <T> T execute(String name, Function<RedisConnection, T> callback) {
@Override
public <T> T execute(Function<RedisConnection, T> callback) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I am sure this will clear up w/ javadocs once we get firm on the direction, but it is currently not clear that the newly added execute method does not do the potential wait for unlocked (i.e. checkAndPotentiallyWaitUntilUnlocked).

return execute(null, callback);
}

private <T> T execute(@Nullable String name, Function<RedisConnection, T> callback) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We could just call executeLockFree instead of adding the @Nullable here.

@Override
public void resetCaches() {

if(resetCachesStrategy instanceof CacheWriterOperation<?> operation) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My first thought was to abstract the impl down into the strategy via a resetCaches method on the strategy where the strategy could take in a cache writer and a cache manager. The flushing variant could use the cache writer and the default variant could use the CM resetCaches impl. However, the latter would call back into CacheManager.resetCaches and that would infinite recurse (i.e. not get to super.resetCaches).

Just putting my thoughts - I am not sure which direction to go but I do like the thought of the strategy handling the work.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I am sure I am missing something, but it seems that we are overlapping w/ current cache cleaning impl in BatchStrategy (keys and scan). Seems like we could just add another strategy for flushAsync().

@Override
public String doWithCacheWriter(RedisCacheWriter cacheWriter) {
return cacheWriter.execute(connection -> {
connection.serverCommands().flushDb(FlushOption.ASYNC);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there any other cleanup / stats we are missing that the super.resetCaches may be doing that we should do here as well?

/**
* @author Christoph Strobl
*/
public abstract class ResetCachesStrategies {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about just ResetStrategy? Cache defines clear and evict methods and reset is distinct from these (thus it ranges semantically in a similar space).

Design-wise, it could be an interface with two static methods, sequential() (the default) and flushDb(). I like the CacheWriterOperation approach as it repeats how we approach functional composition in other parts of the framework.

@mp911de mp911de self-assigned this Mar 23, 2026
@mp911de mp911de added the type: enhancement A general enhancement label Mar 23, 2026
Rename ResetCachesStrategies to ResetStrategy, introduce clear and invalidate strategies. Add tests and docs.
@mp911de mp911de marked this pull request as ready for review March 23, 2026 14:29
@mp911de mp911de changed the title Draft: Reset Caches by flushing DB Reset Caches by flushing DB Mar 23, 2026
@mp911de mp911de linked an issue Mar 30, 2026 that may be closed by this pull request
@mp911de mp911de added this to the 4.1 RC1 (2026.0.0) milestone Mar 30, 2026
mp911de pushed a commit that referenced this pull request Mar 30, 2026
We now support configuration of cache reset strategies in RedisCacheManager. Reset strategies control whether `CacheManager.reset()` sequentially clears the cache or whether to use FLUSHDB to wipe a Redis database entirely.

Closes #3290
Original pull request: #3314
mp911de added a commit that referenced this pull request Mar 30, 2026
Rename ResetCachesStrategies to ResetStrategy, introduce clear and invalidate strategies. Add tests and docs.

See #3290
Original pull request: #3314
@mp911de mp911de closed this Mar 30, 2026
@mp911de mp911de deleted the issue/3290 branch March 30, 2026 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support CacheManager.resetCaches() optimization

4 participants