refs:
https://stackoverflow.com/questions/35699811/subscribing-to-redis-expire-events-in-c-sharp-driver
https://redis.io/topics/notifications
1)config中使能 key space event watch;使能键观察,使能事件观察;
config set notify-keyspace-events KEx
2)sub相关键值
redis-cli --csv psubscribe '__keyspace@0__:dtu:*'
3)在service stack中,设置如下
注意expireKey此处是事件expire,ch中是keyPre和相应的键信息
//config set notify-keyspace-events KEx
string EXPIRED_KEYS_CHANNEL = $"__keyspace@{redisConfigInfo.DefaultDb}__:{KeyPre}*";
using (var client = RedisManager.GetClient())
{
using (var subscription = client.CreateSubscription())
{
subscription.OnMessage += (ch, expiredKey) =>
{
string ch_pre = EXPIRED_KEYS_CHANNEL.Replace("*","");
string bianhao = ch.Replace(ch_pre,"");
OnExpired(bianhao);
};
subscription.SubscribeToChannelsMatching(EXPIRED_KEYS_CHANNEL);
}
}