skip to Main Content

I am generating a key that will expire in Redis. As a programming language, I am using Go. The problem is that I need to know when something expires, because based on that, I know which key is expiring or what data it contains, and I can know when it is no longer in Redis. How can I listen for the expiration event using Golang? I have seen examples in TypeScript but not in Golang

This is my code to set an expiring key

func (r *Redis) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error {
    return r.Client.Set(ctx, key, value, expiration).Err()
}

2

Answers


  1. Chosen as BEST ANSWER

    this works for me https://mattboodoo.com/2021/07/02/using-redis-keyspace-events-with-golang-for-a-poor-mans-event-driven-application/

    the problem was this redis.Client.Do(context.Background(), "CONFIG", "SET", "notify-keyspace-events", "KEA").Result()

    when I added it, I was able to listen to the expiration events


  2. Use Redis keyspace notifications. You’re not saying what client library you are using, but here is an example for Redigo.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search