SetEx sets the value and expiration of a key KEY_NAME TIMEOUT VALUE
(ctx *Context, txn *db.Transaction)
| 293 | |
| 294 | // SetEx sets the value and expiration of a key KEY_NAME TIMEOUT VALUE |
| 295 | func SetEx(ctx *Context, txn *db.Transaction) (OnCommit, error) { |
| 296 | //get the key |
| 297 | key := []byte(ctx.Args[0]) |
| 298 | obj, err := txn.Object(key) |
| 299 | if err != nil && err != db.ErrKeyNotFound { |
| 300 | return nil, errors.New("ERR " + err.Error()) |
| 301 | } |
| 302 | if err != db.ErrKeyNotFound { |
| 303 | txn.Destory(obj, key) |
| 304 | } |
| 305 | |
| 306 | s := db.NewString(txn, key) |
| 307 | ui, err := strconv.ParseInt(ctx.Args[1], 10, 64) |
| 308 | if err != nil { |
| 309 | return nil, ErrInteger |
| 310 | } |
| 311 | if ui <= 0 { |
| 312 | return nil, ErrExpireSetEx |
| 313 | } |
| 314 | unit := ui * int64(time.Second) |
| 315 | if err := s.Set([]byte(ctx.Args[2]), unit); err != nil { |
| 316 | return nil, errors.New("ERR " + err.Error()) |
| 317 | } |
| 318 | |
| 319 | return SimpleString(ctx.Out, OK), nil |
| 320 | } |
| 321 | |
| 322 | // PSetEx sets the value and expiration in milliseconds of a key |
| 323 | func PSetEx(ctx *Context, txn *db.Transaction) (OnCommit, error) { |