cache package - github.com/go-redis/cache/v9 - Go Packages (original) (raw)
ring := redis.NewRing(&redis.RingOptions{ Addrs: map[string]string{ "server1": ":6379", "server2": ":6380", }, })
mycache := cache.New(&cache.Options{ Redis: ring, LocalCache: cache.NewTinyLFU(1000, time.Minute), })
obj := new(Object) err := mycache.Once(&cache.Item{ Key: "mykey", Value: obj, // destination Do: func(*cache.Item) (interface{}, error) { return &Object{ Str: "mystring", Num: 42, }, nil }, }) if err != nil { panic(err) } fmt.Println(obj)
Output:
&{mystring 42}
ring := redis.NewRing(&redis.RingOptions{ Addrs: map[string]string{ "server1": ":6379", "server2": ":6380", }, })
mycache := cache.New(&cache.Options{ Redis: ring, LocalCache: cache.NewTinyLFU(1000, time.Minute), })
ctx := context.TODO() key := "mykey" obj := &Object{ Str: "mystring", Num: 42, }
if err := mycache.Set(&cache.Item{ Ctx: ctx, Key: key, Value: obj, TTL: time.Hour, }); err != nil { panic(err) }
var wanted Object if err := mycache.Get(ctx, key, &wanted); err == nil { fmt.Println(wanted) }
Output:
{mystring 42}
- func (cd *Cache) Delete(ctx context.Context, key string) error
- func (cd *Cache) DeleteFromLocalCache(key string)
- func (cd *Cache) Exists(ctx context.Context, key string) bool
- func (cd *Cache) Get(ctx context.Context, key string, value interface{}) error
- func (cd *Cache) GetSkippingLocalCache(ctx context.Context, key string, value interface{}) error
- func (cd *Cache) Marshal(value interface{}) ([]byte, error)
- func (cd *Cache) Once(item *Item) error
- func (cd *Cache) Set(item *Item) error
- func (cd *Cache) Stats() *Stats
- func (cd *Cache) Unmarshal(b []byte, value interface{}) error
This section is empty.
This section is empty.
func (cd *Cache) DeleteFromLocalCache(key string)
Exists reports whether value for the given key exists.
Get gets the value for the given key.
Get gets the value for the given key skipping local cache.
func (cd *Cache) Marshal(value interface{}) ([]byte, error)
func (cd *Cache) Once(item *Item) error
Once gets the item.Value 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.
func (cd *Cache) Stats() *Stats
Stats returns cache statistics.
func (cd *Cache) Unmarshal(b []byte, value interface{}) error
type MarshalFunc func(interface{}) ([]byte, error)
------------------------------------------------------------------------------
type Options struct { Redis rediser LocalCache LocalCache StatsEnabled bool Marshal MarshalFunc Unmarshal UnmarshalFunc }
type UnmarshalFunc func([]byte, interface{}) error
------------------------------------------------------------------------------