diff --git a/docs/commands/strings.md b/docs/commands/strings.md index 2057b7f..f28fcd5 100644 --- a/docs/commands/strings.md +++ b/docs/commands/strings.md @@ -14,9 +14,9 @@ INCRBY DB.Str().Incr Increments the integer value of a key by a n INCRBYFLOAT DB.Str().IncrFloat Increments the float value of a key by a number. MGET DB.Str().GetMany Returns the values of one or more keys. MSET DB.Str().SetMany Sets the values of one or more keys. -PSETEX DB.Str().SetExpires Sets the value and expiration time (in ms) of a key. +PSETEX DB.Str().SetExpire Sets the value and expiration time (in ms) of a key. SET DB.Str().Set Sets the value of a key. -SETEX DB.Str().SetExpires Sets the value and expiration (in sec) time of a key. +SETEX DB.Str().SetExpire Sets the value and expiration (in sec) time of a key. SETNX DB.Str().SetWith Sets the value of a key when the key doesn't exist. STRLEN DB.Str().Get Returns the length of a value in bytes. ``` diff --git a/internal/command/key/expire_test.go b/internal/command/key/expire_test.go index e5ddb2a..9d48077 100644 --- a/internal/command/key/expire_test.go +++ b/internal/command/key/expire_test.go @@ -87,7 +87,7 @@ func TestExpireExec(t *testing.T) { t.Run("update expire", func(t *testing.T) { red := getRedka(t) - _ = red.Str().SetExpires("name", "alice", 60*time.Second) + _ = red.Str().SetExpire("name", "alice", 60*time.Second) cmd := redis.MustParse(parse, "expire name 30") conn := redis.NewFakeConn() diff --git a/internal/command/key/persist_test.go b/internal/command/key/persist_test.go index 482a608..2c0c561 100644 --- a/internal/command/key/persist_test.go +++ b/internal/command/key/persist_test.go @@ -62,7 +62,7 @@ func TestPersistExec(t *testing.T) { t.Run("volatile to persist", func(t *testing.T) { red := getRedka(t) - _ = red.Str().SetExpires("name", "alice", 60*time.Second) + _ = red.Str().SetExpire("name", "alice", 60*time.Second) cmd := redis.MustParse(ParsePersist, "persist name") conn := redis.NewFakeConn() diff --git a/internal/command/key/pexpire_test.go b/internal/command/key/pexpire_test.go index 7a3fe06..6c5acdf 100644 --- a/internal/command/key/pexpire_test.go +++ b/internal/command/key/pexpire_test.go @@ -88,7 +88,7 @@ func TestPExpireExec(t *testing.T) { t.Run("update pexpire", func(t *testing.T) { red := getRedka(t) - _ = red.Str().SetExpires("name", "alice", 60*time.Second) + _ = red.Str().SetExpire("name", "alice", 60*time.Second) cmd := redis.MustParse(parse, "pexpire name 30000") conn := redis.NewFakeConn() diff --git a/internal/command/key/ttl_test.go b/internal/command/key/ttl_test.go index a5412c9..aa45548 100644 --- a/internal/command/key/ttl_test.go +++ b/internal/command/key/ttl_test.go @@ -47,7 +47,7 @@ func TestTTLParse(t *testing.T) { func TestTTLExec(t *testing.T) { t.Run("has ttl", func(t *testing.T) { red := getRedka(t) - _ = red.Str().SetExpires("name", "alice", 60*time.Second) + _ = red.Str().SetExpire("name", "alice", 60*time.Second) cmd := redis.MustParse(ParseTTL, "ttl name") conn := redis.NewFakeConn() diff --git a/internal/command/string/psetex_test.go b/internal/command/string/psetex_test.go index 71889a5..556135a 100644 --- a/internal/command/string/psetex_test.go +++ b/internal/command/string/psetex_test.go @@ -104,7 +104,7 @@ func TestPSetEXExec(t *testing.T) { t.Run("change ttl", func(t *testing.T) { red := getRedka(t) - _ = red.Str().SetExpires("name", "alice", 60*time.Second) + _ = red.Str().SetExpire("name", "alice", 60*time.Second) cmd := redis.MustParse(parse, "psetex name 10000 bob") conn := redis.NewFakeConn() diff --git a/internal/command/string/set.go b/internal/command/string/set.go index e8a34f3..7e241df 100644 --- a/internal/command/string/set.go +++ b/internal/command/string/set.go @@ -69,7 +69,7 @@ func ParseSet(b redis.BaseCmd) (Set, error) { func (cmd Set) Run(w redis.Writer, red redis.Redka) (any, error) { if !cmd.ifNX && !cmd.ifXX && !cmd.get && !cmd.keepTTL && cmd.at.IsZero() { // Simple SET without additional options (except ttl). - err := red.Str().SetExpires(cmd.key, cmd.value, cmd.ttl) + err := red.Str().SetExpire(cmd.key, cmd.value, cmd.ttl) if err != nil { w.WriteError(cmd.Error(err)) return nil, err diff --git a/internal/command/string/setex.go b/internal/command/string/setex.go index 1908a7e..bf4c1bc 100644 --- a/internal/command/string/setex.go +++ b/internal/command/string/setex.go @@ -34,7 +34,7 @@ func ParseSetEX(b redis.BaseCmd, multi int) (SetEX, error) { } func (cmd SetEX) Run(w redis.Writer, red redis.Redka) (any, error) { - err := red.Str().SetExpires(cmd.key, cmd.value, cmd.ttl) + err := red.Str().SetExpire(cmd.key, cmd.value, cmd.ttl) if err != nil { w.WriteError(cmd.Error(err)) return nil, err diff --git a/internal/command/string/setex_test.go b/internal/command/string/setex_test.go index 564d35b..1e84168 100644 --- a/internal/command/string/setex_test.go +++ b/internal/command/string/setex_test.go @@ -104,7 +104,7 @@ func TestSetEXExec(t *testing.T) { t.Run("change ttl", func(t *testing.T) { red := getRedka(t) - _ = red.Str().SetExpires("name", "alice", 60*time.Second) + _ = red.Str().SetExpire("name", "alice", 60*time.Second) cmd := redis.MustParse(parse, "setex name 10 bob") conn := redis.NewFakeConn() diff --git a/internal/redis/redka.go b/internal/redis/redka.go index e5b5fd0..a812752 100644 --- a/internal/redis/redka.go +++ b/internal/redis/redka.go @@ -96,7 +96,7 @@ type RStr interface { Incr(key string, delta int) (int, error) IncrFloat(key string, delta float64) (float64, error) Set(key string, value any) error - SetExpires(key string, value any, ttl time.Duration) error + SetExpire(key string, value any, ttl time.Duration) error SetMany(items map[string]any) error SetWith(key string, value any) rstring.SetCmd } diff --git a/internal/rkey/db_test.go b/internal/rkey/db_test.go index bb2bf44..5e95d9f 100644 --- a/internal/rkey/db_test.go +++ b/internal/rkey/db_test.go @@ -103,8 +103,8 @@ func TestDeleteExpired(t *testing.T) { t.Run("delete all", func(t *testing.T) { db, kkey := getDB(t) - _ = db.Str().SetExpires("name", "alice", 1*time.Millisecond) - _ = db.Str().SetExpires("age", 25, 1*time.Millisecond) + _ = db.Str().SetExpire("name", "alice", 1*time.Millisecond) + _ = db.Str().SetExpire("age", 25, 1*time.Millisecond) time.Sleep(2 * time.Millisecond) count, err := kkey.DeleteExpired(0) @@ -117,8 +117,8 @@ func TestDeleteExpired(t *testing.T) { t.Run("delete n", func(t *testing.T) { db, kkey := getDB(t) - _ = db.Str().SetExpires("name", "alice", 1*time.Millisecond) - _ = db.Str().SetExpires("age", 25, 1*time.Millisecond) + _ = db.Str().SetExpire("name", "alice", 1*time.Millisecond) + _ = db.Str().SetExpire("age", 25, 1*time.Millisecond) time.Sleep(2 * time.Millisecond) count, err := kkey.DeleteExpired(1) diff --git a/internal/rstring/db.go b/internal/rstring/db.go index 2ed54d9..86cb833 100644 --- a/internal/rstring/db.go +++ b/internal/rstring/db.go @@ -82,12 +82,12 @@ func (d *DB) Set(key string, value any) error { return err } -// SetExpires sets the key value with an optional expiration time (if ttl > 0). +// SetExpire sets the key value with an optional expiration time (if ttl > 0). // Overwrites the value and ttl if the key already exists. // If the key exists but is not a string, returns ErrKeyType. -func (d *DB) SetExpires(key string, value any, ttl time.Duration) error { +func (d *DB) SetExpire(key string, value any, ttl time.Duration) error { err := d.update(func(tx *Tx) error { - return tx.SetExpires(key, value, ttl) + return tx.SetExpire(key, value, ttl) }) return err } diff --git a/internal/rstring/db_test.go b/internal/rstring/db_test.go index d602810..d105f83 100644 --- a/internal/rstring/db_test.go +++ b/internal/rstring/db_test.go @@ -464,11 +464,11 @@ func TestSetExists(t *testing.T) { }) } -func TestSetExpires(t *testing.T) { +func TestSetExpire(t *testing.T) { t.Run("no ttl", func(t *testing.T) { db, str := getDB(t) - err := str.SetExpires("name", "alice", 0) + err := str.SetExpire("name", "alice", 0) be.Err(t, err, nil) val, _ := str.Get("name") @@ -482,7 +482,7 @@ func TestSetExpires(t *testing.T) { now := time.Now() ttl := time.Second - err := str.SetExpires("name", "alice", ttl) + err := str.SetExpire("name", "alice", ttl) be.Err(t, err, nil) val, _ := str.Get("name") @@ -496,7 +496,7 @@ func TestSetExpires(t *testing.T) { t.Run("key type mismatch", func(t *testing.T) { db, str := getDB(t) _, _ = db.Hash().Set("person", "name", "alice") - err := str.SetExpires("person", "alice", time.Second) + err := str.SetExpire("person", "alice", time.Second) be.Err(t, err, core.ErrKeyType) _, err = str.Get("person") @@ -601,7 +601,7 @@ func TestSetWithAt(t *testing.T) { func TestSetWithKeepTTL(t *testing.T) { t.Run("delete ttl", func(t *testing.T) { db, str := getDB(t) - _ = str.SetExpires("name", "alice", 60*time.Second) + _ = str.SetExpire("name", "alice", 60*time.Second) _, err := str.SetWith("name", "bob").Run() be.Err(t, err, nil) @@ -615,7 +615,7 @@ func TestSetWithKeepTTL(t *testing.T) { t.Run("keep ttl", func(t *testing.T) { db, str := getDB(t) ttl := 60 * time.Second - _ = str.SetExpires("name", "alice", ttl) + _ = str.SetExpire("name", "alice", ttl) now := time.Now() _, err := str.SetWith("name", "alice").KeepTTL().Run() diff --git a/internal/rstring/tx.go b/internal/rstring/tx.go index 94dfc80..946ebf4 100644 --- a/internal/rstring/tx.go +++ b/internal/rstring/tx.go @@ -133,13 +133,13 @@ func (tx *Tx) IncrFloat(key string, delta float64) (float64, error) { // Overwrites the value if the key already exists. // If the key exists but is not a string, returns ErrKeyType. func (tx *Tx) Set(key string, value any) error { - return tx.SetExpires(key, value, 0) + return tx.SetExpire(key, value, 0) } -// SetExpires sets the key value with an optional expiration time (if ttl > 0). +// SetExpire sets the key value with an optional expiration time (if ttl > 0). // Overwrites the value and ttl if the key already exists. // If the key exists but is not a string, returns ErrKeyType. -func (tx *Tx) SetExpires(key string, value any, ttl time.Duration) error { +func (tx *Tx) SetExpire(key string, value any, ttl time.Duration) error { var at time.Time if ttl > 0 { at = time.Now().Add(ttl) diff --git a/redka_test.go b/redka_test.go index 0acf2e9..b4273e2 100644 --- a/redka_test.go +++ b/redka_test.go @@ -150,7 +150,7 @@ func ExampleDB_Key() { db, _ := redka.Open("file:/redka.db?vfs=memdb", nil) defer func() { _ = db.Close() }() - _ = db.Str().SetExpires("name", "alice", 60*time.Second) + _ = db.Str().SetExpire("name", "alice", 60*time.Second) _ = db.Str().Set("city", "paris") key, _ := db.Key().Get("name")