Last active
September 13, 2017 18:35
-
-
Save gabriel-tincu/7d24b9333ed993b18037341d38bedeeb to your computer and use it in GitHub Desktop.
minima redisl keyspace notification glob pattern in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/go-redis/redis" | |
"time" | |
) | |
func main() { | |
cli_set_del := redis.NewClient(&redis.Options{ | |
Addr:"localhost:6379", | |
DB:0, | |
}) | |
cli_listen := redis.NewClient(&redis.Options{ | |
Addr:"localhost:6379", | |
DB:0, | |
}) | |
s, err := cli_set_del.ConfigSet("notify-keyspace-events", "KA").Result() | |
if err != nil { | |
panic(err) | |
} else { | |
fmt.Println(s) | |
} | |
for i:=0;i<3;i++{ | |
ps := cli_listen.PSubscribe("__keyspace@0__:foo*") | |
go react(ps, i) | |
} | |
time.Sleep(time.Millisecond * 100) | |
for x:=1;x<5;x++ { | |
cli_set_del.Set(fmt.Sprintf("foo%d", x), "bar", time.Hour) | |
} | |
fmt.Println("Starting to delete crap") | |
for i:=1;i<5;i++{ | |
fmt.Printf("Deleting foo%d\n",i) | |
cli_set_del.Del(fmt.Sprintf("foo%d", i)) | |
time.Sleep(time.Millisecond * 300) | |
} | |
time.Sleep(1 * time.Second) | |
} | |
func react(ps *redis.PubSub, i int) { | |
c := ps.Channel() | |
for { | |
select { | |
case r := <- c: | |
fmt.Println(r, i) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No fanout, glob pattern, on all events on a key pattern, so we end up filtering the ones we actually need