Simple Go caching library with built-in Memcached support. It wraps a getter function and caches its results.
go get github.com/macsko/cacher
By default, support for Memcached caching method is added. To use it, it is required to download and run the Memcached. Cacher uses rainycape/memcache library as a client.
- Create new Cacher using a Memcached client with 10 seconds expiration and
getter
function
mc, err := memcache.New("10.0.0.1:11211")
...
c := cacher.NewCacher[string, int](
memcached.NewMemcached[int](mc, 10),
getter,
)
- Get value
value, err := c.Get(ctx, "key")
Core (Cacher) code of the library is open to use with other caching mechanisms by passing a structure fulfilling the Cache
interface:
type Cache[K comparable, T any] interface {
Get(ctx context.Context, key K) (T, error)
Set(ctx context.Context, key K, value T) error
}
Note: ErrNoKey should be returned by Get method if the key is not present.