redis 检查key expire事件

来源:互联网 发布:屏风专卖店淘宝网 编辑:程序博客网 时间:2024/06/09 10:50

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);                }            }