-
-
Notifications
You must be signed in to change notification settings - Fork 981
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
175 additions
and
77 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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,83 @@ | ||
package cache | ||
|
||
import ( | ||
"crypto/sha256" | ||
"fmt" | ||
"sync" | ||
"time" | ||
) | ||
|
||
// Cache - generic cache implementation | ||
type Cache[V any] struct { | ||
Cache map[string]V | ||
Mutex *sync.Mutex | ||
} | ||
|
||
// NewCache - create new cache with generic type V | ||
func NewCache[V any]() *Cache[V] { | ||
return &Cache[V]{ | ||
Cache: make(map[string]V), | ||
Mutex: &sync.Mutex{}, | ||
} | ||
} | ||
|
||
// Get - fetch value from cache by key | ||
func (c *Cache[V]) Get(key string) (V, bool) { | ||
c.Mutex.Lock() | ||
defer c.Mutex.Unlock() | ||
keyHash := sha256.Sum256([]byte(key)) | ||
cacheKey := fmt.Sprintf("%x", keyHash) | ||
value, found := c.Cache[cacheKey] | ||
return value, found | ||
} | ||
|
||
// Put - put value into cache by key | ||
func (c *Cache[V]) Put(key string, value V) { | ||
c.Mutex.Lock() | ||
defer c.Mutex.Unlock() | ||
keyHash := sha256.Sum256([]byte(key)) | ||
cacheKey := fmt.Sprintf("%x", keyHash) | ||
c.Cache[cacheKey] = value | ||
} | ||
|
||
// ExpiringItem - item with expiration time | ||
type ExpiringItem[V any] struct { | ||
Value V | ||
Expiration time.Time | ||
} | ||
|
||
// ExpiringCache - cache with items with expiration time | ||
type ExpiringCache[V any] struct { | ||
Cache map[string]ExpiringItem[V] | ||
Mutex *sync.Mutex | ||
} | ||
|
||
// NewExpiringCache - create new cache with generic type V | ||
func NewExpiringCache[V any]() *ExpiringCache[V] { | ||
return &ExpiringCache[V]{ | ||
Cache: make(map[string]ExpiringItem[V]), | ||
Mutex: &sync.Mutex{}, | ||
} | ||
} | ||
|
||
// Get - fetch value from cache by key | ||
func (c *ExpiringCache[V]) Get(key string) (V, bool) { | ||
c.Mutex.Lock() | ||
defer c.Mutex.Unlock() | ||
item, found := c.Cache[key] | ||
if !found { | ||
return item.Value, false | ||
} | ||
if time.Now().After(item.Expiration) { | ||
delete(c.Cache, key) | ||
return item.Value, false | ||
} | ||
return item.Value, true | ||
} | ||
|
||
// Put - put value into cache by key | ||
func (c *ExpiringCache[V]) Put(key string, value V, expiration time.Time) { | ||
c.Mutex.Lock() | ||
defer c.Mutex.Unlock() | ||
c.Cache[key] = ExpiringItem[V]{Value: value, Expiration: expiration} | ||
} |
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,79 @@ | ||
package cache | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCacheCreation(t *testing.T) { | ||
t.Parallel() | ||
|
||
cache := NewCache[string]() | ||
|
||
assert.NotNil(t, cache.Mutex) | ||
assert.NotNil(t, cache.Cache) | ||
|
||
assert.Equal(t, 0, len(cache.Cache)) | ||
} | ||
|
||
func TestStringCacheOperation(t *testing.T) { | ||
t.Parallel() | ||
|
||
cache := NewCache[string]() | ||
|
||
value, found := cache.Get("potato") | ||
|
||
assert.False(t, found) | ||
assert.Empty(t, value) | ||
|
||
cache.Put("potato", "carrot") | ||
value, found = cache.Get("potato") | ||
|
||
assert.True(t, found) | ||
assert.NotEmpty(t, value) | ||
assert.Equal(t, "carrot", value) | ||
} | ||
|
||
func TestExpiringCacheCreation(t *testing.T) { | ||
t.Parallel() | ||
|
||
cache := NewExpiringCache[string]() | ||
|
||
assert.NotNil(t, cache.Mutex) | ||
assert.NotNil(t, cache.Cache) | ||
|
||
assert.Equal(t, 0, len(cache.Cache)) | ||
} | ||
|
||
func TestExpiringCacheOperation(t *testing.T) { | ||
t.Parallel() | ||
|
||
cache := NewExpiringCache[string]() | ||
|
||
value, found := cache.Get("potato") | ||
|
||
assert.False(t, found) | ||
assert.Empty(t, value) | ||
|
||
cache.Put("potato", "carrot", time.Now().Add(1*time.Second)) | ||
value, found = cache.Get("potato") | ||
|
||
assert.True(t, found) | ||
assert.NotEmpty(t, value) | ||
assert.Equal(t, "carrot", value) | ||
} | ||
|
||
func TestExpiringCacheExpiration(t *testing.T) { | ||
t.Parallel() | ||
|
||
cache := NewExpiringCache[string]() | ||
|
||
cache.Put("potato", "carrot", time.Now().Add(-1*time.Second)) | ||
value, found := cache.Get("potato") | ||
|
||
assert.False(t, found) | ||
assert.NotEmpty(t, value) | ||
assert.Equal(t, "carrot", value) | ||
} |