Documentation
¶
Index ¶
- Constants
- Variables
- type CacheType
- type Config
- type LocalCache
- type MemoryStore
- func (s *MemoryStore) CleanAndCount(ctx context.Context, key string, windowStart int64) error
- func (s *MemoryStore) Close() error
- func (s *MemoryStore) Delete(ctx context.Context, key string) error
- func (s *MemoryStore) Expire(ctx context.Context, key string, expiration time.Duration) error
- func (s *MemoryStore) Get(ctx context.Context, key string, value interface{}) error
- func (s *MemoryStore) GetCount(ctx context.Context, key string) (int64, error)
- func (s *MemoryStore) Increment(ctx context.Context, key string, timestamp int64) error
- func (s *MemoryStore) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error
- type RedisStore
- func (s *RedisStore) CleanAndCount(ctx context.Context, key string, windowStart int64) error
- func (s *RedisStore) Close() error
- func (s *RedisStore) Delete(ctx context.Context, key string) error
- func (s *RedisStore) Expire(ctx context.Context, key string, expiration time.Duration) error
- func (s *RedisStore) Get(ctx context.Context, key string, value interface{}) error
- func (s *RedisStore) GetCount(ctx context.Context, key string) (int64, error)
- func (s *RedisStore) Increment(ctx context.Context, key string, timestamp int64) error
- func (s *RedisStore) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error
- type Store
Constants ¶
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 ¶
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 { // Redis configuration RedisAddr string // Memory cache configuration DataDir string // Directory for persistent storage (derived from DB path) }
Config holds cache configuration options
type LocalCache ¶
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 ¶
CleanAndCount removes old timestamps and returns the count of remaining ones
func (*MemoryStore) Delete ¶
func (s *MemoryStore) Delete(ctx context.Context, key string) error
Delete removes a value from cache
func (*MemoryStore) Get ¶
func (s *MemoryStore) Get(ctx context.Context, key string, value interface{}) error
Get retrieves a value from 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 (*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) Get ¶
func (s *RedisStore) Get(ctx context.Context, key string, value interface{}) error
Get retrieves a value from cache with local cache first
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 ¶
InitCache initializes a cache instance based on configuration. It always returns a valid cache store, falling back to memory cache if Redis fails.
func NewMemoryStore ¶
NewMemoryStore creates a new in-memory cache instance