kv 支持配置超时时间

This commit is contained in:
ExplodingDragon
2025-11-25 18:49:15 +08:00
parent ad14614842
commit 60e3c8e90c

View File

@@ -2,6 +2,7 @@ package goja
import ( import (
"os" "os"
"time"
"github.com/dop251/goja" "github.com/dop251/goja"
"github.com/pkg/errors" "github.com/pkg/errors"
@@ -37,14 +38,24 @@ func kvResult(db kv.CursorPagedKV) func(ctx core.FilterContext, jsCtx *goja.Runt
} }
return jsCtx.ToValue(get), nil return jsCtx.ToValue(get), nil
}, },
"set": func(key, value string) error { "set": func(key, value string, ttl ...int) error {
return db.Put(ctx, key, value, kv.TTLKeep) var t time.Duration
t = kv.TTLKeep
if len(ttl) > 0 && ttl[0] > 0 {
t = time.Duration(ttl[0]) * time.Millisecond
}
return db.Put(ctx, key, value, t)
}, },
"delete": func(key string) (bool, error) { "delete": func(key string) (bool, error) {
return db.Delete(ctx, key) return db.Delete(ctx, key)
}, },
"putIfNotExists": func(key, value string) (bool, error) { "putIfNotExists": func(key, value string, ttl ...int) (bool, error) {
return db.PutIfNotExists(ctx, key, value, kv.TTLKeep) var t time.Duration
t = kv.TTLKeep
if len(ttl) > 0 && ttl[0] > 0 {
t = time.Duration(ttl[0]) * time.Millisecond
}
return db.PutIfNotExists(ctx, key, value, t)
}, },
"compareAndSwap": func(key, oldValue, newValue string) (bool, error) { "compareAndSwap": func(key, oldValue, newValue string) (bool, error) {
return db.CompareAndSwap(ctx, key, oldValue, newValue) return db.CompareAndSwap(ctx, key, oldValue, newValue)