|
@@ -0,0 +1,70 @@
|
|
|
+package redis
|
|
|
+
|
|
|
+import (
|
|
|
+ "log"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ redisLogin "github.com/gomodule/redigo/redis"
|
|
|
+)
|
|
|
+
|
|
|
+var RedisLoginPool *redisLogin.Pool
|
|
|
+
|
|
|
+func InitRedisLogin(addrs string) {
|
|
|
+ addr := strings.Split(addrs, ",")
|
|
|
+ for _, v := range addr {
|
|
|
+ saddr := strings.Split(v, "=")
|
|
|
+ if saddr[0] == "login" {
|
|
|
+ RedisLoginPool = &redisLogin.Pool{MaxActive: 10, MaxIdle: 5,
|
|
|
+ IdleTimeout: time.Duration(10) * time.Second, Dial: func() (redisLogin.Conn, error) {
|
|
|
+ c, err := redisLogin.Dial("tcp", saddr[1])
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ return c, nil
|
|
|
+ }}
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+//
|
|
|
+func SetLoginVal(key, value string) {
|
|
|
+ conn := RedisLoginPool.Get()
|
|
|
+ defer conn.Close()
|
|
|
+ conn.Do("PUBLISH", key, value)
|
|
|
+}
|
|
|
+
|
|
|
+//
|
|
|
+func GetLoginVal(key string, wxFunc func(wxParams []string) bool) {
|
|
|
+ for {
|
|
|
+ defer catch()
|
|
|
+ L:
|
|
|
+ for {
|
|
|
+ conn := RedisLoginPool.Get()
|
|
|
+ defer conn.Close()
|
|
|
+ if conn.Err() == nil {
|
|
|
+ psc := redisLogin.PubSubConn{Conn: conn}
|
|
|
+ if err := psc.Subscribe(redisLogin.Args{}.AddFlat(key)...); err != nil {
|
|
|
+ log.Println(err)
|
|
|
+ }
|
|
|
+ for {
|
|
|
+ msg := psc.Receive()
|
|
|
+ // go func(msg interface{}) {
|
|
|
+ switch n := msg.(type) {
|
|
|
+ case error:
|
|
|
+ log.Println("wxlogin err", msg)
|
|
|
+ break L
|
|
|
+ case redisLogin.Message:
|
|
|
+ res := string(n.Data)
|
|
|
+ param := strings.Split(res, ",")
|
|
|
+ go wxFunc(param)
|
|
|
+ }
|
|
|
+ // }(msg)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ time.Sleep(2 * time.Second)
|
|
|
+ }
|
|
|
+ time.Sleep(1 * time.Second)
|
|
|
+ }
|
|
|
+}
|