Documentation
¶
Overview ¶
Package cache provides a Laravel-inspired caching library for Go with support for multiple backends (memory, file, Redis) configured via env vars.
Quick start:
c, _ := cache.New()
c.Put(ctx, "key", "value", time.Minute)
result, _ := cache.Remember(ctx, c, "users", time.Hour, func() ([]User, error) {
return db.FindAllUsers()
})
Index ¶
- func Remember[T any](ctx context.Context, c *Cache, key string, ttl time.Duration, ...) (T, error)
- func RememberForever[T any](ctx context.Context, c *Cache, key string, fn func() (T, error)) (T, error)
- func TagRemember[T any](ctx context.Context, t *Tagged, key string, ttl time.Duration, ...) (T, error)
- func TagRememberForever[T any](ctx context.Context, t *Tagged, key string, fn func() (T, error)) (T, error)
- type Cache
- func (c *Cache) Add(ctx context.Context, key string, value any, ttl time.Duration) (bool, error)
- func (c *Cache) Decrement(ctx context.Context, key string, amount int64) (int64, error)
- func (c *Cache) DefaultTTL() time.Duration
- func (c *Cache) Flush(ctx context.Context) error
- func (c *Cache) Forever(ctx context.Context, key string, value any) error
- func (c *Cache) Forget(ctx context.Context, key string) error
- func (c *Cache) Get(ctx context.Context, key string, dest any) (bool, error)
- func (c *Cache) GetInt(ctx context.Context, key string, def int64) int64
- func (c *Cache) GetString(ctx context.Context, key, def string) string
- func (c *Cache) Has(ctx context.Context, key string) bool
- func (c *Cache) Increment(ctx context.Context, key string, amount int64) (int64, error)
- func (c *Cache) Pull(ctx context.Context, key string, dest any) (bool, error)
- func (c *Cache) Put(ctx context.Context, key string, value any, ttl time.Duration) error
- func (c *Cache) Store() Store
- func (c *Cache) Tags(tags ...string) *Tagged
- type Config
- type Driver
- type Store
- type Tagged
- func (t *Tagged) Add(ctx context.Context, key string, value any, ttl time.Duration) (bool, error)
- func (t *Tagged) Decrement(ctx context.Context, key string, amount int64) (int64, error)
- func (t *Tagged) Flush(ctx context.Context) error
- func (t *Tagged) Forever(ctx context.Context, key string, value any) error
- func (t *Tagged) Forget(ctx context.Context, key string) error
- func (t *Tagged) Get(ctx context.Context, key string, dest any) (bool, error)
- func (t *Tagged) Has(ctx context.Context, key string) bool
- func (t *Tagged) Increment(ctx context.Context, key string, amount int64) (int64, error)
- func (t *Tagged) Pull(ctx context.Context, key string, dest any) (bool, error)
- func (t *Tagged) Put(ctx context.Context, key string, value any, ttl time.Duration) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Remember ¶
func Remember[T any](ctx context.Context, c *Cache, key string, ttl time.Duration, fn func() (T, error)) (T, error)
Remember returns the cached value for key. If the key is missing it calls fn, stores the result with ttl, and returns it.
Concurrent calls for the same key on a cache miss are coalesced: only one goroutine executes fn and all others receive the same result, preventing cache stampedes.
users, err := cache.Remember(ctx, c, "users.all", time.Hour, func() ([]User, error) {
return db.FindAllUsers()
})
func RememberForever ¶
func RememberForever[T any](ctx context.Context, c *Cache, key string, fn func() (T, error)) (T, error)
RememberForever is like Remember but stores without expiry. Concurrent misses for the same key are coalesced (stampede-safe).
func TagRemember ¶
func TagRemember[T any](ctx context.Context, t *Tagged, key string, ttl time.Duration, fn func() (T, error)) (T, error)
TagRemember returns the cached value for key under this tag scope. On miss it calls fn, stores the result with ttl, and returns it.
users, err := cache.TagRemember(ctx, c.Tags("users"), "all", time.Hour, func() ([]User, error) {
return db.FindAllUsers()
})
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache wraps a Store with a Laravel-like API.
func New ¶
New creates a Cache using ConfigFromEnv. This is the standard entry point — configure via environment variables.
func NewWithStore ¶
NewWithStore creates a Cache around an existing Store. Useful for testing or when you need to share a store across multiple Cache instances.
func (*Cache) Add ¶
Add stores value only if the key does not already exist. Returns true if the value was stored.
func (*Cache) DefaultTTL ¶
DefaultTTL returns the configured default TTL.
func (*Cache) Get ¶
Get retrieves a cached value into dest (must be a pointer). Returns false if the key does not exist or has expired.
func (*Cache) GetInt ¶
GetInt returns the cached int64 counter value, or def if missing. Only works with keys written via Increment/Decrement.
func (*Cache) Increment ¶
Increment atomically increases an integer counter by amount. Creates the key with value amount if it does not exist.
type Config ¶
type Config struct {
Driver Driver
Prefix string
TTL time.Duration
// Redis
RedisAddr string
RedisPassword string
RedisDB int
// File
FilePath string
}
func ConfigFromEnv ¶
func ConfigFromEnv() Config
ConfigFromEnv loads cache config from environment variables.
CACHE_DRIVER = memory | redis | file (default: memory) CACHE_PREFIX = key prefix (default: "") CACHE_TTL = seconds (default: 3600) CACHE_REDIS_ADDR = host:port (default: 127.0.0.1:6379) CACHE_REDIS_PASSWORD = (default: "") CACHE_REDIS_DB = 0 (default: 0) CACHE_FILE_PATH = /path/to/cache (default: OS cache dir + "/go-cache")
type Store ¶
type Store interface {
Get(ctx context.Context, key string) ([]byte, bool, error)
Put(ctx context.Context, key string, value []byte, ttl time.Duration) error
Forever(ctx context.Context, key string, value []byte) error
Add(ctx context.Context, key string, value []byte, ttl time.Duration) (bool, error)
Increment(ctx context.Context, key string, amount int64) (int64, error)
Decrement(ctx context.Context, key string, amount int64) (int64, error)
Forget(ctx context.Context, key string) error
Flush(ctx context.Context) error
Has(ctx context.Context, key string) (bool, error)
}
Store is the low-level interface every driver must satisfy.
type Tagged ¶
type Tagged struct {
// contains filtered or unexported fields
}
Tagged is a cache scope tied to one or more tags. Calling Flush on a Tagged invalidates every entry stored under those tags without touching the rest of the cache.
tc := c.Tags("users", "roles")
tc.Put(ctx, "admin", user, time.Hour)
tc.Flush(ctx) // only invalidates "users"+"roles" entries
func (*Tagged) Flush ¶
Flush invalidates all entries under these tags by rotating each tag's version token. Old entries become unreachable immediately and will either expire (TTL entries) or be orphaned (forever entries, cleaned up on next full Flush of the underlying store).