Help us shape the future of Go! Take the 2026 Go Developer Survey

cache

package
v1.11.9 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package cache defines cache stores and bounded memory and Redis implementations. Cached values are optimization data and must not be the sole source of durable application correctness.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BoundedConfig

type BoundedConfig struct {
	MaxEntries int
}

BoundedConfig defines the maximum number of process-local cache entries.

type BoundedMemoryStore

type BoundedMemoryStore struct {
	// contains filtered or unexported fields
}

BoundedMemoryStore implements an in-memory Store with strict capacity limits to prevent OOM memory growth.

Example
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/saifsilver/goplusplus/cache"
)

func main() {
	ctx := context.Background()
	store := cache.NewBoundedMemoryStore(100)
	_ = store.Set(ctx, "query:123", "result_data", 5*time.Minute)
	val, _, _ := store.Get(ctx, "query:123")
	fmt.Println(val)
}
Output:
result_data

func NewBoundedMemoryStore

func NewBoundedMemoryStore(maxEntries int) *BoundedMemoryStore

NewBoundedMemoryStore initializes a capacity-capped in-memory cache store.

func (*BoundedMemoryStore) Delete

func (s *BoundedMemoryStore) Delete(ctx context.Context, key string) error

Delete removes key from the bounded memory store.

func (*BoundedMemoryStore) Get

func (s *BoundedMemoryStore) Get(ctx context.Context, key string) (any, bool, error)

Get returns a live cached value when present.

func (*BoundedMemoryStore) GetOrSet

func (s *BoundedMemoryStore) GetOrSet(ctx context.Context, key string, ttl time.Duration, fetcher func() (any, error)) (any, error)

GetOrSet coalesces concurrent misses and caches one fetched value.

func (*BoundedMemoryStore) InvalidatePrefix

func (s *BoundedMemoryStore) InvalidatePrefix(ctx context.Context, prefix string) error

InvalidatePrefix removes keys that begin with prefix.

func (*BoundedMemoryStore) Set

func (s *BoundedMemoryStore) Set(ctx context.Context, key string, val any, ttl time.Duration) error

Set stores a value and evicts one entry when capacity is reached.

type Client

type Client = MemoryStore

Legacy Client alias for backwards compatibility

func NewClient

func NewClient() *Client

NewClient returns the legacy process-local cache client.

type Codec

type Codec interface {
	Marshal(value any) ([]byte, error)
	Unmarshal(data []byte) (any, error)
}

Codec serializes values stored by RedisStore.

type JSONCodec

type JSONCodec struct{}

JSONCodec is the safe default Redis cache codec. Decoded objects use the standard encoding/json representations such as map[string]any.

func (JSONCodec) Marshal

func (JSONCodec) Marshal(value any) ([]byte, error)

Marshal encodes value as JSON.

func (JSONCodec) Unmarshal

func (JSONCodec) Unmarshal(data []byte) (any, error)

Unmarshal decodes JSON into standard Go JSON representations.

type MemoryStore

type MemoryStore struct {
	// contains filtered or unexported fields
}

MemoryStore implements an in-memory Store with single-flight stampede protection.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore initializes a local in-memory cache store.

func (*MemoryStore) Delete

func (s *MemoryStore) Delete(ctx context.Context, key string) error

Delete removes a process-local cache entry.

func (*MemoryStore) Get

func (s *MemoryStore) Get(ctx context.Context, key string) (any, bool, error)

Get returns a live process-local value when present.

func (*MemoryStore) GetOrSet

func (s *MemoryStore) GetOrSet(ctx context.Context, key string, ttl time.Duration, fetcher func() (any, error)) (any, error)

GetOrSet coalesces concurrent process-local misses and caches the fetched value.

func (*MemoryStore) InvalidatePrefix

func (s *MemoryStore) InvalidatePrefix(ctx context.Context, prefix string) error

InvalidatePrefix removes process-local keys that begin with prefix.

func (*MemoryStore) Set

func (s *MemoryStore) Set(ctx context.Context, key string, val any, ttl time.Duration) error

Set stores a process-local value until its TTL expires.

type MultiLevelStore

type MultiLevelStore struct {
	// contains filtered or unexported fields
}

