// generate new sid for redis session func (rp *RedisProvider) SessionRegenerate(oldsid, sid string) (session.SessionStore, error) { c := rp.poollist.Get() defer c.Close() if existed, _ := redis.Int(c.Do("EXISTS", oldsid)); existed == 0 { // oldsid doesn't exists, set the new sid directly // ignore error here, since if it return error // the existed value will be 0 c.Do("SET", sid, "", "EX", rp.maxlifetime) } else { c.Do("RENAME", oldsid, sid) c.Do("EXPIRE", sid, rp.maxlifetime) } kvs, err := redis.String(c.Do("GET", sid)) var kv map[interface{}]interface{} if len(kvs) == 0 { kv = make(map[interface{}]interface{}) } else { kv, err = session.DecodeGob([]byte(kvs)) if err != nil { return nil, err } } rs := &RedisSessionStore{p: rp.poollist, sid: sid, values: kv, maxlifetime: rp.maxlifetime} return rs, nil }
// Dial dials the local Redis server and selects database 9. To prevent // stomping on real data, DialTestDB fails if database 9 contains data. The // returned connection flushes database 9 on close. func Dial() (redis.Conn, error) { c, err := redis.DialTimeout("tcp", ":6379", 0, 1*time.Second, 1*time.Second) if err != nil { return nil, err } _, err = c.Do("SELECT", "9") if err != nil { c.Close() return nil, err } n, err := redis.Int(c.Do("DBSIZE")) if err != nil { c.Close() return nil, err } if n != 0 { c.Close() return nil, errors.New("database #9 is not empty, test can not continue") } return testConn{c}, nil }
func ExampleInt() { c, err := dial() if err != nil { fmt.Println(err) return } defer c.Close() c.Do("SET", "k1", 1) n, _ := redis.Int(c.Do("GET", "k1")) fmt.Printf("%#v\n", n) n, _ = redis.Int(c.Do("INCR", "k1")) fmt.Printf("%#v\n", n) // Output: // 1 // 2 }
// check redis session exist by sid func (rp *RedisProvider) SessionExist(sid string) bool { c := rp.poollist.Get() defer c.Close() if existed, err := redis.Int(c.Do("EXISTS", sid)); err != nil || existed == 0 { return false } else { return true } }
func TestRedisCache(t *testing.T) { bm, err := cache.NewCache("redis", `{"conn": "127.0.0.1:6379"}`) if err != nil { t.Error("init err") } if err = bm.Put("astaxie", 1, 10); err != nil { t.Error("set Error", err) } if !bm.IsExist("astaxie") { t.Error("check err") } time.Sleep(10 * time.Second) if bm.IsExist("astaxie") { t.Error("check err") } if err = bm.Put("astaxie", 1, 10); err != nil { t.Error("set Error", err) } if v, _ := redis.Int(bm.Get("astaxie"), err); v != 1 { t.Error("get err") } if err = bm.Incr("astaxie"); err != nil { t.Error("Incr Error", err) } if v, _ := redis.Int(bm.Get("astaxie"), err); v != 2 { t.Error("get err") } if err = bm.Decr("astaxie"); err != nil { t.Error("Decr Error", err) } if v, _ := redis.Int(bm.Get("astaxie"), err); v != 1 { t.Error("get err") } bm.Delete("astaxie") if bm.IsExist("astaxie") { t.Error("delete err") } //test string if err = bm.Put("astaxie", "author", 10); err != nil { t.Error("set Error", err) } if !bm.IsExist("astaxie") { t.Error("check err") } if v, _ := redis.String(bm.Get("astaxie"), err); v != "author" { t.Error("get err") } //test GetMulti if err = bm.Put("astaxie1", "author1", 10); err != nil { t.Error("set Error", err) } if !bm.IsExist("astaxie1") { t.Error("check err") } vv := bm.GetMulti([]string{"astaxie", "astaxie1"}) if len(vv) != 2 { t.Error("GetMulti ERROR") } if v, _ := redis.String(vv[0], nil); v != "author" { t.Error("GetMulti ERROR") } if v, _ := redis.String(vv[1], nil); v != "author1" { t.Error("GetMulti ERROR") } // test clear all if err = bm.ClearAll(); err != nil { t.Error("clear all err") } }