Documentation
¶
Overview ¶
Package caching provides a generic, thread-safe in-memory cache with TTL support.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[T any] interface { // Get retrieves a value by key. Returns the value, true if found, and any error. Get(ctx context.Context, key string) (T, bool, error) // Set stores a value with an optional per-call TTL. If ttl is 0, the default // TTL configured on the cache is used. Set(ctx context.Context, key string, value T, ttl time.Duration) error // Invalidate removes a key from the cache. Invalidate(ctx context.Context, key string) error }
Cache defines the generic caching contract.
Example:
cache := caching.NewInMemory[string](caching.WithTTL[string](5*time.Minute)) cache.Set(ctx, "key", "value", 0) val, ok, err := cache.Get(ctx, "key")
type DistributedBridge ¶
type DistributedBridge[T any] struct { // contains filtered or unexported fields }
DistributedBridge adapts a DistributedCache to a typed Cache[T] using JSON.
func NewDistributedBridge ¶
func NewDistributedBridge[T any](backend DistributedCache) *DistributedBridge[T]
NewDistributedBridge creates a typed cache bridge over a DistributedCache backend.
func (*DistributedBridge[T]) Invalidate ¶
func (b *DistributedBridge[T]) Invalidate(ctx context.Context, key string) error
Invalidate removes a key from the cache.
type DistributedCache ¶
type DistributedCache interface {
Get(ctx context.Context, key string) ([]byte, bool, error)
Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
Delete(ctx context.Context, key string) error
}
DistributedCache is a byte-level cache backend for distributed systems. Implementations include Redis, Memcached, etc.
type DistributedInMemory ¶
type DistributedInMemory struct {
// contains filtered or unexported fields
}
DistributedInMemory is an in-memory implementation of DistributedCache suitable for testing and single-process scenarios.
func NewDistributedInMemory ¶
func NewDistributedInMemory(opts ...DistributedInMemoryOption) *DistributedInMemory
NewDistributedInMemory creates a new in-memory distributed cache.
func (*DistributedInMemory) Delete ¶
func (m *DistributedInMemory) Delete(ctx context.Context, key string) error
Delete implements DistributedCache.Delete.
type DistributedInMemoryOption ¶
type DistributedInMemoryOption func(*DistributedInMemory)
DistributedInMemoryOption configures a DistributedInMemory cache.
func WithDistributedTTL ¶
func WithDistributedTTL(d time.Duration) DistributedInMemoryOption
WithDistributedTTL sets the default TTL for entries.
type InMemoryCache ¶
type InMemoryCache[T any] struct { // contains filtered or unexported fields }
InMemoryCache is a thread-safe in-memory implementation of Cache.
Example:
cache := caching.NewInMemory[string]() cache.Set(ctx, "greeting", "hello", time.Minute)
func NewInMemory ¶
func NewInMemory[T any](opts ...InMemoryOption[T]) *InMemoryCache[T]
NewInMemory creates a new InMemoryCache with optional configuration.
Example:
cache := caching.NewInMemory[string](
caching.WithTTL[string](time.Minute),
caching.WithMaxEntries[string](1000),
)
func (*InMemoryCache[T]) Invalidate ¶
func (c *InMemoryCache[T]) Invalidate(_ context.Context, key string) error
Invalidate implements Cache.Invalidate.
func (*InMemoryCache[T]) Len ¶
func (c *InMemoryCache[T]) Len() int
Len returns the number of entries currently in the cache.
type InMemoryOption ¶
type InMemoryOption[T any] func(*InMemoryCache[T])
InMemoryOption configures an InMemoryCache.
func WithMaxEntries ¶
func WithMaxEntries[T any](n int) InMemoryOption[T]
WithMaxEntries sets the maximum number of entries before eviction.