Documentation
¶
Overview ¶
Package cache provides a concurrency-safe in-memory cache with TTL expiration, atomic counters, and cache-backed mutual-exclusion locks.
The API mirrors the cache module of goravel/framework (itself modeled on Laravel's cache): Put/Get/Add/Forever/Remember plus typed getters. Entries expire lazily on access; caches built with NewCache additionally run a background janitor that sweeps expired entries and stops on its own when the cache is garbage collected.
c := cache.NewCache()
_ = c.Put("token", "abc123", time.Minute)
token := c.GetString("token")
value, err := c.Remember("user:1", time.Hour, func() (any, error) {
return loadUser(1)
})
Index ¶
- Constants
- Variables
- func GetAs[T any](c Cache, key string, def ...T) T
- type Cache
- type Lock
- type LockStore
- type Memory
- func (r *Memory) Add(key string, value any, t time.Duration) bool
- func (r *Memory) Decrement(key string, value ...int64) (int64, error)
- func (r *Memory) DeleteExpired()
- func (r *Memory) Flush() bool
- func (r *Memory) Forever(key string, value any) bool
- func (r *Memory) Forget(key string) bool
- func (r *Memory) Get(key string, def ...any) any
- func (r *Memory) GetBool(key string, def ...bool) bool
- func (r *Memory) GetInt(key string, def ...int) int
- func (r *Memory) GetInt64(key string, def ...int64) int64
- func (r *Memory) GetString(key string, def ...string) string
- func (r *Memory) Has(key string) bool
- func (r *Memory) Increment(key string, value ...int64) (int64, error)
- func (r *Memory) Lock(key string, t ...time.Duration) *Lock
- func (r *Memory) Pull(key string, def ...any) any
- func (r *Memory) Put(key string, value any, t time.Duration) error
- func (r *Memory) ReleaseLock(key, owner string) bool
- func (r *Memory) Remember(key string, ttl time.Duration, callback func() (any, error)) (any, error)
- func (r *Memory) RememberForever(key string, callback func() (any, error)) (any, error)
- func (r *Memory) WithContext(_ context.Context) Cache
- type Option
Constants ¶
const ( // NoExpiration indicates that an entry never expires. NoExpiration time.Duration = 0 // DefaultCleanupInterval is how often the janitor started by NewCache // sweeps expired entries. Override it with WithCleanupInterval. DefaultCleanupInterval = 5 * time.Minute )
Variables ¶
var ErrNotInteger = errors.New("cache: value is not an integer")
ErrNotInteger is returned by Increment and Decrement when the stored value is not an integer.
Functions ¶
Types ¶
type Cache ¶
type Cache interface {
// Add an item in the cache if the key does not exist.
Add(key string, value any, t time.Duration) bool
// Decrement decrements the value of an item in the cache.
Decrement(key string, value ...int64) (int64, error)
// Forever add an item in the cache indefinitely.
Forever(key string, value any) bool
// Forget removes an item from the cache.
Forget(key string) bool
// Flush remove all items from the cache.
Flush() bool
// Get retrieve an item from the cache by key.
// The optional default may be a plain value or a func() any that is
// invoked lazily when the key is missing; any other function signature
// is returned as-is.
Get(key string, def ...any) any
// GetBool retrieves an item from the cache by key as a boolean.
GetBool(key string, def ...bool) bool
// GetInt retrieves an item from the cache by key as an integer.
GetInt(key string, def ...int) int
// GetInt64 retrieves an item from the cache by key as a 64-bit integer.
GetInt64(key string, def ...int64) int64
// GetString retrieves an item from the cache by key as a string.
GetString(key string, def ...string) string
// Has check an item exists in the cache.
Has(key string) bool
// Increment increments the value of an item in the cache.
Increment(key string, value ...int64) (int64, error)
// Lock get a lock instance.
Lock(key string, t ...time.Duration) *Lock
// Put stores an item in the cache for a given time.
// NoExpiration (0) means the entry never expires; a negative duration
// stores an entry that is already expired.
Put(key string, value any, t time.Duration) error
// Pull retrieve an item from the cache and delete it.
Pull(key string, def ...any) any
// Remember gets an item from the cache, or execute the given Closure and store the result.
Remember(key string, ttl time.Duration, callback func() (any, error)) (any, error)
// RememberForever get an item from the cache, or execute the given Closure and store the result forever.
RememberForever(key string, callback func() (any, error)) (any, error)
// WithContext returns a Cache instance using the given context for
// operations that support it.
WithContext(ctx context.Context) Cache
}
type Lock ¶
type Lock struct {
// contains filtered or unexported fields
}
Lock is a mutual-exclusion lock backed by a LockStore. Every instance carries a unique owner token: releasing only succeeds while the lock is still held by that owner, so an instance whose lock already expired cannot release a competitor's lock.
func (*Lock) Block ¶
Block waits up to t for the lock to become available, retrying every blockRetryInterval, and reports whether it was acquired. When a callback is given, the lock is released after the callback runs.
func (*Lock) ForceRelease ¶
ForceRelease releases the lock regardless of its current owner.
func (*Lock) Get ¶
Get attempts to acquire the lock and reports whether it succeeded. When a callback is given and the lock is acquired, the callback runs before the lock is released again; Get still returns true when that release fails (e.g. the lock expired while the callback ran), since the callback did execute.
type LockStore ¶ added in v1.3.0
type LockStore interface {
Add(key string, value any, t time.Duration) bool
Forget(key string) bool
ReleaseLock(key, owner string) bool
}
LockStore is the storage a Lock needs: acquisition via Add, owner-checked atomic release via ReleaseLock, and unconditional deletion via Forget. ReleaseLock must delete key only while it still holds owner's token, as a non-atomic check-then-delete could remove a lock acquired by someone else in between. Requiring it at construction time guarantees every acquirable lock can also be released safely.
type Memory ¶
type Memory struct {
// contains filtered or unexported fields
}
Memory is an in-memory Cache implementation. Expired entries are removed lazily on access and, when constructed via NewCache, periodically by a background janitor.
The zero value is ready to use but runs no janitor: entries then expire only lazily, so a workload that writes TTL keys it never reads back will retain them indefinitely. Prefer NewCache, or call DeleteExpired periodically when using the zero value.
func (*Memory) Add ¶
Add an item in the cache if the key does not exist or its current entry has expired.
func (*Memory) DeleteExpired ¶ added in v1.3.0
func (r *Memory) DeleteExpired()
DeleteExpired removes all expired entries. It is called periodically by the janitor started by NewCache and may be called manually on instances constructed without one.
func (*Memory) Increment ¶
Increment increments the value of an item in the cache. Missing keys start at zero without expiration; existing entries keep their expiration and must hold an integer value.
func (*Memory) Put ¶
Put stores an item in the cache for a given time, replacing any previous entry and its expiration. NoExpiration (0) means the entry never expires; a negative duration stores an entry that is already expired.
func (*Memory) ReleaseLock ¶ added in v1.3.0
ReleaseLock atomically deletes key while it still holds owner's token, implementing LockReleaser.
func (*Memory) Remember ¶
Remember Get an item from the cache, or execute the given Closure and store the result. Concurrent calls for the same key share a single callback execution, and a cached nil value counts as a hit.
func (*Memory) RememberForever ¶
RememberForever Get an item from the cache, or execute the given Closure and store the result forever.
type Option ¶ added in v1.3.0
type Option func(*options)
Option configures the Cache returned by NewCache.
func WithCleanupInterval ¶ added in v1.3.0
WithCleanupInterval sets how often the background janitor sweeps expired entries. A zero or negative interval disables the janitor entirely, in which case expired entries are only removed lazily when accessed.