cache

package
v0.4.7 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// RedisKey to identify the configuration JSON key
	RedisKey = "redis"
)

Variables

View Source
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(config JSONConfigurationRedis) string

PrepareAddr to generate redis connection string

func RegisterMetrics added in v0.4.7

func RegisterMetrics(reg prometheus.Registerer)

RegisterMetrics registers all cache metrics with the provided registerer

Types

type Cache added in v0.4.7

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 Item added in v0.4.7

type Item[T any] struct {
	Value      T
	Expiration int64
}

Item represents a cached item with expiration

type JSONConfigurationRedis

type JSONConfigurationRedis struct {
	Host             string `json:"host"`
	Port             string `json:"port"`
	Password         string `json:"password"`
	ConnectionString string `json:"connectionstring"`
	DB               int    `json:"db"`
	ConnRetry        int    `json:"connRetry"`
}

JSONConfigurationRedis to hold all redis configuration values

func LoadConfiguration

func LoadConfiguration(file, key string) (JSONConfigurationRedis, error)

LoadConfiguration to load the redis configuration file and assign to variables

type MemoryCache added in v0.4.7

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.7

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.7

func (c *MemoryCache[T]) Clear(ctx context.Context)

Clear removes all items from the cache

func (*MemoryCache[T]) Delete added in v0.4.7

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.7

func (c *MemoryCache[T]) Get(ctx context.Context, key string) (T, bool)

Get retrieves an item from the cache

func (*MemoryCache[T]) ItemCount added in v0.4.7

func (c *MemoryCache[T]) ItemCount() int

ItemCount returns the number of items in the cache

func (*MemoryCache[T]) Set added in v0.4.7

func (c *MemoryCache[T]) Set(ctx context.Context, key string, value T, duration time.Duration)

Set adds an item to the cache with expiration

func (*MemoryCache[T]) Stop added in v0.4.7

func (c *MemoryCache[T]) Stop()

Stop stops the cleanup goroutine

type MemoryCacheOption added in v0.4.7

type MemoryCacheOption[T any] func(*MemoryCache[T])

MemoryCacheOption is a function that configures a MemoryCache

func WithCleanupInterval added in v0.4.7

func WithCleanupInterval[T any](interval time.Duration) MemoryCacheOption[T]

WithCleanupInterval sets the interval for cleaning expired items

func WithName added in v0.4.7

func WithName[T any](name string) MemoryCacheOption[T]

WithName sets the name for the cache instance

type RedisManager

type RedisManager struct {
	Config *JSONConfigurationRedis
	Client *redis.Client
}

RedisManager have access to cached data

func CreateRedisManager

func CreateRedisManager(config JSONConfigurationRedis) (*RedisManager, error)

CreateRedisManager to initialize the redis manager struct

func CreateRedisManagerFile

func CreateRedisManagerFile(file string) (*RedisManager, error)

CreateRedisManagerFile to initialize the redis manager struct from file

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

Jump to

Keyboard shortcuts

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