-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Use localStorage for memqueue tests instead of Redis for #162
- Loading branch information
Showing
4 changed files
with
93 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package taskq | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
"github.com/hashicorp/golang-lru/simplelru" | ||
) | ||
|
||
type Storage interface { | ||
Exists(ctx context.Context, key string) bool | ||
} | ||
|
||
var _ Storage = (*localStorage)(nil) | ||
var _ Storage = (*redisStorage)(nil) | ||
|
||
// LOCAL | ||
|
||
type localStorage struct { | ||
mu sync.Mutex | ||
cache *simplelru.LRU | ||
} | ||
|
||
func NewLocalStorage() Storage { | ||
return &localStorage{} | ||
} | ||
|
||
func (s *localStorage) Exists(_ context.Context, key string) bool { | ||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
|
||
if s.cache == nil { | ||
var err error | ||
s.cache, err = simplelru.NewLRU(128000, nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
_, ok := s.cache.Get(key) | ||
if ok { | ||
return true | ||
} | ||
|
||
s.cache.Add(key, nil) | ||
return false | ||
} | ||
|
||
// REDIS | ||
|
||
type redisStorage struct { | ||
redis Redis | ||
} | ||
|
||
func newRedisStorage(redis Redis) Storage { | ||
return &redisStorage{ | ||
redis: redis, | ||
} | ||
} | ||
|
||
func (s *redisStorage) Exists(ctx context.Context, key string) bool { | ||
val, err := s.redis.SetNX(ctx, key, "", 24*time.Hour).Result() | ||
if err != nil { | ||
return true | ||
} | ||
return !val | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters