Skip to content

Commit

Permalink
feat: new method--Persist (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
qishenonly committed Jul 4, 2023
1 parent 6f9b73f commit cfd5485
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
17 changes: 17 additions & 0 deletions structure/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ type StringStructure struct {
db *engine.DB
}

var (
// ErrWrongNumberOfArguments is returned when the number of arguments is wrong
ErrWrongNumberOfArguments = errors.New("wrong number of arguments")
)

// NewStringStructure returns a new StringStructure
// It will return a nil StringStructure if the database cannot be opened
// or the database cannot be created
Expand Down Expand Up @@ -296,6 +301,18 @@ func (s *StringStructure) Expire(key []byte, ttl time.Duration) error {
return s.Set(key, value, ttl)
}

// Persist removes the expiration time of a key
func (s *StringStructure) Persist(key []byte) error {
// Get the value
value, err := s.Get(key)
if err != nil {
return err
}

// Set the value
return s.Set(key, value, 0)
}

// encodeStringValue encodes the value
// format: [type][expire][value]
// type: 1 byte
Expand Down
19 changes: 19 additions & 0 deletions structure/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,22 @@ func TestStringStructure_Expire(t *testing.T) {
assert.Equal(t, err, ErrKeyExpired)
assert.Equal(t, string(v2), "")
}

func TestStringStructure_Persist(t *testing.T) {
str := initdb()

err = str.Set(randkv.GetTestKey(1), []byte("1"), 0)
assert.Nil(t, err)

err = str.Expire(randkv.GetTestKey(1), 1*time.Second)
assert.Nil(t, err)
v1, err := str.Get(randkv.GetTestKey(1))
assert.Nil(t, err)
assert.Equal(t, string(v1), "1")

err = str.Persist(randkv.GetTestKey(1))
assert.Nil(t, err)
v2, err := str.Get(randkv.GetTestKey(1))
assert.Nil(t, err)
assert.Equal(t, string(v2), "1")
}

0 comments on commit cfd5485

Please sign in to comment.