MCPcopy Create free account
hub / github.com/distributedio/titan / SetEx

Function SetEx

command/strings.go:295–320  ·  view source on GitHub ↗

SetEx sets the value and expiration of a key KEY_NAME TIMEOUT VALUE

(ctx *Context, txn *db.Transaction)

Source from the content-addressed store, hash-verified

293
294// SetEx sets the value and expiration of a key KEY_NAME TIMEOUT VALUE
295func 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
323func PSetEx(ctx *Context, txn *db.Transaction) (OnCommit, error) {

Callers

nothing calls this directly

Calls 6

SetMethod · 0.95
NewStringFunction · 0.92
SimpleStringFunction · 0.85
ObjectMethod · 0.80
ErrorMethod · 0.65
DestoryMethod · 0.65

Tested by

no test coverage detected