Expire sets a timeout on key
(ctx *Context, txn *db.Transaction)
| 49 | |
| 50 | // Expire sets a timeout on key |
| 51 | func Expire(ctx *Context, txn *db.Transaction) (OnCommit, error) { |
| 52 | kv := txn.Kv() |
| 53 | key := []byte(ctx.Args[0]) |
| 54 | seconds, err := strconv.ParseInt(ctx.Args[1], 10, 64) |
| 55 | if err != nil { |
| 56 | return nil, ErrInteger |
| 57 | } |
| 58 | |
| 59 | at := time.Now().Add(time.Second * time.Duration(seconds)).UnixNano() |
| 60 | if err := kv.ExpireAt(key, at); err != nil { |
| 61 | if err == db.ErrKeyNotFound { |
| 62 | return Integer(ctx.Out, 0), nil |
| 63 | } |
| 64 | return nil, errors.New("ERR " + err.Error()) |
| 65 | } |
| 66 | return Integer(ctx.Out, 1), nil |
| 67 | } |
| 68 | |
| 69 | // ExpireAt sets an absolute timestamp to expire on key |
| 70 | func ExpireAt(ctx *Context, txn *db.Transaction) (OnCommit, error) { |