示例#1
0
func TestCmdEval(t *testing.T) {
	c := getTestConn()
	defer c.Close()

	if v, err := ledis.Strings(c.Do("eval", "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}", 2, "key1", "key2", "first", "second")); err != nil {
		t.Fatal(err)
	} else if !reflect.DeepEqual(v, []string{"key1", "key2", "first", "second"}) {
		t.Fatal(fmt.Sprintf("%v", v))
	}

	if v, err := ledis.Strings(c.Do("eval", "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}", 2, "key1", "key2", "first", "second")); err != nil {
		t.Fatal(err)
	} else if !reflect.DeepEqual(v, []string{"key1", "key2", "first", "second"}) {
		t.Fatal(fmt.Sprintf("%v", v))
	}

	var sha1 string
	var err error
	if sha1, err = ledis.String(c.Do("script", "load", "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}")); err != nil {
		t.Fatal(err)
	} else if len(sha1) != 40 {
		t.Fatal(sha1)
	}

	if v, err := ledis.Strings(c.Do("evalsha", sha1, 2, "key1", "key2", "first", "second")); err != nil {
		t.Fatal(err)
	} else if !reflect.DeepEqual(v, []string{"key1", "key2", "first", "second"}) {
		t.Fatal(fmt.Sprintf("%v", v))
	}

	if ay, err := ledis.Values(c.Do("script", "exists", sha1, "01234567890123456789")); err != nil {
		t.Fatal(err)
	} else if !reflect.DeepEqual(ay, []interface{}{int64(1), int64(0)}) {
		t.Fatal(fmt.Sprintf("%v", ay))
	}

	if ok, err := ledis.String(c.Do("script", "flush")); err != nil {
		t.Fatal(err)
	} else if ok != "OK" {
		t.Fatal(ok)
	}

	if ay, err := ledis.Values(c.Do("script", "exists", sha1)); err != nil {
		t.Fatal(err)
	} else if !reflect.DeepEqual(ay, []interface{}{int64(0)}) {
		t.Fatal(fmt.Sprintf("%v", ay))
	}
}
示例#2
0
func checkScanValues(t *testing.T, ay interface{}, values ...int) {
	a, err := ledis.Strings(ay, nil)
	if err != nil {
		t.Fatal(err)
	}

	if len(a) != len(values) {
		t.Fatal(fmt.Sprintf("len %d != %d", len(a), len(values)))
	}

	for i, v := range a {
		if string(v) != fmt.Sprintf("%d", values[i]) {
			t.Fatal(fmt.Sprintf("%d %s != %d", string(v), values[i]))
		}
	}
}
示例#3
0
func setRead() {
	c := client.Get()

	var first bool = true

	var cursor []byte
	var total int64 = 0
	t1 := time.Now().UnixNano()

	for string(cursor) != "" || first {
		ay, err := ledis.Values(c.Do("scan", cursor, "count", *count))
		if err != nil {
			fmt.Printf("do scan err %s", err.Error())
			return
		} else if len(ay) != 2 {
			fmt.Println("scan result invalid")
			return
		}

		cursor = ay[0].([]byte)
		data, err := ledis.Strings(ay[1], nil)
		if err != nil {
			fmt.Println(err)
			return
		}

		for _, k := range data {
			ledis.String(c.Do("get", []byte(k)))
			total++

			if total%100000 == 0 {
				fmt.Println(total)
				fmt.Println(time.Now().Format("2006/01/02 15:04:05"))
			}
		}

		first = false
	}

	t2 := time.Now().UnixNano()
	delta := float64(t2-t1) / float64(time.Second)
	fmt.Printf("total %d kv keys , consumed %f seconds \n", total, delta)
}