v1.0.0

API Reference

Last updated


This page lists the constructor and RedisFallback methods of go-redis-fallback with their signatures, return values, and errors.

Constructor


New


  1. func New(c Config) (*RedisFallback, error)

ItemDetail
BehaviorApplies defaults, initializes the logger and Redis client, and pings Redis: on success it runs the Recovery Resync synchronously, on failure it enters fallback and starts the health check; finally it starts the batch writer and the 30-second expiry sweep
ErrorsOnly a logger initialization failure; the message starts with Failed to initialize and wraps the cause with %w
Redis unreachableNot an error; the instance is returned in fallback mode
PanicsWhen c.Redis is nil: inside New if the ping succeeds, otherwise on the first local file access (details)

Methods


Get


  1. func (rf *RedisFallback) Get(key string) (interface{}, error)

ItemDetail
BehaviorNormal mode: see Normal-Mode Read; fallback mode: see Fallback-Mode Read
Return valueThe original value on a memory hit; the JSON-decoded result when restored from a local file; nil with a nil error when another instance stored a map or struct in Redis (details)
Error Not foundThe key is missing or expired
Error Failed to parseThe local file is not valid JSON
Side effectsA normal-mode memory hit writes back to Redis in the background; a failed Redis read switches to fallback

Set


  1. func (rf *RedisFallback) Set(key string, value interface{}, ttl time.Duration) error

ItemDetail
BehaviorSee Write Path
ttlStored as whole seconds; < 1s means no expiry
Errorsvalue fails to serialize; the fallback queue is full and the synchronous file write fails
PanicsWhen value is nil (reflect.TypeOf(nil).String())
Side effectsA failed Redis write switches to fallback

Del


  1. func (rf *RedisFallback) Del(key string) error

ItemDetail
BehaviorDeletes from memory and the local file; normal mode also deletes the Redis key
ErrorsRedis DEL fails in normal mode: Failed to delete
Missing keyNo error

Close


  1. func (rf *RedisFallback) Close()

StopsDoes not stop
Health-check ticker, batch-write ticker, Redis clientBatch-writer goroutine, 30-second expiry sweep ticker

Close does not flush queued writes; fallback writes from the last TimeToWrite before the call never reach disk.

Types


Config, Redis, Options, Cache, and the other types are in the Type Reference.
中文