caching API

caching

package

API reference for the caching package.

I
interface

Cache

Cache defines the generic caching contract.

pkg/caching/cache.go:17-27
type Cache interface

Example

cache := caching.NewInMemory[string](caching.WithTTL[string](5*time.Minute))
cache.Set(ctx, "key", "value", 0)
val, ok, err := cache.Get(ctx, "key")

Methods

Get
Method

Parameters

key string

Returns

T
bool
error
func Get(...)
Set
Method

Parameters

key string
value T

Returns

error
func Set(...)
Invalidate
Method

Parameters

key string

Returns

error
func Invalidate(...)
S
struct

entry

entry holds a cached value with its expiry time.

pkg/caching/cache.go:30-33
type entry struct

Fields

Name Type Description
value T
expiry time.Time
S
struct

InMemoryCache

InMemoryCache is a thread-safe in-memory implementation of Cache.

pkg/caching/cache.go:41-46
type InMemoryCache struct

Example

cache := caching.NewInMemory[string]()
cache.Set(ctx, "greeting", "hello", time.Minute)

Fields

Name Type Description
mu sync.RWMutex
data map[string]entry[T]
defaultTTL time.Duration
maxEntries int
T
type

InMemoryOption

InMemoryOption configures an InMemoryCache.

pkg/caching/cache.go:49-49
type InMemoryOption func(*InMemoryCache[T])
F
function

NewInMemory

NewInMemory creates a new InMemoryCache with optional configuration.

Parameters

opts
...InMemoryOption[T]

Returns

*InMemoryCache[T]
pkg/caching/cache.go:59-67
func NewInMemory[T any](opts ...InMemoryOption[T]) *InMemoryCache[T]

{
	c := &InMemoryCache[T]{
		data: make(map[string]entry[T]),
	}
	for _, opt := range opts {
		opt(c)
	}
	return c
}

Example

cache := caching.NewInMemory[string](
    caching.WithTTL[string](time.Minute),
    caching.WithMaxEntries[string](1000),
)
F
function

WithTTL

WithTTL sets the default TTL for cache entries.

Parameters

Returns

InMemoryOption[T]
pkg/caching/cache.go:70-72
func WithTTL[T any](d time.Duration) InMemoryOption[T]

{
	return func(c *InMemoryCache[T]) { c.defaultTTL = d }
}
F
function

WithMaxEntries

WithMaxEntries sets the maximum number of entries before eviction.

Parameters

n
int

Returns

InMemoryOption[T]
pkg/caching/cache.go:75-77
func WithMaxEntries[T any](n int) InMemoryOption[T]

{
	return func(c *InMemoryCache[T]) { c.maxEntries = n }
}
I
interface

DistributedCache

DistributedCache is a byte-level cache backend for distributed systems.
Implementations include Redis, Memcached, etc.

pkg/caching/distributed.go:12-16
type DistributedCache interface

Methods

Get
Method

Parameters

key string

Returns

[]byte
bool
error
func Get(...)
Set
Method

Parameters

key string
value []byte

Returns

error
func Set(...)
Delete
Method

Parameters

key string

Returns

error
func Delete(...)
S
struct

DistributedBridge

DistributedBridge adapts a DistributedCache to a typed Cache[T] using JSON.

pkg/caching/distributed.go:19-21
type DistributedBridge struct

Fields

Name Type Description
inner DistributedCache
F
function

NewDistributedBridge

NewDistributedBridge creates a typed cache bridge over a DistributedCache backend.

Parameters

Returns

*DistributedBridge[T]
pkg/caching/distributed.go:24-26
func NewDistributedBridge[T any](backend DistributedCache) *DistributedBridge[T]

{
	return &DistributedBridge[T]{inner: backend}
}
S
struct
Implements: DistributedCache

DistributedInMemory

DistributedInMemory is an in-memory implementation of DistributedCache
suitable for testing and single-process scenarios.

pkg/caching/distributed.go:61-65
type DistributedInMemory struct

Methods

Get
Method

Get implements DistributedCache.Get.

Parameters

key string

Returns

[]byte
bool
error
func (*DistributedInMemory) Get(ctx context.Context, key string) ([]byte, bool, error)
{
	m.mu.RLock()
	e, ok := m.data[key]
	m.mu.RUnlock()

	if !ok {
		return nil, false, nil
	}

	if !e.expiry.IsZero() && time.Now().After(e.expiry) {
		m.mu.Lock()
		delete(m.data, key)
		m.mu.Unlock()
		return nil, false, nil
	}

	return e.value, true, nil
}
Set
Method

Set implements DistributedCache.Set.

Parameters

key string
value []byte

Returns

error
func (*DistributedInMemory) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
{
	e := distributedEntry{value: value}
	if ttl > 0 {
		e.expiry = time.Now().Add(ttl)
	} else if m.ttl > 0 {
		e.expiry = time.Now().Add(m.ttl)
	}

	m.mu.Lock()
	m.data[key] = e
	m.mu.Unlock()
	return nil
}
Delete
Method

Delete implements DistributedCache.Delete.

Parameters

key string

Returns

error
func (*DistributedInMemory) Delete(ctx context.Context, key string) error
{
	m.mu.Lock()
	delete(m.data, key)
	m.mu.Unlock()
	return nil
}

Fields

Name Type Description
mu sync.RWMutex
data map[string]distributedEntry
ttl time.Duration
S
struct

distributedEntry

pkg/caching/distributed.go:67-70
type distributedEntry struct

Fields

Name Type Description
value []byte
expiry time.Time
F
function

NewDistributedInMemory

NewDistributedInMemory creates a new in-memory distributed cache.

Parameters

opts
...DistributedInMemoryOption
pkg/caching/distributed.go:73-81
func NewDistributedInMemory(opts ...DistributedInMemoryOption) *DistributedInMemory

{
	c := &DistributedInMemory{
		data: make(map[string]distributedEntry),
	}
	for _, opt := range opts {
		opt(c)
	}
	return c
}
T
type

DistributedInMemoryOption

DistributedInMemoryOption configures a DistributedInMemory cache.

pkg/caching/distributed.go:84-84
type DistributedInMemoryOption func(*DistributedInMemory)
F
function

WithDistributedTTL

WithDistributedTTL sets the default TTL for entries.

pkg/caching/distributed.go:87-89
func WithDistributedTTL(d time.Duration) DistributedInMemoryOption

{
	return func(c *DistributedInMemory) { c.ttl = d }
}