github.com/go-redis/redis

1, install,

go get -u github.com/go-redis/redis
Copy the code

2. Import and initialize

var ( Redis *redis.Client ) func InitRedis() (err error) { Redis = redis.NewClient(&redis.Options{ Addr: "127.0.0.1:6379", Password: "", // no Password set DB: 0, // use default DB}) _, err = redis.ping ().result () if err! = nil { return err } return nil }Copy the code

3, use,

The expire unit is nanoseconds, and if a value greater than 0 is passed in, it needs to be greater than 1000

// Save (key, value, expire) if err := dao.redis.set (key, val, 0).err (); err ! = nil{fmt.println (err)} // Value (key, value, expire) if err := dao.redis.get (key).err (); err ! = nil{ fmt.Println(err) }Copy the code