见代码:
package cache
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
"time"
)
var rdb *redis.Client
// initial conn
func initClient() (err error) {
rdb = redis.NewClient(&redis.Options{
Addr: "192.90.20.145:6379",
Password: "",
DB: 0,
PoolSize: 100,
})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err = rdb.Ping(ctx).Result()
return err
}
func V8Example() {
ctx := context.Background()
if err := initClient(); err != nil {
return
}
err := rdb.Set(ctx, "go-redis", "value", 30*time.Second)
if err != nil {
fmt.Println(err)
}
val, err1 := rdb.Get(ctx, "go-redis").Result()
if err1 != nil {
fmt.Println(err1)
}
fmt.Println("go-redis:", val)
}