cache

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2024 License: GPL-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PrefixSession  = "session:"
	PrefixHealth   = "health:"
	PrefixVersion  = "version:"
	PrefixRate     = "rate:"
	DefaultTimeout = 5 * time.Second
	RetryAttempts  = 2
	RetryDelay     = 50 * time.Millisecond

	// 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 CacheType

type CacheType string

CacheType represents the type of cache to use

const (
	CacheTypeRedis  CacheType = "redis"
	CacheTypeMemory CacheType = "memory"
)

type Config added in v0.2.0

type Config struct {
	// Redis configuration
	RedisAddr string

	// Memory cache configuration
	DataDir string // Directory for persistent storage (derived from DB path)
}

Config holds cache configuration options

type LocalCache

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

LocalCache provides in-memory caching to reduce Redis hits

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(ctx context.Context, key string, value interface{}) 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(ctx context.Context, key string, value interface{}, expiration time.Duration) error

Set stores a value in cache

type RedisStore

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

RedisStore represents a Redis cache instance with local memory cache

func (*RedisStore) CleanAndCount

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

func (*RedisStore) Close

func (s *RedisStore) Close() error

Close closes the Redis connection and stops the cleanup goroutine

func (*RedisStore) Delete

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

Delete removes a value from both Redis and local cache

func (*RedisStore) Expire

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

func (*RedisStore) Get

func (s *RedisStore) Get(ctx context.Context, key string, value interface{}) error

Get retrieves a value from cache with local cache first

func (*RedisStore) GetCount

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

func (*RedisStore) Increment

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

Rate limiting methods

func (*RedisStore) Set

func (s *RedisStore) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error

Set stores a value in both Redis and local cache

type Store

type Store interface {
	Get(ctx context.Context, key string, value interface{}) error
	Set(ctx context.Context, key string, value interface{}, 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(cfg Config) (Store, error)

InitCache initializes a cache instance based on configuration. It always returns a valid cache store, falling back to memory cache if Redis fails.

func NewCache

func NewCache(addr string) (Store, error)

NewCache creates a new Redis cache instance with optimized configuration

func NewMemoryStore

func NewMemoryStore(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