modulepackage
5.0.4+incompatible
Repository: https://github.com/go-redis/cache.git
Documentation: pkg.go.dev
# README
Redis cache library for Golang 
Installation
go get gopkg.in/go-redis/cache.v5
Quickstart
package cache_test
import (
"fmt"
"time"
"gopkg.in/go-redis/cache.v5"
"gopkg.in/redis.v5"
"gopkg.in/vmihailenco/msgpack.v2"
)
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)
},
}
v, err := codec.Do(&cache.Item{
Key: "mykey",
Object: new(Object), // destination
Func: func() (interface{}, error) {
return &Object{
Str: "mystring",
Num: 42,
}, nil
},
})
if err != nil {
panic(err)
}
fmt.Println(v.(*Object))
// Output: &{mystring 42}
}
# Packages
No description provided by the author
# Variables
No description provided by the author