cache

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrKeyNotFound = errors.New("key not found")
	ErrInvalidTTL  = errors.New("invalid TTL")
)

Functions

This section is empty.

Types

type Cache

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

Cache provides Redis caching functionality.

func New

func New(cfg Config) *Cache

New creates a new Redis cache instance.

func NewFromClient

func NewFromClient(client *redis.Client) *Cache

NewFromClient creates a cache from an existing Redis client.

func (*Cache) Client

func (c *Cache) Client() *redis.Client

Client returns the underlying Redis client.

func (*Cache) Close

func (c *Cache) Close() error

Close closes the Redis connection.

func (*Cache) Decr

func (c *Cache) Decr(ctx context.Context, key string) (int64, error)

Decr decrements a counter.

func (*Cache) Delete

func (c *Cache) Delete(ctx context.Context, key string) error

Delete removes a key.

func (*Cache) DeletePattern

func (c *Cache) DeletePattern(ctx context.Context, pattern string) (int64, error)

DeletePattern deletes all keys matching a pattern.

func (*Cache) Exists

func (c *Cache) Exists(ctx context.Context, key string) (bool, error)

Exists checks if a key exists.

func (*Cache) Expire

func (c *Cache) Expire(ctx context.Context, key string, ttl time.Duration) error

Expire sets expiration on a key.

func (*Cache) Get

func (c *Cache) Get(ctx context.Context, key string) (string, error)

Get retrieves a value by key.

func (*Cache) GetJSON

func (c *Cache) GetJSON(ctx context.Context, key string, dest interface{}) error

GetJSON retrieves and unmarshals JSON.

func (*Cache) HealthCheck

func (c *Cache) HealthCheck(ctx context.Context) error

HealthCheck returns cache health status.

func (*Cache) Incr

func (c *Cache) Incr(ctx context.Context, key string) (int64, error)

Incr increments a counter.

func (*Cache) Ping

func (c *Cache) Ping(ctx context.Context) error

Ping checks Redis connectivity.

func (*Cache) Set

func (c *Cache) Set(ctx context.Context, key string, value string, ttl time.Duration) error

Set stores a value with TTL.

func (*Cache) SetJSON

func (c *Cache) SetJSON(ctx context.Context, key string, value interface{}, ttl time.Duration) error

SetJSON marshals and stores JSON.

func (*Cache) SetNX

func (c *Cache) SetNX(ctx context.Context, key string, value string, ttl time.Duration) (bool, error)

SetNX stores a value only if key doesn't exist.

func (*Cache) SetPrefix

func (c *Cache) SetPrefix(prefix string)

SetPrefix sets the key prefix.

func (*Cache) SortedSetAdd

func (c *Cache) SortedSetAdd(ctx context.Context, key string, member string, score float64) error

SortedSetAdd adds to a sorted set.

func (*Cache) SortedSetRange

func (c *Cache) SortedSetRange(ctx context.Context, key string, start, stop int64) ([]string, error)

SortedSetRange retrieves sorted set members by rank.

func (*Cache) SortedSetRemove

func (c *Cache) SortedSetRemove(ctx context.Context, key string, members ...interface{}) error

SortedSetRemove removes from a sorted set.

func (*Cache) TTL

func (c *Cache) TTL(ctx context.Context, key string) (time.Duration, error)

TTL returns remaining TTL for a key.

type CacheWarmer

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

CacheWarmer preloads frequently accessed data into cache.

func NewCacheWarmer

func NewCacheWarmer(cache *Cache, fetcher func(ctx context.Context, key string) (string, time.Duration, error)) *CacheWarmer

NewCacheWarmer creates a new cache warmer.

func (*CacheWarmer) WarmByPattern

func (w *CacheWarmer) WarmByPattern(ctx context.Context, pattern string, fetchKeys func(ctx context.Context) ([]string, error)) error

WarmByPattern loads keys matching a pattern.

func (*CacheWarmer) WarmKey

func (w *CacheWarmer) WarmKey(ctx context.Context, key string) error

WarmKey loads a single key into cache.

func (*CacheWarmer) WarmKeys

func (w *CacheWarmer) WarmKeys(ctx context.Context, keys []string) error

WarmKeys loads multiple keys into cache.

type Config

type Config struct {
	Addr         string        `default:"localhost:6379"`
	Password     string        `default:""`
	DB           int           `default:"0"`
	PoolSize     int           `default:"10"`
	MinIdleConns int           `default:"5"`
	DialTimeout  time.Duration `default:"5s"`
	ReadTimeout  time.Duration `default:"3s"`
	WriteTimeout time.Duration `default:"3s"`
}

Config holds Redis cache configuration.

type InvalidateAll

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

InvalidateAll removes all matching keys.

func NewInvalidateAll

func NewInvalidateAll(prefix string) *InvalidateAll

NewInvalidateAll creates a strategy that deletes keys matching a prefix.

func (*InvalidateAll) Invalidate

func (i *InvalidateAll) Invalidate(ctx context.Context, cache *Cache, keys ...string) error

type InvalidateByTags

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

InvalidateByTags invalidates keys by tags.

func NewInvalidateByTags

func NewInvalidateByTags() *InvalidateByTags

NewInvalidateByTags creates a tag-based invalidation strategy.

func (*InvalidateByTags) Invalidate

func (i *InvalidateByTags) Invalidate(ctx context.Context, cache *Cache, tags ...string) error

type InvalidationStrategy

type InvalidationStrategy interface {
	Invalidate(ctx context.Context, cache *Cache, keys ...string) error
}

InvalidationStrategy defines cache invalidation behavior.

type TagBasedCache

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

TagBasedCache adds tags to cached items for targeted invalidation.

func NewTagBasedCache

func NewTagBasedCache(cache *Cache) *TagBasedCache

NewTagBasedCache creates a cache with tag support.

func (*TagBasedCache) GetTags

func (t *TagBasedCache) GetTags(ctx context.Context, key string) ([]string, error)

GetTags retrieves all tags for a key.

func (*TagBasedCache) InvalidateByTag

func (t *TagBasedCache) InvalidateByTag(ctx context.Context, tag string) error

InvalidateByTag removes all cached items with a specific tag.

func (*TagBasedCache) SetWithTags

func (t *TagBasedCache) SetWithTags(ctx context.Context, key string, value string, ttl time.Duration, tags ...string) error

SetWithTags stores a value with associated tags.

Directories

Path Synopsis
Package adapter provides hexagonal architecture adapters for the cache package.
Package adapter provides hexagonal architecture adapters for the cache package.
Package service provides application services for cache domain.
Package service provides application services for cache domain.

Jump to

Keyboard shortcuts

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