package main
import (
"fmt"
"time"
"strconv"
"github.com/go-redis/redis"
)
func getRedisClient() (*redis.Client) {
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
})
return client
}
func setKey(ttl int) {
rc := getRedisClient()
ts := strconv.Itoa(int(time.Now().Unix()))
sttl := strconv.Itoa(ttl)
key := "ts:"+sttl+":"+ts
rc.Set(key, sttl, time.Duration(ttl)*time.Second)
v := rc.TTL(key).Val()
fmt.Printf("TTL(%s):%v\n",key,v)
}
func main() {
setKey(10)
setKey(0)
setKey(-1)
}