0

我正在将此库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)
}

}

错误截图: 在此处输入图像描述

提前致谢。

4

1 回答 1

1

尝试使用“github.com/eko/gocache/v2”(这是 V2 版本的包名称)。这为我解决了这个问题。

于 2022-01-10T21:20:54.513 回答