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

Feature Request : method to flush all caches #121

Closed
ErikPilsits-RJW opened this issue Nov 18, 2020 · 5 comments · Fixed by #130
Closed

Feature Request : method to flush all caches #121

ErikPilsits-RJW opened this issue Nov 18, 2020 · 5 comments · Fixed by #130

Comments

@ErikPilsits-RJW
Copy link

Would like to have a method to fully flush all cache layers. Right now I'm doing it directly through my redis connection, and then disposing / recreating the full cache stack to clear the memory layer. I also have to implement the pub/sub myself to get remote flush to work.

@Turnerj
Copy link
Member

Turnerj commented Nov 18, 2020

"Flush" like removing all items from the cache? I'm curious on the use case though otherwise sounds like a good feature.

For in-memory that is straight forward though for other types, it would need to be careful.

Redis could use FLUSHDB assuming there is no other items not related to caching. If a second Cache Stack used the same Redis, it would remove its data too.

MongoDB is a similar story. Can just delete all records in a query but would affect multiple Cache Stacks unless we namespace keys in each layer.

Filesystem caching less so as it has a central manifest file. Explicitly using it for multiple Cache Stacks means you are assuming the risk of a shared store.

So I guess the question comes down to: should users be using dedicated DBs/connections for cache layers so flushing/clearing all items won't break across Stacks unexpectedly? Or should I be ensuring namespaces for each cache layer so it wouldn't happen? (Though in this case, I wouldn't be able to use FLUSHDB on Redis)

Like you pointed out too, also need to consider remote flush/clearing so other cache layers can delete their local caches too

@ErikPilsits-RJW
Copy link
Author

Yes, to remove all from cache. It's definitely just a maintenance thing, and for redis you have to specify admin access on the connection string. Definitely user beware.

I implemented similar to your remote eviction extension. I also added retries via Polly. But I think the generic approach would be to flush the same list given to the remote eviction extension.

public async ValueTask ClearCache()
{
    // redis
    foreach (var endpoint in _connection.RedisConnection.GetEndPoints())
    {
        var server = _connection.RedisConnection.GetServer(endpoint);
        await RedisRetryExtensions.RetryPolicy.ExecuteWithLogger(
            async () =>
            {
                if (server.IsConnected) await server.FlushDatabaseAsync(_options.Database).Caf();
            }, _logger).Caf();
    }

    // publish to remotes
    await RedisRetryExtensions.RetryPolicy.ExecuteWithLogger(
        async () => await _subscriber.PublishAsync(RemoteFlushChannel, "FlushAll", CommandFlags.FireAndForget).Caf(), _logger).Caf();
}

private void RegisterRemoteFlush()
{
    if (_isRegistered) throw new InvalidOperationException("Cache provider can only be registered for remote flush one time.");

    _isRegistered = true;

    _subscriber.Subscribe(RemoteFlushChannel)
        .OnMessage(async channelMessage =>
        {
            // clear local by creating a new cachestack
            if (_cache != null) await _cache.DisposeAsync().Caf();
            _cache = CreateCacheStack();
        });
}

@Turnerj Turnerj mentioned this issue Dec 2, 2020
4 tasks
@prat0088
Copy link

I need this for debugging during development. I have an endpoint that flushes caches so I can quickly triage certain types of bugs. Eg. aid the problem with a bad cache state and stale data (mine) or is an upstream dependency returning bad data? Flush my caches and retry the request.

That’s easy to do now when everything is in memory. Reboot the web service. If I use redis and other distributed off box stores it will quickly become error prone and laborious to flush every one of them.

@Turnerj
Copy link
Member

Turnerj commented Dec 10, 2020

Thanks for letting me know @prat0088, I'm part way through adding it as a feature. Follow #130 to keep up to date on progress.

@Turnerj
Copy link
Member

Turnerj commented Jan 28, 2021

Hey @prat0088 - just letting you know that v0.8.0 of Cache Tower is now released with a method to flush all cache layers. See the updated documentation in the readme about how to use it.

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

Successfully merging a pull request may close this issue.

3 participants