xcache

package
v0.9.43 Latest Latest
Warning

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

Go to latest
Published: May 23, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ModeMemory   = "memory"
	ModeRedis    = "redis"
	ModeTwoLevel = "two-level"
)

Mode represents the cache backend mode

  • memory: pure in-memory
  • redis: pure redis
  • two-level: memory + redis chain

Variables

View Source
var ErrCacheNotConfigured = errors.New("cache not configured")

ErrCacheNotConfigured is returned when trying to get from a noop cache.

Functions

This section is empty.

Types

type Cache

type Cache[T any] = cachelib.CacheInterface[T]

func NewFromConfig

func NewFromConfig[T any](cfg Config) Cache[T]

NewFromConfig builds a typed cache from the given Config. Modes:

  • memory: in-memory only
  • redis: redis only
  • two-level: memory + redis chain

Memory and Redis expiration can be configured separately. If mode is not set or invalid, returns a noop cache that does nothing.

func NewNoop

func NewNoop[T any]() Cache[T]

NewNoop creates a new noop cache that implements the Cache interface. This is useful when cache is not configured but you want to avoid nil checks.

func NewTwoLevel

func NewTwoLevel[T any](memory SetterCache[T], redis SetterCache[T]) Cache[T]

NewTwoLevel constructs a 2-level cache: memory first, then Redis. It takes already constructed setter caches so you can mix any memory/redis implementations. Typical usage: NewTwoLevel(NewMemory(...), NewRedis(...)).

func NewTwoLevelWithClients

func NewTwoLevelWithClients[T any](memClient *gocache.Cache, redisClient *redis.Client, memOptions []Option, redisOptions []Option) Cache[T]

NewTwoLevelWithClients is a convenience to create a 2-level cache from raw clients directly.

type Config

type Config struct {
	Mode   string        `conf:"mode" yaml:"mode" json:"mode"`
	Memory MemoryConfig  `conf:"memory" yaml:"memory" json:"memory"`
	Redis  xredis.Config `conf:"redis" yaml:"redis" json:"redis"`
}

type Entry added in v0.9.28

type Entry[T any] struct {
	Value    T
	IsEmpty  bool // true if this is a negative cache entry (key not found)
	ExpireAt time.Time
}

Entry represents a cache entry with support for negative caching. It can represent either a valid value or a "not found" marker.

func NewEmptyEntry added in v0.9.28

func NewEmptyEntry[T any](ttl time.Duration) *Entry[T]

NewEmptyEntry creates a new negative cache entry (representing "not found").

func NewEntry added in v0.9.28

func NewEntry[T any](value T, ttl time.Duration) *Entry[T]

NewEntry creates a new cache entry with the given value and expiration.

func (*Entry[T]) IsExpired added in v0.9.28

func (e *Entry[T]) IsExpired() bool

IsExpired checks if the entry has expired.

type MemoryConfig

type MemoryConfig struct {
	Expiration      time.Duration `conf:"expiration" yaml:"expiration" json:"expiration"`
	CleanupInterval time.Duration `conf:"cleanup_interval" yaml:"cleanup_interval" json:"cleanup_interval"`
}

type Option

type Option = store.Option

func WithExpiration

func WithExpiration(expiration time.Duration) Option

type SetterCache

type SetterCache[T any] = cachelib.SetterCacheInterface[T]

func NewMemory

func NewMemory[T any](client *gocache.Cache, options ...Option) SetterCache[T]

NewMemory creates a pure in-memory cache using patrickmn/go-cache as the backend. Pass an existing *gocache.Cache so you control default expiration & cleanup interval. Optionally pass store options (e.g., store.WithExpiration) when setting values.

func NewMemoryWithOptions

func NewMemoryWithOptions[T any](defaultExpiration, cleanupInterval time.Duration, options ...Option) SetterCache[T]

NewMemoryWithOptions is a convenience constructor that builds the patrickmn/go-cache client for you using the provided default expiration and cleanup interval.

func NewRedis

func NewRedis[T any](client *redis.Client, options ...Option) SetterCache[T]

NewRedis creates a pure Redis cache using github.com/redis/go-redis/v9 as the client. You can pass store options like store.WithExpiration, store.WithTags, etc.

func NewRedisWithOptions

func NewRedisWithOptions[T any](opts *redis.Options, options ...Option) SetterCache[T]

NewRedisWithOptions builds a redis.Client for you and returns the cache.

Directories

Path Synopsis
Package live provides a generic in-memory cache that automatically refreshes its data periodically or on demand using polling mechanism.
Package live provides a generic in-memory cache that automatically refreshes its data periodically or on demand using polling mechanism.
Package redis is a generated GoMock package.
Package redis is a generated GoMock package.

Jump to

Keyboard shortcuts

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