Documentation
¶
Overview ¶
Package cache provides in-memory implementations of the core cache.Cache interface.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewMemoryCache ¶
NewMemoryCache creates an in-memory cache. If opts.CleanupInterval is positive, a background goroutine periodically removes expired entries; it is stopped by Close.
func NewRedisCache ¶
func NewRedisCache(opts RedisOptions) cache.Cache
NewRedisCache creates a Redis-backed cache. Close releases the underlying client connections.
Types ¶
type EvictionPolicy ¶
type EvictionPolicy string
EvictionPolicy selects which entry to remove when the cache is full.
const ( // EvictOldest removes the entry inserted least recently (by insertion time). EvictOldest EvictionPolicy = "oldest" // EvictLRU removes the entry accessed least recently. EvictLRU EvictionPolicy = "lru" )
type Options ¶
type Options struct {
// DefaultTTL is applied to entries stored without an explicit TTL.
// A non-positive value means entries never expire by default.
DefaultTTL time.Duration
// MaxSize bounds the number of entries. 0 means unbounded.
MaxSize int
// MaxBytes bounds the total size of stored data in bytes. 0 means unbounded.
MaxBytes int64
// CleanupInterval sets the period for the background expiry sweep.
// A non-positive value disables the background goroutine.
CleanupInterval time.Duration
// Eviction is the policy used when MaxSize or MaxBytes is exceeded.
// Defaults to EvictOldest.
Eviction EvictionPolicy
// Logger is used for debug logging. Optional.
Logger logger.Logger
}
Options configures a memory cache.
type RedisOptions ¶
type RedisOptions struct {
// Addr is the Redis server address (host:port).
Addr string
// DB is the Redis database number.
DB int
// Username for Redis authentication. Optional.
Username string
// Password for Redis authentication. Optional.
Password string
// DefaultTTL is applied to entries stored without an explicit TTL.
// A non-positive value means entries never expire by default.
DefaultTTL time.Duration
// KeyPrefix is prepended to all cache keys. Defaults to "tnet:cache:".
KeyPrefix string
// Logger is used for debug logging. Optional.
Logger logger.Logger
}
RedisOptions configures a Redis cache.