redigo golang客户端是否支持键空间事件通知?

huangapple go评论70阅读模式
英文:

Does redigo golang client support keyspace event notifications?

问题

我正在使用golangredigo库原型化一个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
}
}

huangapple
  • 本文由 发表于 2016年4月1日 03:53:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/36342510.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定