cache

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2026 License: GPL-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PrefixSession = "session:"
	PrefixHealth  = "health:"
	PrefixVersion = "version:"
	PrefixRate    = "rate:"

	// Cache durations
	DefaultTTL  = 15 * time.Minute
	HealthTTL   = 30 * time.Minute
	StatsTTL    = 5 * time.Minute
	SessionsTTL = 1 * time.Minute

	CleanupInterval = 1 * time.Minute // Increased to reduce cleanup frequency
)

Variables

View Source
var (
	ErrKeyNotFound = errors.New("cache: key not found")
	ErrClosed      = errors.New("cache: store is closed")
)

Functions

This section is empty.

Types

type Config added in v0.2.0

type Config struct {
	// Directory for persistent storage (derived from DB path).
	DataDir string
	// contains filtered or unexported fields
}

Config holds cache configuration options.

type LocalCache

type LocalCache struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

LocalCache provides in-memory caching for store implementations.

type MemoryStore

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

MemoryStore implements Store interface using in-memory storage

func (*MemoryStore) CleanAndCount

func (s *MemoryStore) CleanAndCount(ctx context.Context, key string, windowStart int64) error

CleanAndCount removes old timestamps and returns the count of remaining ones

func (*MemoryStore) Close

func (s *MemoryStore) Close() error

Close cleans up resources

func (*MemoryStore) Delete

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

Delete removes a value from cache

func (*MemoryStore) Expire

func (s *MemoryStore) Expire(ctx context.Context, key string, expiration time.Duration) error

Expire updates the expiration time for a key

func (*MemoryStore) Get

func (s *MemoryStore) Get(_ context.Context, key string, value any) error

Get retrieves a value from cache

func (*MemoryStore) GetCount

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

GetCount returns the number of timestamps in the current window

func (*MemoryStore) Increment

func (s *MemoryStore) Increment(ctx context.Context, key string, timestamp int64) error

Increment adds a timestamp to the rate limit window

func (*MemoryStore) Set

func (s *MemoryStore) Set(_ context.Context, key string, value any, expiration time.Duration) error

Set stores a value in cache

type Store

type Store interface {
	Get(ctx context.Context, key string, value any) error
	Set(ctx context.Context, key string, value any, expiration time.Duration) error
	Delete(ctx context.Context, key string) error
	Increment(ctx context.Context, key string, timestamp int64) error
	CleanAndCount(ctx context.Context, key string, windowStart int64) error
	GetCount(ctx context.Context, key string) (int64, error)
	Expire(ctx context.Context, key string, expiration time.Duration) error
	Close() error
}

Store defines the caching operations. Implementations must be safe for concurrent use.

func InitCache

func InitCache(ctx context.Context, cfg Config) (Store, error)

InitCache initializes the global in-memory cache instance.

func NewMemoryStore

func NewMemoryStore(ctx context.Context, dataDir string) Store

NewMemoryStore creates a new in-memory cache instance

Jump to

Keyboard shortcuts

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