Skip to content

Commit

Permalink
fix: Clear() should only clear the values and not session
Browse files Browse the repository at this point in the history
  • Loading branch information
vividvilla committed May 30, 2024
1 parent f8611d6 commit 4ee7fae
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion stores/memory/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (s *Store) Clear(id string) error {
if !ok {
return ErrInvalidSession
}
delete(s.sessions, id)
s.sessions[id] = make(map[string]interface{})

return nil
}
Expand Down
3 changes: 2 additions & 1 deletion stores/memory/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ func TestClear(t *testing.T) {

err = str.Clear(id)
assert.NoError(t, err)
assert.NotContains(t, str.sessions, id)
assert.Contains(t, str.sessions, id)
assert.Equal(t, len(str.sessions[id]), 0)
}

func TestInt(t *testing.T) {
Expand Down
9 changes: 8 additions & 1 deletion stores/redis/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,14 @@ func (s *Store) Delete(id string, key string) error {

// Clear clears session in redis.
func (s *Store) Clear(id string) error {
return s.client.Del(s.clientCtx, s.prefix+id).Err()
p := s.client.TxPipeline()
p.Del(s.clientCtx, s.prefix+id).Err()
p.HSet(s.clientCtx, s.prefix+id, defaultSessKey, "1")
if s.ttl > 0 {
p.Expire(s.clientCtx, s.prefix+id, s.ttl)
}
_, err := p.Exec(s.clientCtx)
return err
}

// Int converts interface to integer.
Expand Down
11 changes: 5 additions & 6 deletions stores/redis/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,17 +275,16 @@ func TestClear(t *testing.T) {
err := client.HMSet(context.TODO(), str.prefix+key, defaultSessKey, "1", field1, value1, field2, value2).Err()
assert.NoError(t, err)

// Check if its set
val, err := client.Exists(context.TODO(), str.prefix+key).Result()
err = str.Clear(key)
assert.NoError(t, err)
assert.NotEqual(t, val, int64(0))

err = str.Clear(key)
val, err := client.HExists(context.TODO(), str.prefix+key, defaultSessKey).Result()
assert.NoError(t, err)
assert.True(t, val)

val, err = client.Exists(context.TODO(), str.prefix+key).Result()
val, err = client.HExists(context.TODO(), str.prefix+key, field1).Result()
assert.NoError(t, err)
assert.Equal(t, val, int64(0))
assert.False(t, val)
}

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

0 comments on commit 4ee7fae

Please sign in to comment.