redis 的key 過期通知設定

Redis - Wikipedia

redis有expire機制,且同時有redis keyspace notification。不過預設默認的通知是關閉的~需要透過redis.conf或config set啟用。

config set notify-keyspace-events KEA

接著在c# 使用StackExchange.Redis

然後訂閱__keyevent@0__:expired 這個頻道就可以了

var redisConnection = ConnectionMultiplexer.Connect(“localhost”);
var db = redisConnection.GetDatabase();

var subscriber = redisConnection.GetSubscriber();
// 訂閱過期事件通知頻道
subscriber.Subscribe(“__keyevent@0__:expired”, (channel, key1) =>
{
// 在這裡進行過期事件的處理邏輯
// key 是過期的 Redis 鍵名
Console.WriteLine($”Key {key1} has expired.”);
});

Console.WriteLine(“Press any key to exit.”);
Console.ReadKey();

// 斷開與 Redis 的連線
redisConnection.Close();

Redis for Windows 5.0.14.1

最近因為開發上的需求使用Redis,不過Redis並不支援Windows。官網上的建議做法也是建議打開WSL(Windows Subsystem for Linux),裝上ubuntu後再安裝(詳見: Install Redis on Windows )。windows上面網路上建議安裝的是Microsoft 已封存的3.0版本 ( https://github.com/microsoftarchive/redis ) 或是建議使用相容redis 6的 memurai (https://www.memurai.com/ ) 。但考量.net後續可以在Linux上跑的原因~還是以較悠久的redis為主,於是在網路上找到一個比較貼近的版本,是由網路上的大神Tomasz Poradowski 在github上分享的版本 https://github.com/tporadowski/redis/releases 。2022年2月發佈最新的版本是5.0.14.1。

Dveloper GUI for Redis
Another Redis Desktop Manager
Redis Assistant

延伸資料:

redis的持久化

Redis数据库学习教程

redis的持久化方式RDB和AOF的区别

2021 iThome 鐵人賽Redis還在學