Sobes.tech
Senior

Why did you use Redis instead of in-memory cache?

sobes.tech AI

Answer from AI

Redis was used instead of in-memory cache for several reasons:

  • Distribution: Redis allows caching data in a distributed environment accessible to multiple services and servers, whereas in-memory cache is limited to a single application instance.
  • Resilience: Redis can persist data to disk and support replication, reducing the risk of data loss during restart or failure.
  • Additional data structures: Redis supports various structures (lists, sets, hashes), which expand caching capabilities.

Example of using Redis in Go:

import (
    "github.com/go-redis/redis/v8"
    "context"
)

ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
    Addr: "localhost:6379",
})

// Setting a value
err := rdb.Set(ctx, "key", "value", 0).Err()
if err != nil {
    panic(err)
}

// Getting a value
val, err := rdb.Get(ctx, "key").Result()
if err != nil {
    panic(err)
}
fmt.Println("key", val)
Why did you use Redis instead of in-memory cache… - sobes.tech