How to get expiration time for a key on Redis using Dotnet Core?

ⅰ亾dé卋堺 提交于 2020-02-05 04:56:05

问题


I'm following this example to implement Redis cache on Dotnet Core.

Unfortunately t seems that both Get(key) and GetString(key) only return the value, not the metadata such as expiry.

When I use redis-cli I'm able to retrieve both the data and metadata using HGETALL key

1) "absexp"
2) "637153531959962660"
3) "data"
4) "[{<snip>}]"
5) "sldexp"
6) "-1"

Is there any way to access this metadata from my code?

There might of course be workarounds like adding the expiry in the data object itself or adding a second key that contains the metadata, but neither solutions are very elegant.


回答1:


Assuming you are using Microsoft.Extensions.Caching.StackExchangeRedis.

There is no way to get this from reviewing the docs and source.

You will have to get it using StackExchange.Redis directly.

Follow the same approach used on RedisCache.cs:

//RedisCacheOptions options as passed to RedisCache contructor.
ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(options.ConfigurationOptions);
IDatabase cache = connection.GetDatabase();
HashEntry[] results = cache.HashGetAll(key);


来源:https://stackoverflow.com/questions/59871919/how-to-get-expiration-time-for-a-key-on-redis-using-dotnet-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!