Пример #1
0
func main() {
	c, err := redis.Dial("tcp", ":6379")
	if err != nil {
		log.Fatal(err)
	}

	v0 := &MyStruct{1, "hello"}

	_, err = c.Do("HMSET", redisx.AppendStruct([]interface{}{"key"}, v0)...)
	if err != nil {
		log.Fatal(err)
	}

	reply, err := c.Do("HGETALL", "key")
	if err != nil {
		log.Fatal(err)
	}

	v1 := &MyStruct{}

	err = redisx.ScanStruct(reply, v1)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("v1=%v", v1)
}
Пример #2
0
func TestFormatStruct(t *testing.T) {
	for _, tt := range formatStructTests {
		args := redisx.AppendStruct(nil, tt.value)
		if !reflect.DeepEqual(args, tt.args) {
			t.Fatalf("FormatStruct(%s) returned %v, want %v", tt.title, args, tt.args)
		}
	}
}
Пример #3
0
//将值插入Hash表中 0:成功 1:失败
func HmSet(src interface{}, key string) int {
	c := InitRedis()
	_, err := c.Do("HMSET", redisx.AppendStruct([]interface{}{key}, src)...)
	if err != nil {
		log.Fatal(err)
		return 1
	}
	return 0
}
Пример #4
0
// Add creates a new album and returns its id, or an error.
func (db *routesDB) CreateRoute(route *RouteInfo) bool {
	key := "routeinfo:" + route.Hostname
	_, err := db.rclient.Do("HMSET", redisx.AppendStruct([]interface{}{key}, route)...)
	if err != nil {
		log.Fatal(err)
	}

	// Add to index
	_, err = db.rclient.Do("SADD", "index:hostnames", route.Hostname)
	if err != nil {
		log.Fatal(err)
	}
	return true
}