redis

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package redis provides a Redis-backed cache adapter implementing CachePort.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RedisCache

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

RedisCache implements port.CachePort using Redis as L2 cache.

RedisCache accepts redis.UniversalClient, which means it works transparently with:

  • redis.NewClient() — standalone Redis
  • redis.NewClusterClient() — Redis Cluster
  • redis.NewFailoverClient() — Redis Sentinel
  • redis.NewUniversalClient() — auto-detection based on config

func NewRedisCache

func NewRedisCache(addr string, defaultTTL time.Duration) (*RedisCache, error)

NewRedisCache creates a new Redis-backed L2 cache from a single Redis address. For Redis Cluster or Sentinel, use NewRedisCacheFromClient instead.

func NewRedisCacheFromClient

func NewRedisCacheFromClient(client redis.UniversalClient, defaultTTL time.Duration) (*RedisCache, error)

NewRedisCacheFromClient creates a new Redis-backed L2 cache from any redis.UniversalClient. This supports standalone Redis, Redis Cluster, and Redis Sentinel:

// Standalone
client := redis.NewClient(&redis.Options{Addr: "localhost:6379"})
cache, err := redis.NewRedisCacheFromClient(client, 5*time.Minute)

// Cluster
client := redis.NewClusterClient(&redis.ClusterOptions{
    Addrs: []string{":7000", ":7001", ":7002"},
})
cache, err := redis.NewRedisCacheFromClient(client, 5*time.Minute)

// Sentinel
client := redis.NewFailoverClient(&redis.FailoverOptions{
    MasterName:    "mymaster",
    SentinelAddrs: []string{":26379"},
})
cache, err := redis.NewRedisCacheFromClient(client, 5*time.Minute)

// Universal (auto-detect based on config)
client := redis.NewUniversalClient(&redis.UniversalOptions{
    Addrs: []string{":7000", ":7001", ":7002"},
})
cache, err := redis.NewRedisCacheFromClient(client, 5*time.Minute)

func (*RedisCache) Clear

func (rc *RedisCache) Clear(ctx context.Context) error

Clear removes all cache keys - implements port.CachePort

func (*RedisCache) Close

func (rc *RedisCache) Close() error

Close closes the Redis connection

func (*RedisCache) Delete

func (rc *RedisCache) Delete(ctx context.Context, key string) error

Delete removes a key from Redis cache - implements port.CachePort

func (*RedisCache) Get

func (rc *RedisCache) Get(ctx context.Context, key string) (any, error)

Get retrieves a value from Redis cache - implements port.CachePort

func (*RedisCache) Set

func (rc *RedisCache) Set(ctx context.Context, key string, value any, ttl time.Duration) error

Set stores a value in Redis cache - implements port.CachePort

func (*RedisCache) Stats

func (rc *RedisCache) Stats() *port.CacheStats

Stats returns cache statistics - implements port.CachePort

Jump to

Keyboard shortcuts

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