Documentation
¶
Overview ¶
Package redis provides a Redis-backed relay.CacheStore for the relay HTTP client. Cache entries are serialized as JSON blobs and stored with a TTL derived from each entry's ExpiresAt field. Keys are namespaced with a caller-supplied prefix so multiple clients can share one Redis instance without collisions.
Usage:
import (
"github.com/redis/go-redis/v9"
"github.com/jhonsferg/relay"
relayredis "github.com/jhonsferg/relay/ext/redis"
)
rdb := redis.NewClient(&redis.Options{Addr: "localhost:6379"})
store := relayredis.NewCacheStore(rdb, "myapp:http-cache:")
client := relay.New(
relay.WithBaseURL("https://api.example.com"),
relay.WithCache(store),
)
The rdb argument may be any type that satisfies redis.Cmdable - a *redis.Client, *redis.ClusterClient, *redis.Ring, etc.
TTL handling ¶
If a cached entry has a non-zero ExpiresAt, the Redis key is stored with the corresponding TTL so Redis itself evicts it. Entries with a zero ExpiresAt are stored without a TTL (persistent until evicted by Redis memory policy or an explicit CacheStore.Delete / CacheStore.Clear).
Cluster note ¶
CacheStore.Clear uses SCAN + DEL against a single connection. When rdb is a *redis.ClusterClient, SCAN only iterates keys on a single shard. Use per-shard ForEachMaster iteration in that case.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CacheStore ¶
type CacheStore struct {
// contains filtered or unexported fields
}
CacheStore is a relay.CacheStore backed by Redis. All methods are safe for concurrent use.
func NewCacheStore ¶
func NewCacheStore(rdb redisclient.Cmdable, prefix string) *CacheStore
NewCacheStore returns a Redis-backed relay.CacheStore.
- rdb: any redis.Cmdable (*redis.Client, *redis.ClusterClient, …).
- prefix: string prepended to every cache key (e.g. "myapp:http-cache:"). An empty prefix is allowed but not recommended on shared Redis instances.
func (*CacheStore) Clear ¶
func (s *CacheStore) Clear()
Clear removes all keys matching the store prefix using an iterative SCAN + DEL loop to avoid blocking the Redis server with a FLUSHDB.
Safe for shared Redis instances: only keys with the configured prefix are removed. See the package-level documentation for cluster caveats.
func (*CacheStore) Delete ¶
func (s *CacheStore) Delete(key string)
Delete removes the entry for key. It is a no-op if the key is absent.
func (*CacheStore) Get ¶
func (s *CacheStore) Get(key string) (*relay.CachedResponse, bool)
Get returns the cached entry for key if present and not expired. Returns nil, false on a Redis miss, a deserialization error, or an expired entry (the expired key is deleted proactively).
func (*CacheStore) Set ¶
func (s *CacheStore) Set(key string, entry *relay.CachedResponse)
Set stores or replaces the entry for key. If entry.ExpiresAt is non-zero, the Redis key is given the corresponding TTL. Already-expired entries are silently dropped.