Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does redigo golang client support keyspace event notifications?

Tags:

go

redigo

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?

like image 911
anbhat Avatar asked Mar 31 '16 19:03

anbhat


1 Answers

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
    }
}
like image 120
Bayta Darell Avatar answered Nov 13 '22 07:11

Bayta Darell