猿问

使用 IDistributedCache 列出和删除给定组中的所有缓存条目

我将用户生成的一些密钥存储在一个IDistributedCache设置中。


密钥是长期存在的,如果用户知道它们中的每一个,则可以由用户手动撤销它们:


public void ConfigureServices(IServiceCollection services)

{

    services.AddDistributedMemoryCache();

    //...

}


//ControllerBase:


//Adding the key:

await _cache.SetAsync(userKey, userId, new DistributedCacheEntryOptions 

    AbsoluteExpirationRelativeToNow = TimeSpan.FromDays(10)

});


//Removing the key:

await _cache.RemoveAsync(key);

现在,我想要做的是能够列出仍然存在于缓存中且由其中一位用户创建的所有密钥。另外,我希望能够批量删除它们。


现在有这样的功能吗?也许有取消令牌?我怎样才能做到这一点?


跃然一笑
浏览 331回答 2
2回答

哆啦的时光机

不,您一次只能使用一个键IDistributedCache。批量密钥驱逐和类似场景超出了这个门面的范围。如果您需要处理更高级的场景,那么您应该使用适当的客户端直接连接到您的缓存。例如,对于 Redis 支持,您可以使用该StackExchange.Redis包,并通过它发出您喜欢的任何命令。

jeck猫

您可以使用方法刷新存储FLUSHALL(IServer.FlushAllDatabasesAsync()- 请注意配置中的 AllowAdmin 标志必须为真)。该方法存在于IServer接口中,可以从中检索IConnectionMultiplexer。如果您使用的是StackExchange.RedisDI,IDistributedCache目前还无法这样做。您可以使用System.Reflection从IConnectionMultiplexer.(RedisCache)IDistributedCache
随时随地看视频慕课网APP
我要回答