我正在将此库https://github.com/eko/gocache用于带有 go lang 的 redis
我的代码是
package main
import (
"context"
"fmt"
"time"
"github.com/eko/gocache/cache"
"github.com/eko/gocache/store"
"github.com/go-redis/redis/v8"
)
func main() {
ctx := context.Background()`
redisStore := store.NewRedis(redis.NewClient(&redis.Options{
Addr: "localhost:6379",
}), nil)
fmt.Println("redisStore", redisStore)
cacheManager := cache.New(redisStore)
err := cacheManager.Set("my-key", "my-value", &store.Options{Expiration: 15 * time.Second})
if err != nil {
panic(err)
}
value, err := cacheManager.Get(ctx, "my-key")
switch err {
case nil:
fmt.Printf("Get the key '%s' from the redis cache. Result: %s", "my-key", value)
case redis.Nil:
fmt.Printf("Failed to find the key '%s' from the redis cache.", "my-key")
default:
fmt.Printf("Failed to get the value from the redis cache with key '%s': %v", "my-key", err)
}
}
提前致谢。