英文:
Does redigo golang client support keyspace event notifications?
问题
我正在使用golang和redigo库原型化一个Redis客户端,以便通过keyspace事件接收通知。我修改了redis.conf文件,将notify-keyspace-events设置为"KEA"以接收所有事件。但是,当我使用命令行界面向数据库添加/更新/删除键时,客户端没有看到任何触发的事件。
以下是使用redigo触发事件的示例代码:
type RedisClient struct {
mRedisServer string
mRedisConn redis.Conn
mWg sync.WaitGroup
}
func (rc *RedisClient) Run() {
conn, err := redis.Dial("tcp", ":6379")
if err != nil {
fmt.Println(err)
return
}
rc.mRedisConn = conn
fmt.Println(conn)
rc.mRedisConn.Do("CONFIG", "SET", "notify-keyspace-events", "KEA")
fmt.Println("Set the notify-keyspace-events to KEA")
defer rc.mRedisConn.Close()
rc.mWg.Add(2)
psc := redis.PubSubConn{Conn: rc.mRedisConn}
go func() {
defer rc.mWg.Done()
for {
switch msg := psc.Receive().(type) {
case redis.Message:
fmt.Printf("Message: %s %s\n", msg.Channel, msg.Data)
case redis.PMessage:
fmt.Printf("PMessage: %s %s %s\n", msg.Pattern, msg.Channel, msg.Data)
case redis.Subscription:
fmt.Printf("Subscription: %s %s %d\n", msg.Kind, msg.Channel, msg.Count)
if msg.Count == 0 {
return
}
case error:
fmt.Printf("error: %v\n", msg)
return
}
}
}()
go func() {
defer rc.mWg.Done()
psc.PSubscribe("\"__key*__:\"")
select {}
}()
rc.mWg.Wait()
}
redigo库支持keyspace事件通知吗?我在这里可能做错了什么吗?
英文:
I am prototyping a redis client in golang using the redigo library to get notified of keyspace events. I modified the redis.conf to set the notify-keyspace-events to "KEA" to receive all events. But when I add/update/delete keys into the db using a cli, I don't see any events getting fired on the client.
Sample code that uses redigo to fire events:
type RedisClient struct {
mRedisServer string
mRedisConn redis.Conn
mWg sync.WaitGroup
}
func (rc *RedisClient) Run() {
conn, err := redis.Dial("tcp", ":6379")
if err != nil {
fmt.Println(err)
return
}
rc.mRedisConn = conn
fmt.Println(conn)
rc.mRedisConn.Do("CONFIG", "SET", "notify-keyspace-events", "KEA")
fmt.Println("Set the notify-keyspace-events to KEA")
defer rc.mRedisConn.Close()
rc.mWg.Add(2)
psc := redis.PubSubConn{Conn: rc.mRedisConn}
go func() {
defer rc.mWg.Done()
for {
switch msg := psc.Receive().(type) {
case redis.Message:
fmt.Printf("Message: %s %s\n", msg.Channel, msg.Data)
case redis.PMessage:
fmt.Printf("PMessage: %s %s %s\n", msg.Pattern, msg.Channel, msg.Data)
case redis.Subscription:
fmt.Printf("Subscription: %s %s %d\n", msg.Kind, msg.Channel, msg.Count)
if msg.Count == 0 {
return
}
case error:
fmt.Printf("error: %v\n", msg)
return
}
}
}()
go func() {
defer rc.mWg.Done()
psc.PSubscribe("\"__key*__:*\"")
select {}
}()
rc.mWg.Wait()
}
Does redigo support keyspace event notifications? Anything I maybe doing wrong here?
答案1
得分: 13
移除订阅模式中的额外引号:
psc.PSubscribe("__key*__:*")
此外,你不需要使用goroutines。可以简化为以下代码:
psc := redis.PubSubConn{Conn: rc.mRedisConn}
psc.PSubscribe("__key*__:*")
for {
switch msg := psc.Receive().(type) {
case redis.Message:
fmt.Printf("Message: %s %s\n", msg.Channel, msg.Data)
case redis.PMessage:
fmt.Printf("PMessage: %s %s %s\n", msg.Pattern, msg.Channel, msg.Data)
case redis.Subscription:
fmt.Printf("Subscription: %s %s %d\n", msg.Kind, msg.Channel, msg.Count)
if msg.Count == 0 {
return
}
case error:
fmt.Printf("error: %v\n", msg)
return
}
}
英文:
Remove the extra quotes in the subscribe pattern:
psc.PSubscribe("__key*__:*")
Also, you don't need the goroutines. It's simpler to write it as:
psc := redis.PubSubConn{Conn: rc.mRedisConn}
psc.PSubscribe("__key*__:*")
for {
switch msg := psc.Receive().(type) {
case redis.Message:
fmt.Printf("Message: %s %s\n", msg.Channel, msg.Data)
case redis.PMessage:
fmt.Printf("PMessage: %s %s %s\n", msg.Pattern, msg.Channel, msg.Data)
case redis.Subscription:
fmt.Printf("Subscription: %s %s %d\n", msg.Kind, msg.Channel, msg.Count)
if msg.Count == 0 {
return
}
case error:
fmt.Printf("error: %v\n", msg)
return
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论