skip to Main Content

I’m working with go-redis and trying to put a expiration on a Set I’m doing.

The examples in the documentation show –

func ExampleClient_Set() {
    // Last argument is expiration. Zero means the key has no
    // expiration time.
    err := rdb.Set("key", "value", 0).Err()
    if err != nil {
        panic(err)
    }

    // key2 will expire in an hour.
    err = rdb.Set("key2", "value", time.Hour).Err()
    if err != nil {
        panic(err)
    }
}

From what I can gather from the time documentation though (https://golang.org/pkg/time/), time.Hour should return “Hour returns the hour within the day specified by t, in the range [0, 23].”, which would make the expiration value different depending on what time of the day this code was run … I’m sure that’s not what they were going for, and as I’m sure the person who wrote the module knows Go better than me, I can’t imagine that’s what it actually does.

Being quite stumped, I read the go-redis code for Set (https://github.com/go-redis/redis/blob/0658532833205c238a41687728cb13c294504162/commands.go#L839) and it looked like it wanted seconds, unless you had something set, then it would want msecs, so I passed it 120 and it took a crap on first run saying “You’ve asked for 120ns, but the minimum is 1ms” … I added a pile of zeros and it expired almost as soon as I did my Set, so I finally gave up and just decided to ask for some help to do it properly instead of adding a bunch of zeros to every value I want and hoping it worked right.

I’ve got time.Minute in there now – based on the example I assume that’s giving me a minute, but I actually want two, and I have no idea after reading all this and trying all these things how to achieve that. Help would be GREATLY appreciated!!

3

Answers


  1. From what I can gather from the time documentation though (https://golang.org/pkg/time/), time.Hour should return “Hour returns the hour within the day specified by t, in the range [0, 23].”

    You’re looking at the documentation for time.Time.Hour, which you’d use like this:

    now := time.Now()
    fmt.Println(now.Hour())
    

    The documentation you’re looking for is of the constant time.Hour, which is a time.Duration defined as 60 * Minute – which happens to show you how to get the two minutes you’re looking for.

    2 * time.Minute
    
    Login or Signup to reply.
  2. You’re confusing time.Hour with Time.Hour().

    time.Hour is a constant, that represents a duration of one hour. That’s what the code in your example uses.

    Time.Hour() is is a method on a time.Time value, which returns the hour within the day specified, as you quoted from documentation.

    Login or Signup to reply.
  3. What the Set API needs is time.Duration type. If you need two minutes, use:

    err := rdb.Set("key", "value", 2*time.Minute).Err()
    

    You’re looking at the documentation for Time.Hour(), not time.Hour.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search