majianyu
6/18/2018 - 2:19 PM

redis_go

package main

import (
    "github.com/go-redis/redis"
    "fmt"
    "time"
)
var client *redis.Client
func init() {
    option := &redis.Options{
        Addr:     "localhost:6379",
        Password: "",
        DB:       0,
    }
    client = redis.NewClient(option)
}
func main() {
    foo()
    client.Close()
}

func foo() {
    day,err := time.ParseDuration("24h")
    fmt.Println(day)
    err = client.Set("key","value",day).Err()
    if err != nil {
        panic(err)
    }
    val,err := client.Get("key").Result()
    if err != nil {
        fmt.Printf("%T,%+v\n",err,err)
    }
    fmt.Println(val)

    val2 ,err := client.Get("key2").Result()
    if err == redis.Nil {
        fmt.Println("key2 does not exist")
    } else if err != nil {
        panic(err)
    } else {
        fmt.Println("key2",val2)
    }
}
func bar() {

}