caching

package
v2.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2026 License: MIT Imports: 5 Imported by: 0

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]) Get

func (b *DistributedBridge[T]) Get(ctx context.Context, key string) (T, bool, error)

Get retrieves and deserializes a value.

func (*DistributedBridge[T]) Invalidate

func (b *DistributedBridge[T]) Invalidate(ctx context.Context, key string) error

Invalidate removes a key from the cache.

func (*DistributedBridge[T]) Set

func (b *DistributedBridge[T]) Set(ctx context.Context, key string, value T, ttl time.Duration) error

Set serializes and stores a value.

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.

func (*DistributedInMemory) Get

func (m *DistributedInMemory) Get(ctx context.Context, key string) ([]byte, bool, error)

Get implements DistributedCache.Get.

func (*DistributedInMemory) Set

func (m *DistributedInMemory) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error

Set implements DistributedCache.Set.

type DistributedInMemoryOption

type DistributedInMemoryOption func(*DistributedInMemory)

DistributedInMemoryOption configures a DistributedInMemory cache.

func WithDistributedMaxEntries

func WithDistributedMaxEntries(n int) DistributedInMemoryOption

WithDistributedMaxEntries sets the maximum number of entries before eviction.

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]) Get

func (c *InMemoryCache[T]) Get(_ context.Context, key string) (T, bool, error)

Get implements Cache.Get.

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.

func (*InMemoryCache[T]) Set

func (c *InMemoryCache[T]) Set(_ context.Context, key string, value T, ttl time.Duration) error

Set implements Cache.Set.

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.

func WithTTL

func WithTTL[T any](d time.Duration) InMemoryOption[T]

WithTTL sets the default TTL for cache entries.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL