首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >redigo客户端支持键空间事件通知吗?

redigo客户端支持键空间事件通知吗?
EN

Stack Overflow用户
提问于 2016-03-31 19:53:15
回答 1查看 2.9K关注 0票数 7

我正在使用redigo图书馆在golang中创建一个redis客户端的原型,以获得键空间事件的通知。我修改了redis.conf,将events设置为"KEA“以接收所有事件。但是,当我使用cli将/update/delete键添加到db中时,我没有看到任何事件在客户机上被触发。

使用redigo激发事件的示例代码:

代码语言:javascript
复制
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支持键空间事件通知吗?我在这里做错什么了吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-31 20:19:02

删除订阅模式中的额外引号:

代码语言:javascript
复制
psc.PSubscribe("__key*__:*")

还有,你不需要猩猩。把它写成:

代码语言:javascript
复制
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
    }
}
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36342510

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档