-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
185 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package memcache | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/mehmetumit/dexus/internal/core/ports" | ||
) | ||
|
||
type CacheMap map[string]string | ||
type MemCache struct { | ||
sync.RWMutex //Useful for concurrent nonblocking reads | ||
logger ports.Logger | ||
cacheMap CacheMap | ||
} | ||
|
||
func NewMemCache(l ports.Logger) *MemCache { | ||
return &MemCache{ | ||
logger: l, | ||
cacheMap: make(CacheMap), | ||
} | ||
|
||
} | ||
|
||
func (mc *MemCache) expireAfter(key string, ttl time.Duration) { | ||
time.AfterFunc(ttl, func() { | ||
mc.Lock() | ||
defer mc.Unlock() | ||
delete(mc.cacheMap, key) | ||
}) | ||
} | ||
func (mc *MemCache) Get(ctx context.Context, key string) (string, error) { | ||
mc.RLock() | ||
defer mc.RUnlock() | ||
val, ok := mc.cacheMap[key] | ||
if !ok { | ||
return "", ports.ErrKeyNotFound | ||
} | ||
return val, nil | ||
|
||
} | ||
func (mc *MemCache) Set(ctx context.Context, key string, val string, ttl time.Duration) error { | ||
mc.Lock() | ||
defer mc.Unlock() | ||
mc.cacheMap[key] = val | ||
mc.expireAfter(key, ttl) | ||
return nil | ||
} | ||
|
||
func (mc *MemCache) Delete(ctx context.Context, key string) error { | ||
mc.Lock() | ||
defer mc.Unlock() | ||
delete(mc.cacheMap, key) | ||
return nil | ||
} | ||
func (mc *MemCache) GenKey(ctx context.Context, s string) (string, error) { | ||
hashKey := uuid.NewSHA1(uuid.NameSpaceOID, []byte(s)).String() | ||
return hashKey, nil | ||
} | ||
func (mc *MemCache) Flush(ctx context.Context) error { | ||
mc.Lock() | ||
defer mc.Unlock() | ||
clear(mc.cacheMap) | ||
return nil | ||
} |
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,118 @@ | ||
package memcache | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/mehmetumit/dexus/internal/core/ports" | ||
"github.com/mehmetumit/dexus/internal/mocks" | ||
) | ||
|
||
func newTestMemCache(t testing.TB) *MemCache { | ||
t.Helper() | ||
mockLogger := mocks.NewMockLogger() | ||
return NewMemCache(mockLogger) | ||
} | ||
|
||
func TestMemCache_GenKey_Set_Get_GetNotFound_Flush_ExpireAfter(t *testing.T) { | ||
memCache := newTestMemCache(t) | ||
ctx := context.Background() | ||
cacheTTL := 5 * time.Second | ||
cacheMap := map[string]string{ | ||
"a-test-path": "https://test-path.com", | ||
"this/is/test": "https://this-is-test.com", | ||
"test1234": "http://test1234.com", | ||
} | ||
//Hashed key : path key | ||
var keyMap map[string]string = make(map[string]string) | ||
|
||
t.Run("GenKey Set Get", func(t *testing.T) { | ||
for k, v := range cacheMap { | ||
hashKey, err := memCache.GenKey(ctx, k) | ||
if err != nil { | ||
t.Errorf("Expected error nil, got %v", err) | ||
} | ||
keyMap[hashKey] = k | ||
memCache.Set(ctx, hashKey, v, cacheTTL) | ||
} | ||
for k, v := range keyMap { | ||
val, err := memCache.Get(ctx, k) | ||
if err != nil { | ||
t.Errorf("Expected error nil, got %v", err) | ||
} | ||
if val != cacheMap[v] { | ||
t.Errorf("Expected redirection url %v, got %v", cacheMap[v], val) | ||
} | ||
} | ||
}) | ||
|
||
t.Run("Get Not Found", func(t *testing.T) { | ||
|
||
notFoundKeys := []string{ | ||
"not-found", | ||
"1234/not/found", | ||
"", | ||
" ", | ||
} | ||
for _, nfk := range notFoundKeys { | ||
_, err := memCache.Get(ctx, nfk) | ||
if err != ports.ErrKeyNotFound { | ||
t.Errorf("Expected error %v, got %v", ports.ErrKeyNotFound, err) | ||
} | ||
} | ||
}) | ||
t.Run("Flush", func(t *testing.T) { | ||
|
||
err := memCache.Flush(ctx) | ||
if err != nil { | ||
t.Errorf("Expected error nil, got %v", err) | ||
} | ||
for k := range keyMap { | ||
_, err := memCache.Get(ctx, k) | ||
if err != ports.ErrKeyNotFound { | ||
t.Errorf("Expected error %v, got %v", ports.ErrKeyNotFound, err) | ||
} | ||
|
||
} | ||
}) | ||
t.Run("Delete", func(t *testing.T) { | ||
for k, v := range cacheMap { | ||
hashKey, err := memCache.GenKey(ctx, k) | ||
if err != nil { | ||
t.Errorf("Expected error nil, got %v", err) | ||
} | ||
memCache.Set(ctx, hashKey, v, cacheTTL) | ||
} | ||
for k := range keyMap { | ||
err := memCache.Delete(ctx, k) | ||
if err != nil { | ||
t.Errorf("Expected error nil, got %v", err) | ||
} | ||
_, err = memCache.Get(ctx, k) | ||
if err != ports.ErrKeyNotFound { | ||
t.Errorf("Expected error %v, got %v", ports.ErrKeyNotFound, err) | ||
} | ||
} | ||
|
||
}) | ||
t.Run("Expire After", func(t *testing.T) { | ||
ttl := 500 * time.Millisecond | ||
for k, v := range cacheMap { | ||
hashKey, err := memCache.GenKey(ctx, k) | ||
if err != nil { | ||
t.Errorf("Expected error nil, got %v", err) | ||
} | ||
memCache.Set(ctx, hashKey, v, ttl) | ||
} | ||
|
||
time.Sleep(ttl * 2) | ||
for k := range keyMap { | ||
_, err := memCache.Get(ctx, k) | ||
if err != ports.ErrKeyNotFound { | ||
t.Errorf("Expected error %v, got %v", ports.ErrKeyNotFound, err) | ||
} | ||
} | ||
}) | ||
|
||
} |