加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 09:24:44 所属栏目:大数据 来源:网络整理
导读:我正在使用 redigo library在golang中对redis客户端进行原型设计,以获得密钥空间事件的通知.我修改了redis.conf,将notify-keyspace-events设置为“KEA”以接收所有事件.但是当我使用cli向数据库添加/更新/删除密钥时,我没有看到任何事件在客户端被触发. 使用
我正在使用 redigo library在golang中对redis客户端进行原型设计,以获得密钥空间事件的通知.我修改了redis.conf,将notify-keyspace-events设置为“KEA”以接收所有事件.但是当我使用cli向数据库添加/更新/删除密钥时,我没有看到任何事件在客户端被触发.

使用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 %sn",msg.Channel,msg.Data)
            case redis.PMessage:
                fmt.Printf("PMessage: %s %s %sn",msg.Pattern,msg.Data)
            case redis.Subscription:
                fmt.Printf("Subscription: %s %s %dn",msg.Kind,msg.Count)
                if msg.Count == 0 {
                    return
                }
            case error:
                fmt.Printf("error: %vn",msg)
                return
            }
        }
    }()
    go func() {
        defer rc.mWg.Done()
        psc.PSubscribe(""__key*__:*"")
        select {}
    }()
    rc.mWg.Wait()
}

redigo是否支持键空间事件通知?我在这里做错了什么?

解决方法

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

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 %sn",msg.Data)
    case redis.PMessage:
        fmt.Printf("PMessage: %s %s %sn",msg.Data)
    case redis.Subscription:
        fmt.Printf("Subscription: %s %s %dn",msg.Count)
        if msg.Count == 0 {
            return
        }
    case error:
        fmt.Printf("error: %vn",msg)
        return
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读