2016-03-31 5 views
4

redigo libraryを使用してgolangのredisクライアントのプロトタイプを作成して、キースペースイベントを通知します。 redis.confを変更して、すべてのイベントを受け取るためにnotify-keyspace-eventsを "KEA"に設定しました。しかし、cliを使用してdbにキーを追加/更新/削除すると、クライアントでイベントが発生することはありません。イベントを発射するredigoを使用していますredigo golangクライアントはキースペースイベント通知をサポートしていますか?

サンプルコード:

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していますか?私はおそらくここで間違って何か?

答えて

5

購読するパターンで、余分な引用符を削除します。

psc.PSubscribe("__key*__:*") 

また、あなたはゴルーチンを必要としません。

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 
    } 
} 
次のように書くのは簡単です。
関連する問題