cache

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	Get(ctx context.Context, key string) (interface{}, error)
	Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
	Delete(ctx context.Context, key string) error
	DeletePattern(ctx context.Context, pattern string) error
	Exists(ctx context.Context, key string) (bool, error)
	Close() error
}

Cache interface defines caching operations.

Two implementations are provided:

  • MemoryCache: Simple in-process LRU cache with global TTL
  • RedisCache: Distributed cache with per-item TTL support

Use MemoryCache for development and single-instance deployments. Use RedisCache for horizontal scaling or when per-item TTL is needed.

type MemoryCache

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

MemoryCache implements an in-memory LRU cache. TTL is global, set at construction time via NewMemoryCache. For per-item TTL, use RedisCache instead.

func NewMemoryCache

func NewMemoryCache(size int, ttl time.Duration) *MemoryCache

NewMemoryCache creates a new in-memory cache

func (*MemoryCache) Close

func (m *MemoryCache) Close() error

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

func (*MemoryCache) Delete

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

Delete removes a key from the cache

func (*MemoryCache) DeletePattern

func (m *MemoryCache) DeletePattern(ctx context.Context, pattern string) error

DeletePattern removes all keys matching a pattern

func (*MemoryCache) Exists

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

Exists checks if a key exists in the cache

func (*MemoryCache) Get

func (m *MemoryCache) Get(ctx context.Context, key string) (interface{}, error)

Get retrieves a value from the cache

func (*MemoryCache) Set

func (m *MemoryCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error

Set stores a value in the cache. Note: ttl parameter is ignored; global TTL from NewMemoryCache is used. For per-item TTL, use RedisCache.

type RedisCache

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

RedisCache implements a Redis-backed cache

func NewRedisCache

func NewRedisCache(host string, port int, ttl time.Duration) (*RedisCache, error)

NewRedisCache creates a new Redis cache

func (*RedisCache) Close

func (r *RedisCache) Close() error

Close closes the Redis connection

func (*RedisCache) Delete

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

Delete removes a key from Redis

func (*RedisCache) DeletePattern

func (r *RedisCache) DeletePattern(ctx context.Context, pattern string) error

DeletePattern removes all keys matching a pattern

func (*RedisCache) Exists

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

Exists checks if a key exists in Redis

func (*RedisCache) Get

func (r *RedisCache) Get(ctx context.Context, key string) (interface{}, error)

Get retrieves a value from Redis

func (*RedisCache) Set

func (r *RedisCache) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error

Set stores a value in Redis

Jump to

Keyboard shortcuts

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