mirror of
				https://github.com/nalgeon/redka.git
				synced 2025-11-01 03:42:41 +08:00 
			
		
		
		
	rstring: rename SetExpires to SetExpire
This commit is contained in:
		| @@ -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. | 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. | MGET         DB.Str().GetMany       Returns the values of one or more keys. | ||||||
| MSET         DB.Str().SetMany       Sets 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. | 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. | 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. | STRLEN       DB.Str().Get           Returns the length of a value in bytes. | ||||||
| ``` | ``` | ||||||
|   | |||||||
| @@ -87,7 +87,7 @@ func TestExpireExec(t *testing.T) { | |||||||
|  |  | ||||||
| 	t.Run("update expire", func(t *testing.T) { | 	t.Run("update expire", func(t *testing.T) { | ||||||
| 		red := getRedka(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") | 		cmd := redis.MustParse(parse, "expire name 30") | ||||||
| 		conn := redis.NewFakeConn() | 		conn := redis.NewFakeConn() | ||||||
|   | |||||||
| @@ -62,7 +62,7 @@ func TestPersistExec(t *testing.T) { | |||||||
|  |  | ||||||
| 	t.Run("volatile to persist", func(t *testing.T) { | 	t.Run("volatile to persist", func(t *testing.T) { | ||||||
| 		red := getRedka(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") | 		cmd := redis.MustParse(ParsePersist, "persist name") | ||||||
| 		conn := redis.NewFakeConn() | 		conn := redis.NewFakeConn() | ||||||
|   | |||||||
| @@ -88,7 +88,7 @@ func TestPExpireExec(t *testing.T) { | |||||||
|  |  | ||||||
| 	t.Run("update pexpire", func(t *testing.T) { | 	t.Run("update pexpire", func(t *testing.T) { | ||||||
| 		red := getRedka(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") | 		cmd := redis.MustParse(parse, "pexpire name 30000") | ||||||
| 		conn := redis.NewFakeConn() | 		conn := redis.NewFakeConn() | ||||||
|   | |||||||
| @@ -47,7 +47,7 @@ func TestTTLParse(t *testing.T) { | |||||||
| func TestTTLExec(t *testing.T) { | func TestTTLExec(t *testing.T) { | ||||||
| 	t.Run("has ttl", func(t *testing.T) { | 	t.Run("has ttl", func(t *testing.T) { | ||||||
| 		red := getRedka(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") | 		cmd := redis.MustParse(ParseTTL, "ttl name") | ||||||
| 		conn := redis.NewFakeConn() | 		conn := redis.NewFakeConn() | ||||||
|   | |||||||
| @@ -104,7 +104,7 @@ func TestPSetEXExec(t *testing.T) { | |||||||
|  |  | ||||||
| 	t.Run("change ttl", func(t *testing.T) { | 	t.Run("change ttl", func(t *testing.T) { | ||||||
| 		red := getRedka(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") | 		cmd := redis.MustParse(parse, "psetex name 10000 bob") | ||||||
| 		conn := redis.NewFakeConn() | 		conn := redis.NewFakeConn() | ||||||
|   | |||||||
| @@ -69,7 +69,7 @@ func ParseSet(b redis.BaseCmd) (Set, error) { | |||||||
| func (cmd Set) Run(w redis.Writer, red redis.Redka) (any, 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() { | 	if !cmd.ifNX && !cmd.ifXX && !cmd.get && !cmd.keepTTL && cmd.at.IsZero() { | ||||||
| 		// Simple SET without additional options (except ttl). | 		// 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 { | 		if err != nil { | ||||||
| 			w.WriteError(cmd.Error(err)) | 			w.WriteError(cmd.Error(err)) | ||||||
| 			return nil, err | 			return nil, err | ||||||
|   | |||||||
| @@ -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) { | 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 { | 	if err != nil { | ||||||
| 		w.WriteError(cmd.Error(err)) | 		w.WriteError(cmd.Error(err)) | ||||||
| 		return nil, err | 		return nil, err | ||||||
|   | |||||||
| @@ -104,7 +104,7 @@ func TestSetEXExec(t *testing.T) { | |||||||
|  |  | ||||||
| 	t.Run("change ttl", func(t *testing.T) { | 	t.Run("change ttl", func(t *testing.T) { | ||||||
| 		red := getRedka(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") | 		cmd := redis.MustParse(parse, "setex name 10 bob") | ||||||
| 		conn := redis.NewFakeConn() | 		conn := redis.NewFakeConn() | ||||||
|   | |||||||
| @@ -96,7 +96,7 @@ type RStr interface { | |||||||
| 	Incr(key string, delta int) (int, error) | 	Incr(key string, delta int) (int, error) | ||||||
| 	IncrFloat(key string, delta float64) (float64, error) | 	IncrFloat(key string, delta float64) (float64, error) | ||||||
| 	Set(key string, value any) 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 | 	SetMany(items map[string]any) error | ||||||
| 	SetWith(key string, value any) rstring.SetCmd | 	SetWith(key string, value any) rstring.SetCmd | ||||||
| } | } | ||||||
|   | |||||||
| @@ -103,8 +103,8 @@ func TestDeleteExpired(t *testing.T) { | |||||||
| 	t.Run("delete all", func(t *testing.T) { | 	t.Run("delete all", func(t *testing.T) { | ||||||
| 		db, kkey := getDB(t) | 		db, kkey := getDB(t) | ||||||
|  |  | ||||||
| 		_ = db.Str().SetExpires("name", "alice", 1*time.Millisecond) | 		_ = db.Str().SetExpire("name", "alice", 1*time.Millisecond) | ||||||
| 		_ = db.Str().SetExpires("age", 25, 1*time.Millisecond) | 		_ = db.Str().SetExpire("age", 25, 1*time.Millisecond) | ||||||
|  |  | ||||||
| 		time.Sleep(2 * time.Millisecond) | 		time.Sleep(2 * time.Millisecond) | ||||||
| 		count, err := kkey.DeleteExpired(0) | 		count, err := kkey.DeleteExpired(0) | ||||||
| @@ -117,8 +117,8 @@ func TestDeleteExpired(t *testing.T) { | |||||||
| 	t.Run("delete n", func(t *testing.T) { | 	t.Run("delete n", func(t *testing.T) { | ||||||
| 		db, kkey := getDB(t) | 		db, kkey := getDB(t) | ||||||
|  |  | ||||||
| 		_ = db.Str().SetExpires("name", "alice", 1*time.Millisecond) | 		_ = db.Str().SetExpire("name", "alice", 1*time.Millisecond) | ||||||
| 		_ = db.Str().SetExpires("age", 25, 1*time.Millisecond) | 		_ = db.Str().SetExpire("age", 25, 1*time.Millisecond) | ||||||
|  |  | ||||||
| 		time.Sleep(2 * time.Millisecond) | 		time.Sleep(2 * time.Millisecond) | ||||||
| 		count, err := kkey.DeleteExpired(1) | 		count, err := kkey.DeleteExpired(1) | ||||||
|   | |||||||
| @@ -82,12 +82,12 @@ func (d *DB) Set(key string, value any) error { | |||||||
| 	return err | 	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. | // Overwrites the value and ttl if the key already exists. | ||||||
| // If the key exists but is not a string, returns ErrKeyType. | // 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 { | 	err := d.update(func(tx *Tx) error { | ||||||
| 		return tx.SetExpires(key, value, ttl) | 		return tx.SetExpire(key, value, ttl) | ||||||
| 	}) | 	}) | ||||||
| 	return err | 	return err | ||||||
| } | } | ||||||
|   | |||||||
| @@ -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) { | 	t.Run("no ttl", func(t *testing.T) { | ||||||
| 		db, str := getDB(t) | 		db, str := getDB(t) | ||||||
|  |  | ||||||
| 		err := str.SetExpires("name", "alice", 0) | 		err := str.SetExpire("name", "alice", 0) | ||||||
| 		be.Err(t, err, nil) | 		be.Err(t, err, nil) | ||||||
|  |  | ||||||
| 		val, _ := str.Get("name") | 		val, _ := str.Get("name") | ||||||
| @@ -482,7 +482,7 @@ func TestSetExpires(t *testing.T) { | |||||||
|  |  | ||||||
| 		now := time.Now() | 		now := time.Now() | ||||||
| 		ttl := time.Second | 		ttl := time.Second | ||||||
| 		err := str.SetExpires("name", "alice", ttl) | 		err := str.SetExpire("name", "alice", ttl) | ||||||
| 		be.Err(t, err, nil) | 		be.Err(t, err, nil) | ||||||
|  |  | ||||||
| 		val, _ := str.Get("name") | 		val, _ := str.Get("name") | ||||||
| @@ -496,7 +496,7 @@ func TestSetExpires(t *testing.T) { | |||||||
| 	t.Run("key type mismatch", func(t *testing.T) { | 	t.Run("key type mismatch", func(t *testing.T) { | ||||||
| 		db, str := getDB(t) | 		db, str := getDB(t) | ||||||
| 		_, _ = db.Hash().Set("person", "name", "alice") | 		_, _ = 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) | 		be.Err(t, err, core.ErrKeyType) | ||||||
|  |  | ||||||
| 		_, err = str.Get("person") | 		_, err = str.Get("person") | ||||||
| @@ -601,7 +601,7 @@ func TestSetWithAt(t *testing.T) { | |||||||
| func TestSetWithKeepTTL(t *testing.T) { | func TestSetWithKeepTTL(t *testing.T) { | ||||||
| 	t.Run("delete ttl", func(t *testing.T) { | 	t.Run("delete ttl", func(t *testing.T) { | ||||||
| 		db, str := getDB(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() | 		_, err := str.SetWith("name", "bob").Run() | ||||||
| 		be.Err(t, err, nil) | 		be.Err(t, err, nil) | ||||||
| @@ -615,7 +615,7 @@ func TestSetWithKeepTTL(t *testing.T) { | |||||||
| 	t.Run("keep ttl", func(t *testing.T) { | 	t.Run("keep ttl", func(t *testing.T) { | ||||||
| 		db, str := getDB(t) | 		db, str := getDB(t) | ||||||
| 		ttl := 60 * time.Second | 		ttl := 60 * time.Second | ||||||
| 		_ = str.SetExpires("name", "alice", ttl) | 		_ = str.SetExpire("name", "alice", ttl) | ||||||
|  |  | ||||||
| 		now := time.Now() | 		now := time.Now() | ||||||
| 		_, err := str.SetWith("name", "alice").KeepTTL().Run() | 		_, err := str.SetWith("name", "alice").KeepTTL().Run() | ||||||
|   | |||||||
| @@ -133,13 +133,13 @@ func (tx *Tx) IncrFloat(key string, delta float64) (float64, error) { | |||||||
| // Overwrites the value if the key already exists. | // Overwrites the value if the key already exists. | ||||||
| // If the key exists but is not a string, returns ErrKeyType. | // If the key exists but is not a string, returns ErrKeyType. | ||||||
| func (tx *Tx) Set(key string, value any) error { | 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. | // Overwrites the value and ttl if the key already exists. | ||||||
| // If the key exists but is not a string, returns ErrKeyType. | // 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 | 	var at time.Time | ||||||
| 	if ttl > 0 { | 	if ttl > 0 { | ||||||
| 		at = time.Now().Add(ttl) | 		at = time.Now().Add(ttl) | ||||||
|   | |||||||
| @@ -150,7 +150,7 @@ func ExampleDB_Key() { | |||||||
| 	db, _ := redka.Open("file:/redka.db?vfs=memdb", nil) | 	db, _ := redka.Open("file:/redka.db?vfs=memdb", nil) | ||||||
| 	defer func() { _ = db.Close() }() | 	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") | 	_ = db.Str().Set("city", "paris") | ||||||
|  |  | ||||||
| 	key, _ := db.Key().Get("name") | 	key, _ := db.Key().Get("name") | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Anton
					Anton