安装redis, 运行如下go代码:
- package main
-
- import (
- "time"
- "fmt"
-
- "github.com/go-redis/redis"
- )
-
- var Client *redis.Client
-
- func init() {
- Client = redis.NewClient(&redis.Options{
- Addr: "127.0.0.1:6379",
- PoolSize: 1000,
- ReadTimeout: time.Millisecond * time.Duration(100),
- WriteTimeout: time.Millisecond * time.Duration(100),
- IdleTimeout: time.Second * time.Duration(60),
- })
-
- _, err := Client.Ping().Result()
- if err != nil {
- panic("init redis error")
- } else {
- fmt.Println("init redis ok")
- }
- }
-
- func get(key string) (string, bool) {
- r, err := Client.Get(key).Result()
- if err != nil {
- return "", false
- }
-
- return r, true
- }
-
- func set(key string, val string, expTime int32) {
- Client.Set(key, val, time.Duration(expTime) * time.Second)
- }
-
-
- func main() {
- set("name", "x", 100)
- s, b := get("name")
- fmt.Println(s, b)
- }
结果:
init redis ok
x true
过期时间是100s, 过期后,get无法获取信息, 返回了nil
简单, 不多说。