Documentation
¶
Overview ¶
Package cache defines cache stores and bounded memory and Redis implementations. Cached values are optimization data and must not be the sole source of durable application correctness.
Index ¶
- type BoundedConfig
- type BoundedMemoryStore
- func (s *BoundedMemoryStore) Delete(ctx context.Context, key string) error
- func (s *BoundedMemoryStore) Get(ctx context.Context, key string) (any, bool, error)
- func (s *BoundedMemoryStore) GetOrSet(ctx context.Context, key string, ttl time.Duration, ...) (any, error)
- func (s *BoundedMemoryStore) InvalidatePrefix(ctx context.Context, prefix string) error
- func (s *BoundedMemoryStore) Set(ctx context.Context, key string, val any, ttl time.Duration) error
- type Client
- type Codec
- type JSONCodec
- type MemoryStore
- func (s *MemoryStore) Delete(ctx context.Context, key string) error
- func (s *MemoryStore) Get(ctx context.Context, key string) (any, bool, error)
- func (s *MemoryStore) GetOrSet(ctx context.Context, key string, ttl time.Duration, ...) (any, error)
- func (s *MemoryStore) InvalidatePrefix(ctx context.Context, prefix string) error
- func (s *MemoryStore) Set(ctx context.Context, key string, val any, ttl time.Duration) error
- type MultiLevelStore
- func (m *MultiLevelStore) Delete(ctx context.Context, key string) error
- func (m *MultiLevelStore) Get(ctx context.Context, key string) (any, bool, error)
- func (m *MultiLevelStore) GetOrSet(ctx context.Context, key string, ttl time.Duration, ...) (any, error)
- func (m *MultiLevelStore) InvalidatePrefix(ctx context.Context, prefix string) error
- func (m *MultiLevelStore) Set(ctx context.Context, key string, val any, ttl time.Duration) error
- type RedisConfig
- type RedisStore
- func (s *RedisStore) Client() redis.UniversalClient
- func (s *RedisStore) Close() error
- func (s *RedisStore) Delete(ctx context.Context, key string) error
- func (s *RedisStore) Get(ctx context.Context, key string) (any, bool, error)
- func (s *RedisStore) GetOrSet(ctx context.Context, key string, ttl time.Duration, ...) (any, error)
- func (s *RedisStore) InvalidatePrefix(ctx context.Context, prefix string) error
- func (s *RedisStore) Set(ctx context.Context, key string, value any, ttl time.Duration) error
- type Store
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BoundedConfig ¶
type BoundedConfig struct {
MaxEntries int
}
BoundedConfig defines the maximum number of process-local cache entries.
type BoundedMemoryStore ¶
type BoundedMemoryStore struct {
// contains filtered or unexported fields
}
BoundedMemoryStore implements an in-memory Store with strict capacity limits to prevent OOM memory growth.
Example ¶
package main
import (
"context"
"fmt"
"time"
"github.com/saifsilver/goplusplus/cache"
)
func main() {
ctx := context.Background()
store := cache.NewBoundedMemoryStore(100)
_ = store.Set(ctx, "query:123", "result_data", 5*time.Minute)
val, _, _ := store.Get(ctx, "query:123")
fmt.Println(val)
}
Output: result_data
func NewBoundedMemoryStore ¶
func NewBoundedMemoryStore(maxEntries int) *BoundedMemoryStore
NewBoundedMemoryStore initializes a capacity-capped in-memory cache store.
func (*BoundedMemoryStore) Delete ¶
func (s *BoundedMemoryStore) Delete(ctx context.Context, key string) error
Delete removes key from the bounded memory store.
func (*BoundedMemoryStore) GetOrSet ¶
func (s *BoundedMemoryStore) GetOrSet(ctx context.Context, key string, ttl time.Duration, fetcher func() (any, error)) (any, error)
GetOrSet coalesces concurrent misses and caches one fetched value.
func (*BoundedMemoryStore) InvalidatePrefix ¶
func (s *BoundedMemoryStore) InvalidatePrefix(ctx context.Context, prefix string) error
InvalidatePrefix removes keys that begin with prefix.
type JSONCodec ¶
type JSONCodec struct{}
JSONCodec is the safe default Redis cache codec. Decoded objects use the standard encoding/json representations such as map[string]any.
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
MemoryStore implements an in-memory Store with single-flight stampede protection.
func NewMemoryStore ¶
func NewMemoryStore() *MemoryStore
NewMemoryStore initializes a local in-memory cache store.
func (*MemoryStore) Delete ¶
func (s *MemoryStore) Delete(ctx context.Context, key string) error
Delete removes a process-local cache entry.
func (*MemoryStore) GetOrSet ¶
func (s *MemoryStore) GetOrSet(ctx context.Context, key string, ttl time.Duration, fetcher func() (any, error)) (any, error)
GetOrSet coalesces concurrent process-local misses and caches the fetched value.
func (*MemoryStore) InvalidatePrefix ¶
func (s *MemoryStore) InvalidatePrefix(ctx context.Context, prefix string) error
InvalidatePrefix removes process-local keys that begin with prefix.
type MultiLevelStore ¶
type MultiLevelStore struct {
// contains filtered or unexported fields
}
MultiLevelStore provides L1 Memory + L2 Redis tiered caching.
func NewMultiLevelStore ¶
func NewMultiLevelStore(l1, l2 Store) *MultiLevelStore
NewMultiLevelStore creates a multi-level tiered cache store.
func (*MultiLevelStore) Delete ¶
func (m *MultiLevelStore) Delete(ctx context.Context, key string) error
Delete removes key from both cache levels and joins failures.
func (*MultiLevelStore) Get ¶
Get reads L1 before L2 and repopulates L1 with the remaining lifetime.
func (*MultiLevelStore) GetOrSet ¶
func (m *MultiLevelStore) GetOrSet(ctx context.Context, key string, ttl time.Duration, fetcher func() (any, error)) (any, error)
GetOrSet coalesces misses and populates both cache levels.
func (*MultiLevelStore) InvalidatePrefix ¶
func (m *MultiLevelStore) InvalidatePrefix(ctx context.Context, prefix string) error
InvalidatePrefix removes matching keys from both levels and joins failures.
type RedisConfig ¶
type RedisConfig struct {
URL string
Prefix string
Codec Codec
PingTimeout time.Duration
DialTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
ScanCount int64
LoadLockTTL time.Duration
LoadPoll time.Duration
}
RedisConfig controls connection validation, namespacing, and serialization.
type RedisStore ¶
type RedisStore struct {
// contains filtered or unexported fields
}
RedisStore implements Store using the official Redis Go client.
func NewRedisClient ¶
func NewRedisClient(ctx context.Context, redisURL string) (*RedisStore, error)
NewRedisClient is a compatibility convenience for URL-based construction.
func NewRedisStore ¶
func NewRedisStore(ctx context.Context, config RedisConfig) (*RedisStore, error)
NewRedisStore creates and verifies a Redis-backed cache. URL is required and may use redis:// or rediss:// syntax.
func NewRedisStoreFromClient ¶
func NewRedisStoreFromClient(ctx context.Context, client redis.UniversalClient, config RedisConfig) (*RedisStore, error)
NewRedisStoreFromClient verifies and wraps an application-owned Redis client. Closing the store does not close the shared client.
func (*RedisStore) Client ¶
func (s *RedisStore) Client() redis.UniversalClient
Client exposes the shared Redis client for other Redis-backed framework components such as distributed idempotency.
func (*RedisStore) Close ¶
func (s *RedisStore) Close() error
Close releases the Redis client only when the store owns it.
func (*RedisStore) Delete ¶
func (s *RedisStore) Delete(ctx context.Context, key string) error
Delete asynchronously unlinks a Redis cache key.
func (*RedisStore) GetOrSet ¶
func (s *RedisStore) GetOrSet(ctx context.Context, key string, ttl time.Duration, fetcher func() (any, error)) (any, error)
GetOrSet coordinates misses across local callers and Redis-backed instances.
func (*RedisStore) InvalidatePrefix ¶
func (s *RedisStore) InvalidatePrefix(ctx context.Context, prefix string) error
InvalidatePrefix scans and unlinks namespaced keys beginning with prefix.
type Store ¶
type Store interface {
Get(ctx context.Context, key string) (any, bool, error)
Set(ctx context.Context, key string, val any, ttl time.Duration) error
Delete(ctx context.Context, key string) error
GetOrSet(ctx context.Context, key string, ttl time.Duration, fetcher func() (any, error)) (any, error)
InvalidatePrefix(ctx context.Context, prefix string) error
}
Store defines the universal caching contract for all in-memory and distributed cache providers.