MultiLevelStore provides L1 Memory + L2 Redis tiered caching.

func NewMultiLevelStore

func NewMultiLevelStore(l1, l2 Store) *MultiLevelStore

NewMultiLevelStore creates a multi-level tiered cache store.

func (*MultiLevelStore) Delete

func (m *MultiLevelStore) Delete(ctx context.Context, key string) error

Delete removes key from both cache levels and joins failures.

func (*MultiLevelStore) Get

func (m *MultiLevelStore) Get(ctx context.Context, key string) (any, bool, error)

Get reads L1 before L2 and repopulates L1 with the remaining lifetime.

func (*MultiLevelStore) GetOrSet

func (m *MultiLevelStore) GetOrSet(ctx context.Context, key string, ttl time.Duration, fetcher func() (any, error)) (any, error)

GetOrSet coalesces misses and populates both cache levels.

func (*MultiLevelStore) InvalidatePrefix

func (m *MultiLevelStore) InvalidatePrefix(ctx context.Context, prefix string) error

InvalidatePrefix removes matching keys from both levels and joins failures.

func (*MultiLevelStore) Set

func (m *MultiLevelStore) Set(ctx context.Context, key string, val any, ttl time.Duration) error

Set writes the authoritative L2 before L1.

type RedisConfig

type RedisConfig struct {
	URL          string
	Prefix       string
	Codec        Codec
	PingTimeout  time.Duration
	DialTimeout  time.Duration
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
	ScanCount    int64
	LoadLockTTL  time.Duration
	LoadPoll     time.Duration
}

RedisConfig controls connection validation, namespacing, and serialization.

type RedisStore

type RedisStore struct {
	// contains filtered or unexported fields
}

RedisStore implements Store using the official Redis Go client.

func NewRedisClient

func NewRedisClient(ctx context.Context, redisURL string) (*RedisStore, error)

NewRedisClient is a compatibility convenience for URL-based construction.

func NewRedisStore

func NewRedisStore(ctx context.Context, config RedisConfig) (*RedisStore, error)

NewRedisStore creates and verifies a Redis-backed cache. URL is required and may use redis:// or rediss:// syntax.

func NewRedisStoreFromClient

func NewRedisStoreFromClient(ctx context.Context, client redis.UniversalClient, config RedisConfig) (*RedisStore, error)

NewRedisStoreFromClient verifies and wraps an application-owned Redis client. Closing the store does not close the shared client.

func (*RedisStore) Client

func (s *RedisStore) Client() redis.UniversalClient

Client exposes the shared Redis client for other Redis-backed framework components such as distributed idempotency.

func (*RedisStore) Close

func (s *RedisStore) Close() error

Close releases the Redis client only when the store owns it.

func (*RedisStore) Delete

func (s *RedisStore) Delete(ctx context.Context, key string) error

Delete asynchronously unlinks a Redis cache key.

func (*RedisStore) Get

func (s *RedisStore) Get(ctx context.Context, key string) (any, bool, error)

Get retrieves and decodes a Redis cache value.

func (*RedisStore) GetOrSet

func (s *RedisStore) GetOrSet(ctx context.Context, key string, ttl time.Duration, fetcher func() (any, error)) (any, error)

GetOrSet coordinates misses across local callers and Redis-backed instances.

func (*RedisStore) InvalidatePrefix

func (s *RedisStore) InvalidatePrefix(ctx context.Context, prefix string) error

InvalidatePrefix scans and unlinks namespaced keys beginning with prefix.

func (*RedisStore) Set

func (s *RedisStore) Set(ctx context.Context, key string, value any, ttl time.Duration) error

Set encodes and stores a Redis value with a positive TTL.

type Store

type Store interface {
	Get(ctx context.Context, key string) (any, bool, error)
	Set(ctx context.Context, key string, val any, ttl time.Duration) error
	Delete(ctx context.Context, key string) error
	GetOrSet(ctx context.Context, key string, ttl time.Duration, fetcher func() (any, error)) (any, error)
	InvalidatePrefix(ctx context.Context, prefix string) error
}

Store defines the universal caching contract for all in-memory and distributed cache providers.

Jump to

Keyboard shortcuts

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