-
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.
- Loading branch information
Daniel Menet
committed
Aug 21, 2023
1 parent
00700b8
commit c5d402d
Showing
4 changed files
with
160 additions
and
5 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,46 @@ | ||
package main | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type cache struct { | ||
timeout time.Duration | ||
store map[string]cacheElement | ||
} | ||
|
||
func NewCache(timeout time.Duration) *cache { | ||
return &cache{ | ||
timeout: timeout, | ||
store: map[string]cacheElement{}, | ||
} | ||
} | ||
|
||
func (c *cache) Add(key string, data []byte) { | ||
c.store[key] = cacheElement{ | ||
cachedAt: time.Now(), | ||
data: data, | ||
} | ||
} | ||
|
||
func (c *cache) Get(key string) ([]byte, bool) { | ||
elem, ok := c.store[key] | ||
if !ok { | ||
return nil, false | ||
} | ||
age := time.Since(elem.cachedAt) | ||
if age >= c.timeout { | ||
delete(c.store, key) | ||
return nil, false | ||
} | ||
return elem.data, true | ||
} | ||
|
||
func (c *cache) Purge() { | ||
c.store = make(map[string]cacheElement) | ||
} | ||
|
||
type cacheElement struct { | ||
data []byte | ||
cachedAt time.Time | ||
} |
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,93 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestCache_Add(t *testing.T) { | ||
// Create a new cache instance | ||
cache := NewCache(time.Minute) | ||
|
||
// Add a cache element with a key and data | ||
key := "test" | ||
data := []byte("test data") | ||
cache.Add(key, data) | ||
|
||
// Check if the cache element exists in the store | ||
if _, ok := cache.store[key]; !ok { | ||
t.Errorf("Cache element with key '%s' not found", key) | ||
} | ||
|
||
// Check if the data of the cache element is correct | ||
if got := cache.store[key].data; !bytes.Equal(got, data) { | ||
t.Errorf("Cache element data mismatch, got %v, want %v", got, data) | ||
} | ||
} | ||
|
||
func TestCache_Get(t *testing.T) { | ||
// Create a new cache instance | ||
cache := NewCache(time.Minute) | ||
|
||
// Add a cache element with a key and data | ||
key := "test" | ||
data := []byte("test data") | ||
cache.Add(key, data) | ||
|
||
// Retrieve the cache element using the Get method | ||
got, ok := cache.Get(key) | ||
|
||
// Check if the cache element is found | ||
if !ok { | ||
t.Errorf("Cache element with key '%s' not found", key) | ||
} | ||
|
||
// Check if the retrieved data is correct | ||
if string(got) != string(data) { | ||
t.Errorf("Retrieved data mismatch, got %s, want %s", got, data) | ||
} | ||
} | ||
|
||
func TestCache_Get_Expired(t *testing.T) { | ||
// Create a new cache instance with a short timeout duration | ||
cache := NewCache(1 * time.Millisecond) | ||
|
||
// Add a cache element with a key and data | ||
key := "test" | ||
data := []byte("test data") | ||
cache.Add(key, data) | ||
|
||
// Wait for the cache element to expire | ||
time.Sleep(2 * time.Millisecond) | ||
|
||
// Retrieve the cache element using the Get method | ||
got, ok := cache.Get(key) | ||
|
||
// Check if the cache element is not found | ||
if ok { | ||
t.Errorf("Cache element with key '%s' found, expected not found", key) | ||
} | ||
|
||
// Check if the retrieved data is nil | ||
if got != nil { | ||
t.Errorf("Retrieved data mismatch, got %v, want nil", got) | ||
} | ||
} | ||
|
||
func TestCache_Purge(t *testing.T) { | ||
// Create a new cache instance | ||
cache := NewCache(time.Minute) | ||
|
||
// Add multiple cache elements | ||
cache.Add("key1", []byte("data1")) | ||
cache.Add("key2", []byte("data2")) | ||
|
||
// Purge the cache | ||
cache.Purge() | ||
|
||
// Check if the store is empty | ||
if len(cache.store) != 0 { | ||
t.Errorf("Cache store is not empty after purging") | ||
} | ||
} |
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