Skip to content

Commit

Permalink
Merge pull request ByteStorage#272 from ByteStorage/key_expire
Browse files Browse the repository at this point in the history
fix bug when key is expire
  • Loading branch information
qishenonly authored Aug 10, 2023
2 parents 580865f + e851f22 commit b573cea
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
13 changes: 11 additions & 2 deletions structure/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,17 @@ func (s *StringStructure) DecrBy(key string, amount int, ttl int64) error {
// Keys returns all keys matching pattern
func (s *StringStructure) Keys() ([]string, error) {
var keys []string
byte_keys := s.db.GetListKeys()
for _, key := range byte_keys {
byteKeys := s.db.GetListKeys()
for _, key := range byteKeys {
// check if key is expired
_, err := s.Get(string(key))
if err != nil {
if errors.Is(err, _const.ErrKeyIsExpired) {
continue
} else {
return nil, err
}
}
keys = append(keys, string(key))
}
return keys, nil
Expand Down
8 changes: 5 additions & 3 deletions structure/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,10 +425,10 @@ func TestStringStructure_Keys(t *testing.T) {
str, _ := initdb()
defer str.db.Clean()

err = str.Set("1", randkv.RandomValue(100), 0)
err = str.Set("1", randkv.RandomValue(100), 1)
assert.Nil(t, err)

err = str.Set("2", randkv.RandomValue(100), 0)
err = str.Set("2", randkv.RandomValue(100), 2)
assert.Nil(t, err)

err = str.Set("3", randkv.RandomValue(100), 0)
Expand All @@ -440,9 +440,11 @@ func TestStringStructure_Keys(t *testing.T) {
err = str.Set("你好", randkv.RandomValue(100), 0)
assert.Nil(t, err)

time.Sleep(2 * time.Second)

keys, err := str.Keys()
assert.Nil(t, err)
assert.Equal(t, len(keys), 5)
assert.Equal(t, len(keys), 3)
}

func TestStringStructure_TTL(t *testing.T) {
Expand Down

0 comments on commit b573cea

Please sign in to comment.