Documentation
¶
Overview ¶
Package processcache provides a bounded, thread-safe, in-process LRU cache for Go applications that need fast local caching without Redis, Memcached, a database, an HTTP server, or any runtime sidecar.
Example usage:
cache, err := processcache.NewMemoryCache(
processcache.WithCleanupDisabled(),
processcache.WithMaxSize(10*processcache.MB),
)
if err != nil {
panic(err)
}
defer cache.Close()
cache.Set("user:1", "Tonmoy", time.Minute)
name, ok := processcache.GetAs[string](cache, "user:1")
fmt.Println(name, ok)
Index ¶
- Constants
- Variables
- func GetAs[T any](c Cache, key string) (T, bool)
- type Cache
- type Clock
- type Config
- type MemoryCache
- type Option
- func WithCleanupDisabled() Option
- func WithCleanupInterval(interval time.Duration) Option
- func WithClock(clock Clock) Option
- func WithMaxSize(bytes int64) Option
- func WithMetrics(enabled bool) Option
- func WithSizer(sizer Sizer) Option
- func WithTypeLimit(prefix string, bytes int64) Option
- func WithTypeLimits(limits ...TypeLimit) Option
- type Sizer
- type Stats
- type TypeLimit
Constants ¶
Variables ¶
var ( // ErrInvalidMaxSize reports a non-positive cache size limit. ErrInvalidMaxSize = internal.ErrInvalidMaxSize // ErrInvalidCleanupInterval reports a non-positive cleanup interval when cleanup is enabled. ErrInvalidCleanupInterval = internal.ErrInvalidCleanupInterval // ErrInvalidTypePrefix reports an empty type-prefix quota key. ErrInvalidTypePrefix = internal.ErrInvalidTypePrefix // ErrInvalidTypeLimit reports a non-positive type quota. ErrInvalidTypeLimit = internal.ErrInvalidTypeLimit // ErrDuplicateTypePrefix reports duplicate configured type prefixes. ErrDuplicateTypePrefix = internal.ErrDuplicateTypePrefix // ErrOverlappingTypePrefix reports prefixes whose match ranges overlap. ErrOverlappingTypePrefix = internal.ErrOverlappingTypePrefix // ErrNilSizer reports a nil sizer implementation. ErrNilSizer = internal.ErrNilSizer // ErrNilClock reports a nil clock implementation. ErrNilClock = internal.ErrNilClock )
Functions ¶
Types ¶
type Cache ¶
type Cache interface {
Get(key string) (any, bool)
Set(key string, value any, ttl ...time.Duration) bool
Delete(key string) bool
Exists(key string) bool
Clear()
Len() int
Stats() Stats
Close() error
}
Cache is the minimal concurrent cache contract exposed by this package.
type Clock ¶
Clock provides time to the cache for expiration logic.
Implementations must not call back into the cache; doing so may deadlock.
type Config ¶
Config configures a MemoryCache instance.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns the package defaults for a new MemoryCache.
type MemoryCache ¶
type MemoryCache = internal.MemoryCache
MemoryCache is the in-memory LRU cache implementation.
func NewMemoryCache ¶
func NewMemoryCache(opts ...Option) (*MemoryCache, error)
NewMemoryCache constructs a MemoryCache from functional options.
func NewMemoryCacheFromConfig ¶
func NewMemoryCacheFromConfig(cfg Config) (*MemoryCache, error)
NewMemoryCacheFromConfig constructs a MemoryCache from an explicit Config.
Callers typically start from DefaultConfig and then override selected fields.
type Option ¶
Option mutates a Config during construction.
func WithCleanupDisabled ¶
func WithCleanupDisabled() Option
WithCleanupDisabled disables the background expiration sweeper.
func WithCleanupInterval ¶
WithCleanupInterval sets the background expiration sweep interval.
func WithMaxSize ¶
WithMaxSize sets the global cache size limit in bytes.
func WithMetrics ¶
WithMetrics enables or disables atomic stats counters.
func WithTypeLimit ¶
WithTypeLimit adds one prefix-scoped quota.
func WithTypeLimits ¶
WithTypeLimits adds multiple prefix-scoped quotas.
type Sizer ¶
Sizer estimates the cache cost of one entry.
Implementations must not call back into the cache; doing so may deadlock.