Skip to content

Commit

Permalink
Using simple set redis commands to invalidate tags (#3)
Browse files Browse the repository at this point in the history
* Using simple set redis commands for invalidate tags
  • Loading branch information
plbouchard committed Jan 19, 2021
1 parent 07cc0a0 commit 371ff1e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 43 deletions.
12 changes: 5 additions & 7 deletions store/redis.go
Expand Up @@ -10,22 +10,20 @@ import (
// RedisClientInterface represents a go-redis/redis client
type RedisClientInterface interface {
Get(key string) *redis.StringCmd
HGetAll(key string) *redis.StringStringMapCmd
TTL(key string) *redis.DurationCmd
Expire(key string, expiration time.Duration) *redis.BoolCmd
Set(key string, values interface{}, expiration time.Duration) *redis.StatusCmd
HSet(key string, values ...interface{}) *redis.IntCmd
Del(keys ...string) *redis.IntCmd
FlushAll() *redis.StatusCmd
SAdd(key string, members ...interface{}) *redis.IntCmd
SMembers(key string) *redis.StringSliceCmd
}

const (
// RedisType represents the storage type as a string value
RedisType = "redis"
// RedisTagPattern represents the tag pattern to be used as a key in specified storage
RedisTagPattern = "gocache_tag_%s"
// RedisEmptyValue represents an empty value to be used in hsets when the content is not used
RedisEmptyValue = 0
)

// RedisStore is a store for Redis
Expand Down Expand Up @@ -87,7 +85,7 @@ func (s *RedisStore) Set(key interface{}, value interface{}, options *Options) e
func (s *RedisStore) setTags(key interface{}, tags []string) {
for _, tag := range tags {
tagKey := fmt.Sprintf(RedisTagPattern, tag)
s.client.HSet(tagKey, key.(string), RedisEmptyValue)
s.client.SAdd(tagKey, key.(string))
s.client.Expire(tagKey, 720*time.Hour)
}
}
Expand All @@ -103,12 +101,12 @@ func (s *RedisStore) Invalidate(options InvalidateOptions) error {
if tags := options.TagsValue(); len(tags) > 0 {
for _, tag := range tags {
tagKey := fmt.Sprintf(RedisTagPattern, tag)
cacheKeys, err := s.client.HGetAll(tagKey).Result()
cacheKeys, err := s.client.SMembers(tagKey).Result()
if err != nil {
continue
}

for cacheKey := range cacheKeys {
for _, cacheKey := range cacheKeys {
s.Delete(cacheKey)
}

Expand Down
6 changes: 3 additions & 3 deletions store/redis_test.go
Expand Up @@ -105,7 +105,7 @@ func TestRedisSetWithTags(t *testing.T) {

client := mocksStore.NewMockRedisClientInterface(ctrl)
client.EXPECT().Set(cacheKey, cacheValue, time.Duration(0)).Return(&redis.StatusCmd{})
client.EXPECT().HSet("gocache_tag_tag1", "my-key", 0).Return(&redis.IntCmd{})
client.EXPECT().SAdd("gocache_tag_tag1", "my-key").Return(&redis.IntCmd{})
client.EXPECT().Expire("gocache_tag_tag1", 720*time.Hour).Return(&redis.BoolCmd{})

store := NewRedis(client, nil)
Expand Down Expand Up @@ -145,10 +145,10 @@ func TestRedisInvalidate(t *testing.T) {
Tags: []string{"tag1"},
}

cacheKeys := &redis.StringStringMapCmd{}
cacheKeys := &redis.StringSliceCmd{}

client := mocksStore.NewMockRedisClientInterface(ctrl)
client.EXPECT().HGetAll("gocache_tag_tag1").Return(cacheKeys)
client.EXPECT().SMembers("gocache_tag_tag1").Return(cacheKeys)
client.EXPECT().Del("gocache_tag_tag1").Return(&redis.IntCmd{})

store := NewRedis(client, nil)
Expand Down
66 changes: 33 additions & 33 deletions test/mocks/store/clients/redis_interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 371ff1e

Please sign in to comment.