cache

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2026 License: Apache-2.0, BSD-3-Clause, MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetJSON

func GetJSON(ctx context.Context, c Cache, key string, v interface{}) error

GetJSON retrieves and unmarshals a JSON value

func SetJSON

func SetJSON(ctx context.Context, c Cache, key string, v interface{}, ttl time.Duration) error

SetJSON marshals and stores a JSON value

Types

type Cache

type Cache interface {
	// Get retrieves a value from the cache
	Get(ctx context.Context, key string) ([]byte, error)
	// Set stores a value in the cache with TTL
	Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
	// Delete removes a value from the cache
	Delete(ctx context.Context, key string) error
	// Exists checks if a key exists
	Exists(ctx context.Context, key string) (bool, error)
	// Clear removes all keys matching a pattern
	Clear(ctx context.Context, pattern string) error
	// Close closes the cache connection
	Close() error
	// Ping checks cache connectivity
	Ping(ctx context.Context) error
	// Stats returns cache statistics
	Stats(ctx context.Context) (*Stats, error)
}

Cache is the interface for cache implementations

func New

func New(cfg *Config) (Cache, error)

New creates a new cache based on configuration

type Config

type Config struct {
	// Type: none (disabled), memory (default), valkey, redis
	Type string `yaml:"type"`

	// Connection URL (takes precedence over host/port)
	URL string `yaml:"url"`

	// Individual connection settings
	Host     string `yaml:"host"`
	Port     int    `yaml:"port"`
	Password string `yaml:"password"`
	DB       int    `yaml:"db"`

	// Pool settings
	PoolSize int           `yaml:"pool_size"`
	MinIdle  int           `yaml:"min_idle"`
	Timeout  time.Duration `yaml:"timeout"`

	// Key prefix
	Prefix string `yaml:"prefix"`

	// Default TTL
	TTL int `yaml:"ttl"` // seconds

	// Cluster mode
	Cluster      bool     `yaml:"cluster"`
	ClusterNodes []string `yaml:"cluster_nodes"`

	// Memory cache specific
	MaxSize int `yaml:"max_size"` // Max items for memory cache
}

Config holds cache configuration per AI.md PART 18

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns default cache configuration

type MemoryCache

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

MemoryCache implements Cache interface using in-memory storage

func NewMemoryCache

func NewMemoryCache(maxSize int, defaultTTL time.Duration) *MemoryCache

NewMemoryCache creates a new memory cache

func (*MemoryCache) Clear

func (c *MemoryCache) Clear(ctx context.Context, pattern string) error

Clear removes all keys matching a pattern

func (*MemoryCache) Close

func (c *MemoryCache) Close() error

Close closes the cache (no-op for memory cache)

func (*MemoryCache) Delete

func (c *MemoryCache) Delete(ctx context.Context, key string) error

Delete removes a value from the cache

func (*MemoryCache) Exists

func (c *MemoryCache) Exists(ctx context.Context, key string) (bool, error)

Exists checks if a key exists in the cache

func (*MemoryCache) Get

func (c *MemoryCache) Get(ctx context.Context, key string) ([]byte, error)

Get retrieves a value from the cache

func (*MemoryCache) Ping

func (c *MemoryCache) Ping(ctx context.Context) error

Ping checks cache connectivity (always succeeds for memory cache)

func (*MemoryCache) Set

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

Set stores a value in the cache with TTL

func (*MemoryCache) Stats

func (c *MemoryCache) Stats(ctx context.Context) (*Stats, error)

Stats returns cache statistics

type RedisCache

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

RedisCache implements Cache interface using Valkey/Redis Per AI.md PART 18: Uses github.com/redis/go-redis/v9

func NewRedisCache

func NewRedisCache(cfg *RedisConfig) (*RedisCache, error)

NewRedisCache creates a new Redis/Valkey cache

func (*RedisCache) Clear

func (c *RedisCache) Clear(ctx context.Context, pattern string) error

Clear removes all keys matching a pattern

func (*RedisCache) Close

func (c *RedisCache) Close() error

Close closes the Redis connection

func (*RedisCache) Delete

func (c *RedisCache) Delete(ctx context.Context, key string) error

Delete removes a value from Redis

func (*RedisCache) Exists

func (c *RedisCache) Exists(ctx context.Context, key string) (bool, error)

Exists checks if a key exists in Redis

func (*RedisCache) Expire

func (c *RedisCache) Expire(ctx context.Context, key string, ttl time.Duration) error

Expire sets TTL on an existing key

func (*RedisCache) Get

func (c *RedisCache) Get(ctx context.Context, key string) ([]byte, error)

Get retrieves a value from Redis

func (*RedisCache) HGet

func (c *RedisCache) HGet(ctx context.Context, key, field string) ([]byte, error)

HGet gets a hash field

func (*RedisCache) HGetAll

func (c *RedisCache) HGetAll(ctx context.Context, key string) (map[string]string, error)

HGetAll gets all fields in a hash

func (*RedisCache) HSet

func (c *RedisCache) HSet(ctx context.Context, key, field string, value []byte) error

HSet sets a hash field

func (*RedisCache) Incr

func (c *RedisCache) Incr(ctx context.Context, key string) (int64, error)

Incr increments a key

func (*RedisCache) Ping

func (c *RedisCache) Ping(ctx context.Context) error

Ping checks Redis connectivity

func (*RedisCache) Publish

func (c *RedisCache) Publish(ctx context.Context, channel string, message []byte) error

Publish publishes a message to a channel (for pub/sub)

func (*RedisCache) Set

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

Set stores a value in Redis with TTL

func (*RedisCache) SetNX

func (c *RedisCache) SetNX(ctx context.Context, key string, value []byte, ttl time.Duration) (bool, error)

SetNX sets a key only if it doesn't exist (for distributed locking)

func (*RedisCache) Stats

func (c *RedisCache) Stats(ctx context.Context) (*Stats, error)

Stats returns cache statistics

func (*RedisCache) Subscribe

func (c *RedisCache) Subscribe(ctx context.Context, channel string) *redis.PubSub

Subscribe subscribes to a channel

type RedisConfig

type RedisConfig struct {
	// Type: "redis" or "valkey" (same library, just for clarity)
	Type string

	// Connection URL (takes precedence over host/port)
	// Format: redis://user:password@host:port/db or valkey://...
	URL string

	// Individual connection settings
	Host     string
	Port     int
	Password string
	DB       int

	// Pool settings
	PoolSize int
	MinIdle  int
	Timeout  time.Duration

	// Key prefix
	Prefix string

	// Cluster mode
	Cluster      bool
	ClusterNodes []string
}

RedisConfig holds Redis/Valkey connection configuration

type Stats

type Stats struct {
	Hits       int64  `json:"hits"`
	Misses     int64  `json:"misses"`
	Keys       int64  `json:"keys"`
	MemoryUsed int64  `json:"memory_used"`
	Connected  bool   `json:"connected"`
	Backend    string `json:"backend"`
}

Stats represents cache statistics

Jump to

Keyboard shortcuts

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