Exemple #1
0
// ConnectSSDB SSDB服务必须先启动
func ConnectSSDB(conf *gossdb.Config, password ...string) (err error) {

	rand.Seed(time.Now().UnixNano())

	pool, err = gossdb.NewPool(conf, password...)

	return

}
Exemple #2
0
// NewSSDBStore creates and returns a redis session store.
func New(opts ...Options) (*SSDBStore, error) {
	opt := preOptions(opts)
	pool, err := gossdb.NewPool(&gossdb.Config{
		Host:             opt.Host,
		Port:             opt.Port,
		MinPoolSize:      5,
		MaxPoolSize:      50,
		AcquireIncrement: 5,
	})

	if err != nil {
		return nil, err
	}

	return &SSDBStore{
		Options: opt,
		pool:    pool,
		Logger:  log.Std,
	}, nil
}
Exemple #3
0
func test2() {
	ip := "127.0.0.1"
	port := 8888

	pool, err := gossdb.NewPool(&gossdb.Config{
		Host: ip,
		Port: port,
	})
	if err != nil {
		fmt.Errorf("error new pool %v", err)
		return
	}
	gossdb.Encoding = true

	c, err := pool.NewClient()
	if err != nil {
		fmt.Errorf("new client err=%v", err)
		return
	}

	c.Set("myset", "hello world")
	val, err := c.Get("myset")
	fmt.Println("val=", val)

	c.Set("mysetint", 2008)
	val1, err := c.Get("mysetint")
	fmt.Println("val1=", val1.Int32())

	c.Hset("books", "id1", "book name 1")
	c.Hset("books", "id2", "book name 2")
	c.Hset("books", "id3", "book name 3")
	c.Hset("books", "id4", 8888)

	val2, err := c.Hget("books", "id1")
	fmt.Println("val2(id1)=", val2)

	val3, err := c.Hget("books", "id4")
	fmt.Println("val3(id4)=", val3)

	result, err := c.Hscan("books", "", "", 100)
	if err != nil {
		fmt.Errorf("hscan error = %v", err)
	}
	fmt.Printf("result=%v\n", result)

	c.Hset("booklist", "id1", &Book{"id1", "name1", 1})
	c.Zset("booklist_order_by_price", "id1", 1)
	c.Hset("booklist", "id2", &Book{"id2", "name2", 2})
	c.Zset("booklist_order_by_price", "id2", 2)
	c.Hset("booklist", "id3", &Book{"id3", "name3", 1})
	c.Zset("booklist_order_by_price", "id3", 1)
	c.Hset("booklist", "id4", &Book{"id4", "name4", 3})
	c.Zset("booklist_order_by_price", "id4", 3)
	c.Hset("booklist", "id5", &Book{"id5", "name5", 51})
	c.Zset("booklist_order_by_price", "id5", 51)
	c.Hset("booklist", "id6", &Book{"id6", "name6", 18})
	c.Zset("booklist_order_by_price", "id6", 18)
	c.Hset("booklist", "id7", &Book{"id7", "name7", 15})
	c.Zset("booklist_order_by_price", "id7", 15)
	c.Hset("booklist", "id8", &Book{"id8", "name8", 22})
	c.Zset("booklist_order_by_price", "id8", 22)
	c.Hset("booklist", "id9", &Book{"id9", "name9", 11})
	c.Zset("booklist_order_by_price", "id9", 11)
	c.Hset("booklist", "id10", &Book{"id10", "name10", 10})
	c.Zset("booklist_order_by_price", "id10", 10)

	result1, err := c.Hscan("booklist", "", "", 100)
	fmt.Printf("result1=%v\n", result1)

	for i, b := range result1 {
		fmt.Printf("%v - %v\n", i, b)
	}

	fmt.Println("will zscan---------------------------------")
	keys, scores, err := c.Zscan("booklist_order_by_price", "", "", "", 1000)
	fmt.Printf("keys = %v \nscores = %v\n", keys, scores)
	fmt.Printf("keys len = %v \nscores  len = %v\n", len(keys), len(scores))
	for i, k := range keys {
		fmt.Printf("%v : %v - %v\n", i, k, scores[i])
	}
	fmt.Println("will zrscan---------------------------------")
	keys, scores, err = c.Zrscan("booklist_order_by_price", "", "", "", 1000)
	fmt.Printf("keys = %v \nscores = %v\n", keys, scores)
	fmt.Printf("keys len = %v \nscores  len = %v\n", len(keys), len(scores))
	for i, k := range keys {
		fmt.Printf("%v : %v - %v\n", i, k, scores[i])
	}

	keys, values, err := c.MultiHgetSlice("booklist", keys...)
	fmt.Printf("keys=%v\n", keys)
	for i, k := range keys {
		b := values[i]

		fmt.Printf("%v - %v - %v\n", i, k, b)

		var book Book
		b.As(&book)
		fmt.Printf("---book=%v\n", book)
	}

	c.Zset("zsettest", "zsettest_count", 0)
	c.Zincr("zsettest", "zsettest_count", 1)
	c.Zincr("zsettest", "zsettest_count", 1)
	c.Zincr("zsettest", "zsettest_count", 1)
	count, err := c.Zget("zsettest", "zsettest_count")
	if err != nil {
		fmt.Println("zsettest_count error = ", err.Error())
	} else {
		fmt.Printf("zsettest_count = %v\n", count)
	}

}
Exemple #4
0
func main() {
	//	//	i := 12
	//	var v gossdb.Value = "123"
	//	log.Println(v.String())
	//	log.Println(v.Int())
	//	log.Println(v.Int64())
	//	log.Println(v.Int32())
	//	log.Println(v.Int8())
	//	log.Println(v.Int16())
	//	log.Println(v.UInt())
	//	log.Println(v.UInt64())
	//	log.Println(v.UInt32())
	//	log.Println(v.UInt8())
	//	log.Println(v.UInt16())
	//	log.Println(v.Bool())
	//	log.Println(v.Float32())
	//	log.Println(v.Time())
	//	log.Println(v.Duration())
	//	log.Println(v.Bytes())
	//	return
	runtime.GOMAXPROCS(runtime.NumCPU())
	gossdb.AuthPassword = "******"
	pool, err := gossdb.NewPool(&gossdb.Config{
		Host:             "127.0.0.1",
		Port:             8888,
		MinPoolSize:      5,
		MaxPoolSize:      50,
		AcquireIncrement: 5,
		GetClientTimeout: 10,
		MaxWaitSize:      1000,
		MaxIdleTime:      1,
		HealthSecond:     2,
	})

	if err != nil {
		log.Println(err)
		return
	}
	gossdb.Encoding = true
	client, err := pool.NewClient()
	if err != nil {
		log.Println(err.Error())
		return
	}
	defer client.Close()
	client.Set("a", "hello1")
	client.Set("b", "hello2")
	client.Set("keys", "hello")
	v, err := client.Rscan("z", "", 100)
	log.Println(v, err)
	err = client.Set("keys", "hello")
	log.Println(err)
	//	v, err := client.Setbit("keys", 3, 0)
	//	log.Println(v, err)
	//	v, err = client.Getbit("keys", 3)
	//	log.Println(v, err)
	//client.Del("keys")
	//	v, err := client.Setnx("keys", time.Now())
	//	log.Println(v, err)
	//	v, err = client.Get("keys")
	//	log.Println(err, v)
	//	err = client.Set("keys", time.Now(), 1)
	//	log.Println(err)
	//	//time.Sleep(time.Second * 3)
	//	v, err := client.Get("keys")
	//	log.Println(err, v)
	//	var test time.Time
	//	log.Println(err)
	//	err = v.As(&test)
	//	log.Println(err, test, v)
	//	err = client.Hset("set", "key", 132)
	//	log.Println(err)
	//	//client.Client.Close()
	//	v, err = client.Hget("set", "key")
	//	log.Println(v, err)
	//	v, err = client.Getset("keys", "key1")
	//	log.Println(v, err)
	//	v, err = client.Getset("keys", "key2")
	//	log.Println(v, err)
	//	bv, err := client.Hexists("set", "key")
	//	log.Println(bv, err)
	//	i, err := client.Ttl("set")
	//	log.Println("ttl", i, err)
	//	err = client.Hdel("set", "key")
	//	log.Println(err)
	//	err = client.Hclear("set")
	//	log.Println(err)
	//	client.Qclear("queue")
	//	size, err := client.Qpush("queue", 1, 2, 3, test)
	//	log.Println(err, size)
	//	size, err = client.Qpush("queue", 3, 2, 1)
	//	log.Println(err, size)
	//	v, err = client.Qpop_front("queue")
	//	log.Println(err, v)
	//	v, err = client.Qpop_back("queue")
	//	log.Println(err, v)
	//	vs, err := client.Qrange("queue", 0, 6)
	//	log.Println(err, vs)
	//	vs, err = client.Qslice("queue", 0, 2)
	//	log.Println(err, vs)
	//	size, err = client.Qtrim("queue", 1)
	//	log.Println(err, size)
	//	vs, err = client.Qslice("queue", 0, 2)
	//	log.Println(err, vs)
	//	i, err = client.Incr("incr", 1)
	//	log.Println(i, err)
	//	i, err = client.Incr("incr", 4)
	//	log.Println(i, err)
	//	mm := make(map[string]interface{})
	//	mm["a"] = 1
	//	mm["b"] = 11
	//	mm["a1"] = 1
	//	mm["b11"] = 11
	//	mm["a22"] = 1
	//	mm["b22"] = 11
	//	err = client.MultiSet(mm)
	//	log.Println(err)

	//	err = client.MultiDel("a", "b", "a1")
	//	log.Println(err)
	//	vm, err := client.MultiGet("a", "b", "a1")
	//	log.Println(vm, err)
	//	log.Println("----------------")

	//	for i := 0; i < 100; i++ {
	//		go func(idx int) {
	//			//log.Println(idx, "get client", pool.Info())
	//			c, err := pool.NewClient()
	//			if err != nil {
	//				log.Println(err.Error(), idx)
	//				return
	//			}
	//			defer c.Close()
	//			err = c.Set(fmt.Sprintf("test%d", idx), fmt.Sprintf("test%d", idx))
	//			if err != nil {
	//				log.Println(err)
	//			}
	//			re, err := c.Get(fmt.Sprintf("test%d", idx))
	//			if err != nil {
	//				log.Println(err, re, "close client")
	//			} else {
	//				log.Println(idx, "close client")
	//			}
	//		}(i)
	//		//time.Sleep(time.Millisecond)
	//	}
	//	time.Sleep(time.Second * 10)
	//	log.Println(pool.Info())
	//	time.Sleep(time.Second * 10)
	//	pool.Close() //连接可能未处理完
	//	log.Println(pool.Info())
	//	time.Sleep(time.Second * 10)
}