redis / go-redis

Redis Go client
https://redis.uptrace.dev
BSD 2-Clause "Simplified" License
19.99k stars 2.36k forks source link

Questions about keyspace subscribe #1267

Closed letientai299 closed 3 weeks ago

letientai299 commented 4 years ago

I'm using keyspace notification in redis. Let's say the key I'm interesting in the key-space event of testkey

With redis-cli, I can use PSUBSCRIBE with the pattern _keyspace@*__:testkey successfully.

127.0.0.1:6379> PSUBSCRIBE __keyspace@*__:testkey
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "__keyspace@*__:testkey"
3) (integer) 1
1) "pmessage"
2) "__keyspace@*__:testkey"
3) "__keyspace@0__:testkey"
4) "set"
1) "pmessage"
2) "__keyspace@*__:testkey"
3) "__keyspace@0__:testkey"
4) "set"

But in go, the above pattern following pattern won't work. Here's code sample

subPattern := "__keyspace@*__:testkey"
sub := a.redisClient.PSubscribe(subPattern)
log.Printf("watching %v", subPattern)
//lastScore := util.TimeNowMillis()
for {
  msg, err := sub.Receive()
  if err != nil {
    log.Println(err)
    continue
  }

  log.Println(msg)
  //switch msg.(type) {
}

But this pattern __keyspace@*__:testkey*, with a wildcard suffix, works.

I've tested and see that this issue happen with both redis single instance and in cluster mode. I've tested this with other golang redis lib yet.

Is this an expected behaviors? If so, may I have a link to read more about keyspace patterns?

tpoxa commented 1 year ago

try enabling notify keyspace events first using config set

rdb.ConfigSet(ctx, "notify-keyspace-events", "KEA")

github-actions[bot] commented 1 month ago

This issue is marked stale. It will be closed in 30 days if it is not updated.