v1.0.0

Getting Started

Last updated


This page covers installing go-redis-fallback, creating the first instance, and completing one read and write.

Prerequisites


ItemRequirement
Go1.24.3 or higher (per go.mod)
RedisAny reachable Redis server; it may go offline at runtime
FilesystemWrite access to Options.DBPath (default ./files/redisFallback/db) and Log.Path (default ./logs/redisFallback)

Installation


  1. go get github.com/pardnchiu/go-redis-fallback

The package name is redisFallback; the import path is github.com/pardnchiu/go-redis-fallback.

First Run


  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "path/filepath"
  7. "strconv"
  8. "time"
  9. redisFallback "github.com/pardnchiu/go-redis-fallback"
  10. )
  11. func main() {
  12. dbPath := "./files/redisFallback/db"
  13. db := 0
  14. // On a first start {DBPath}/{DB} must exist, otherwise the instance never enters normal mode
  15. if err := os.MkdirAll(filepath.Join(dbPath, strconv.Itoa(db)), 0755); err != nil {
  16. log.Fatal(err)
  17. }
  18. rf, err := redisFallback.New(redisFallback.Config{
  19. Redis: &redisFallback.Redis{Host: "localhost", Port: 6379, DB: db},
  20. Option: &redisFallback.Options{DBPath: dbPath},
  21. })
  22. if err != nil {
  23. log.Fatal(err)
  24. }
  25. defer rf.Close()
  26. if err := rf.Set("greeting", "hello", 10*time.Minute); err != nil {
  27. log.Fatal(err)
  28. }
  29. value, err := rf.Get("greeting")
  30. if err != nil {
  31. log.Fatal(err)
  32. }
  33. fmt.Println(value) // hello
  34. }

Checking the Current Mode


The library exposes no API to query the mode; mode switches are recorded in the log:
Log line ({Log.Path}/output.log)Meaning
Starting normal modeStartup ping succeeded; the recovery flow starts
[getFromRedis] Switching to fallback modeRead retries exhausted; switched to fallback mode
[setToRedis] Switching to fallback modeWrite retries exhausted; switched to fallback mode

A failed startup ping is recorded in error.log: Failed to connect, Starting fallback mode.

Next Steps


中文