Created
July 29, 2018 02:40
-
-
Save qbig/c36206bfa296e48483ecd4e6a59324b0 to your computer and use it in GitHub Desktop.
redis-go Client example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cache_test | |
import ( | |
"fmt" | |
"time" | |
"github.com/go-redis/redis" | |
"github.com/vmihailenco/msgpack" | |
"github.com/go-redis/cache" | |
) | |
type Object struct { | |
Str string | |
Num int | |
} | |
func Example_basicUsage() { | |
ring := redis.NewRing(&redis.RingOptions{ | |
Addrs: map[string]string{ | |
"server1": ":6379", | |
"server2": ":6380", | |
}, | |
}) | |
codec := &cache.Codec{ | |
Redis: ring, | |
Marshal: func(v interface{}) ([]byte, error) { | |
return msgpack.Marshal(v) | |
}, | |
Unmarshal: func(b []byte, v interface{}) error { | |
return msgpack.Unmarshal(b, v) | |
}, | |
} | |
key := "mykey" | |
obj := &Object{ | |
Str: "mystring", | |
Num: 42, | |
} | |
codec.Set(&cache.Item{ | |
Key: key, | |
Object: obj, | |
Expiration: time.Hour, | |
}) | |
var wanted Object | |
if err := codec.Get(key, &wanted); err == nil { | |
fmt.Println(wanted) | |
} | |
// Output: {mystring 42} | |
} | |
func Example_advancedUsage() { | |
ring := redis.NewRing(&redis.RingOptions{ | |
Addrs: map[string]string{ | |
"server1": ":6379", | |
"server2": ":6380", | |
}, | |
}) | |
codec := &cache.Codec{ | |
Redis: ring, | |
Marshal: func(v interface{}) ([]byte, error) { | |
return msgpack.Marshal(v) | |
}, | |
Unmarshal: func(b []byte, v interface{}) error { | |
return msgpack.Unmarshal(b, v) | |
}, | |
} | |
obj := new(Object) | |
err := codec.Once(&cache.Item{ | |
Key: "mykey", | |
Object: obj, // destination | |
Func: func() (interface{}, error) { | |
return &Object{ | |
Str: "mystring", | |
Num: 42, | |
}, nil | |
}, | |
}) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(obj) | |
// Output: &{mystring 42} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment