Documentation
¶
Index ¶
- Constants
- Variables
- func PrepareAddr(cfg config.YAMLConfigurationRedis) string
- func RegisterMetrics(reg prometheus.Registerer)
- type Cache
- type Item
- type MemoryCache
- func (c *MemoryCache[T]) Clear(ctx context.Context)
- func (c *MemoryCache[T]) Delete(ctx context.Context, key string)
- func (c *MemoryCache[T]) Get(ctx context.Context, key string) (T, bool)
- func (c *MemoryCache[T]) GetStale(ctx context.Context, key string) (T, bool)
- func (c *MemoryCache[T]) ItemCount() int
- func (c *MemoryCache[T]) Set(ctx context.Context, key string, value T, duration time.Duration)
- func (c *MemoryCache[T]) Stop()
- type MemoryCacheOption
- type RedisJSONCache
- func (c *RedisJSONCache[T]) Delete(ctx context.Context, key string) error
- func (c *RedisJSONCache[T]) Get(ctx context.Context, key string) (T, bool, error)
- func (c *RedisJSONCache[T]) GetStale(ctx context.Context, key string) (T, bool, error)
- func (c *RedisJSONCache[T]) Set(ctx context.Context, key string, value T, ttl time.Duration) error
- type RedisManager
Constants ¶
const (
// RedisKey to identify the configuration JSON key
RedisKey = "redis"
)
Variables ¶
var ( // CacheHits tracks the number of cache hits CacheHits = prometheus.NewCounterVec( prometheus.CounterOpts{ Name: cacheHitsName, Help: cacheHitsHelp, }, []string{"cache_name"}, ) // CacheMisses tracks the number of cache misses CacheMisses = prometheus.NewCounterVec( prometheus.CounterOpts{ Name: cacheMissesName, Help: cacheMissesHelp, }, []string{"cache_name"}, ) // CacheEvictions tracks the number of cache evictions CacheEvictions = prometheus.NewCounterVec( prometheus.CounterOpts{ Name: cacheEvictionsName, Help: cacheEvictionsHelp, }, []string{"cache_name"}, ) // CacheItems tracks the current number of items in the cache CacheItems = prometheus.NewGaugeVec( prometheus.GaugeOpts{ Name: cacheItemsName, Help: cacheItemsHelp, }, []string{"cache_name"}, ) )
Functions ¶
func PrepareAddr ¶
func PrepareAddr(cfg config.YAMLConfigurationRedis) string
PrepareAddr to generate redis connection string
func RegisterMetrics ¶ added in v0.4.5
func RegisterMetrics(reg prometheus.Registerer)
RegisterMetrics registers all cache metrics with the provided registerer
Types ¶
type Cache ¶ added in v0.4.5
type Cache[T any] interface { // Get retrieves an item from the cache by key Get(ctx context.Context, key string) (T, bool) // Set adds or updates an item in the cache with expiration Set(ctx context.Context, key string, value T, duration time.Duration) // Delete removes an item from the cache Delete(ctx context.Context, key string) // Clear removes all items from the cache Clear(ctx context.Context) // ItemCount returns the number of items in the cache ItemCount() int // Stop stops the cleanup goroutine Stop() }
Cache interface defines methods that any cache implementation must provide
type MemoryCache ¶ added in v0.4.5
type MemoryCache[T any] struct { // contains filtered or unexported fields }
MemoryCache provides an in-memory implementation of the Cache interface
func NewMemoryCache ¶ added in v0.4.5
func NewMemoryCache[T any](opts ...MemoryCacheOption[T]) *MemoryCache[T]
NewMemoryCache creates a new in-memory cache with the provided options
func (*MemoryCache[T]) Clear ¶ added in v0.4.5
func (c *MemoryCache[T]) Clear(ctx context.Context)
Clear removes all items from the cache
func (*MemoryCache[T]) Delete ¶ added in v0.4.5
func (c *MemoryCache[T]) Delete(ctx context.Context, key string)
Delete removes an item from the cache
func (*MemoryCache[T]) Get ¶ added in v0.4.5
func (c *MemoryCache[T]) Get(ctx context.Context, key string) (T, bool)
Get retrieves an item from the cache
func (*MemoryCache[T]) GetStale ¶ added in v0.5.5
func (c *MemoryCache[T]) GetStale(ctx context.Context, key string) (T, bool)
GetStale returns a cached item even if its TTL has expired but the cleanup goroutine has not yet evicted it. Callers use this as a fallback when the primary data source is unavailable (e.g. DB outage). Returns (zero, false) when the key was never written or has already been evicted by the cleanup goroutine.
func (*MemoryCache[T]) ItemCount ¶ added in v0.4.5
func (c *MemoryCache[T]) ItemCount() int
ItemCount returns the number of items in the cache
func (*MemoryCache[T]) Stop ¶ added in v0.4.5
func (c *MemoryCache[T]) Stop()
Stop stops the cleanup goroutine
type MemoryCacheOption ¶ added in v0.4.5
type MemoryCacheOption[T any] func(*MemoryCache[T])
MemoryCacheOption is a function that configures a MemoryCache
func WithCleanupInterval ¶ added in v0.4.5
func WithCleanupInterval[T any](interval time.Duration) MemoryCacheOption[T]
WithCleanupInterval sets the interval for cleaning expired items
func WithName ¶ added in v0.4.5
func WithName[T any](name string) MemoryCacheOption[T]
WithName sets the name for the cache instance
type RedisJSONCache ¶ added in v0.5.5
type RedisJSONCache[T any] struct { // contains filtered or unexported fields }
RedisJSONCache stores typed JSON values in Redis under a fixed key prefix.
func NewRedisJSONCache ¶ added in v0.5.5
func NewRedisJSONCache[T any](client *redis.Client, prefix string) *RedisJSONCache[T]
NewRedisJSONCache creates a typed Redis-backed JSON cache.
func (*RedisJSONCache[T]) Delete ¶ added in v0.5.5
func (c *RedisJSONCache[T]) Delete(ctx context.Context, key string) error
Delete removes a cached value.
func (*RedisJSONCache[T]) Get ¶ added in v0.5.5
Get retrieves and decodes a value. ok=false means Redis had no value.
func (*RedisJSONCache[T]) GetStale ¶ added in v0.5.5
GetStale retrieves the cached value ignoring whether it has technically expired. Redis only returns a value if the key still exists; expired keys are deleted by Redis on read or by the background eviction, so "stale" here means "still present in Redis past the TTL we set". Callers use this as a last-resort fallback when the primary data source (e.g. the DB) is unavailable.
Returns (value, true, nil) when the key exists regardless of its remaining TTL, and (zero, false, nil) when Redis does not have the key. Redis errors are returned as-is.
type RedisManager ¶
type RedisManager struct {
Config *config.YAMLConfigurationRedis
Client *redis.Client
}
RedisManager have access to cached data
func CreateRedisManager ¶
func CreateRedisManager(cfg config.YAMLConfigurationRedis) (*RedisManager, error)
CreateRedisManager to initialize the redis manager struct
func (*RedisManager) Check ¶
func (rm *RedisManager) Check() error
Check to verify if connection is open and ready
func (*RedisManager) GetRedis ¶
func (rm *RedisManager) GetRedis() *redis.Client
GetRedis to get redis client ready