Mode:
Duration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
var ctx = context.Background()
func main() {
cluster := redis.NewClusterClient(&redis.ClusterOptions{
Addrs: []string{
"localhost:7000",
"localhost:7001",
"localhost:7002",
},
})
userID := "42"
cacheKey := "user:" + userID
cluster.Set(ctx, cacheKey, "Alice", 0)
name, _ := cluster.Get(ctx, cacheKey).Result()
fmt.Println("Cache Hit:", name)
fmt.Println("Updating user record...")
cluster.Del(ctx, cacheKey)
fmt.Println("Cache Invalidated:", cacheKey)
_, err := cluster.Get(ctx, cacheKey).Result()
if err == redis.Nil {
fmt.Println("Cache Miss")
}
}Coding works best on desktop or with an external keyboard.