Documentation
¶
Overview ¶
Example (AdvancedUsage) ¶
ring := redis.NewRing(&redis.RingOptions{
Addrs: map[string]string{
"server1": ":6379",
"server2": ":6380",
},
})
codec := &xservice.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(&xservice.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}
Example (BasicUsage) ¶
ring := redis.NewRing(&redis.RingOptions{
Addrs: map[string]string{
"server1": ":6379",
"server2": ":6380",
},
})
codec := &xservice.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(&xservice.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}
Index ¶
- Variables
- func InitLog()
- type Codec
- func (cd *Codec) Delete(key string) error
- func (cd *Codec) DeleteContext(ctx context.Context, key string) error
- func (cd *Codec) Exists(key string) bool
- func (cd *Codec) ExistsContext(ctx context.Context, key string) bool
- func (cd *Codec) Get(key string, object interface{}) error
- func (cd *Codec) GetContext(ctx context.Context, key string, object interface{}) error
- func (cd *Codec) Once(item *Item) error
- func (cd *Codec) Set(item *Item) error
- func (cd *Codec) Stats() *Stats
- func (cd *Codec) UseLocalCache(maxLen int, expiration time.Duration)
- type IRedis
- type Item
- type Stats
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrCacheMiss = errs.New("cache: key is missing")
Functions ¶
Types ¶
type Codec ¶
type Codec struct {
Redis xservice.IRedis
Marshal func(interface{}) ([]byte, error)
Unmarshal func([]byte, interface{}) error
// contains filtered or unexported fields
}
func (*Codec) GetContext ¶
func (*Codec) Once ¶
Once gets the item.Object for the given item.Key from the cache or executes, caches, and returns the results of the given item.Func, making sure that only one execution is in-flight for a given item.Key at a time. If a duplicate comes in, the duplicate caller waits for the original to complete and receives the same results.
Click to show internal directories.
Click to hide internal directories.