Documentation
¶
Overview ¶
Package cache keeps values that are slow to make: load results, pages rendered on demand, answers from other services. A Cache sits on a Store, in memory, on disk or in Redis; with Redis or a shared disk, every process of the app shares it.
c := cache.New(cache.Memory(64 << 20))
posts, err := cache.Get(ctx, c, "posts:recent", cache.For(time.Minute, "posts"), func(ctx context.Context) ([]Post, error) {
return db.RecentPosts(ctx)
})
...
err = c.Invalidate(ctx, "posts") // after a post changes
Entries carry tags. Invalidating a tag makes every entry with it stale at once, in every process: each tag has a version in the store, and an entry is fresh only while its tags' versions are the ones it was made with.
While an entry is made, other callers for the same key in the process wait for it instead of making it too.
Index ¶
- func Get[T any](ctx context.Context, c *Cache, key string, o Options, ...) (T, error)
- func NoStore() error
- type Cache
- func (c *Cache) Delete(ctx context.Context, keys ...string) error
- func (c *Cache) Fetch(ctx context.Context, key string, o Options, ...) ([]byte, error)
- func (c *Cache) Invalidate(ctx context.Context, tags ...string) error
- func (c *Cache) Lookup(ctx context.Context, key string) (Entry, bool, error)
- func (c *Cache) Make(ctx context.Context, key string, o Options, ...) ([]byte, error)
- func (c *Cache) Put(ctx context.Context, key string, value []byte, o Options) error
- func (c *Cache) Store() Store
- type Entry
- type Options
- type Store
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache keeps entries in a Store, with tags. Create it with New.
func (*Cache) Fetch ¶
func (c *Cache) Fetch(ctx context.Context, key string, o Options, fn func(ctx context.Context) ([]byte, error)) ([]byte, error)
Fetch returns the entry at key if it's fresh; otherwise it calls fn, stores what it returns, and returns that. A stale entry within o.Stale is returned at once while fn runs in the background. Callers for the same key in this process share one call of fn.
func (*Cache) Invalidate ¶
Invalidate makes every entry with any of tags stale, in every process that shares the store.
func (*Cache) Make ¶
func (c *Cache) Make(ctx context.Context, key string, o Options, fn func(ctx context.Context) ([]byte, []string, error)) ([]byte, error)
Make makes the entry at key with fn and stores it, whether or not one is there, with o.Tags and the tags fn returns: those found while making it, such as what a page's loads read. Callers for the same key in this process share one call of fn.
type Entry ¶
type Entry struct {
Value []byte
// Made is when it was stored.
Made time.Time
// Fresh is false once its TTL passed or a tag was invalidated.
Fresh bool
Tags []string
}
Entry is a stored value with when it was made and its tags' versions.
type Options ¶
type Options struct {
// TTL is how long the entry is fresh; 0 is until a tag is invalidated.
TTL time.Duration
// Stale is how long after TTL the entry may still be served while a
// fresh one is made in the background: stale-while-revalidate.
Stale time.Duration
Tags []string
}
Options say how long an entry is fresh, and its tags.
type Store ¶
type Store interface {
// Get returns the value of key, or ok false if there's none or it
// expired.
Get(ctx context.Context, key string) (value []byte, ok bool, err error)
// Set stores value for ttl; 0 keeps it until it's evicted.
Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
// Delete removes keys.
Delete(ctx context.Context, keys ...string) error
// Incr adds one to the number at key, starting from 0, and returns it.
Incr(ctx context.Context, key string) (int64, error)
}
Store keeps bytes by key. Memory, Disk and Redis are stores; a Store of your own works too.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package redis is a cache.Store in Redis, or anything that speaks its protocol (Valkey, KeyDB, Dragonfly), shared by every process of the app.
|
Package redis is a cache.Store in Redis, or anything that speaks its protocol (Valkey, KeyDB, Dragonfly), shared by every process of the app. |