[go-每日一库] go-redis客户端的实现

见代码:

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)

}

posted on 2022-06-02 17:10  进击的davis  阅读(91)  评论(0编辑  收藏  举报

导航