0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

go-redisでSET 0,-1

Last updated at Posted at 2019-11-09
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)
}
$ go run main.go
TTL(ts:10:1573291378):10s
TTL(ts:0:1573291378):-1s
TTL(ts:-1:1573291378):-1s
  • 10は10秒後に消える。
  • 0,-1はつまり一緒。

Refs.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?