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}

This section is empty.

This section is empty.

func New(opt *Options) *Cache

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

------------------------------------------------------------------------------