Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide alternative operation mode for cache for code users #82

Merged
merged 2 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ import (
// Manager contains functions to manage cache instances.
// It provides functions for creating new cache instances and list all of the names of existing cache instances
// Each cache instance has unique name and its own cache size and TTL configuration.
//
// Two modes of operation are available
//
// Default behavior (when the normal Get() methods are used to access the cache):
// - Item is added to the cache
// - After the expiry time, it is automatically purged from the cache to release memory
// - ** THIS PART IS NOT IMPLEMENTED, AND A REAPER NEEDS TO BE ADDED **
// - While it's in memory, it is returned from the Get() methods
// - The TTL is extended on each access, so it's time-to-live since last access
//
// Optional behavior (when calling code uses GetUnexpired() to access the cache):
// - The value is only returned if it has not expired yet (regardless of whether it has been reaped)
// - The TTL is NOT extended on each access
type Manager interface {
// Get a cache by name, if a cache already exists with the same name, it will be returned as is without checking maxSize, ttl and enabled matches
GetCache(ctx context.Context, namespace, name string, maxSize int64, ttl time.Duration, enabled bool) (CInterface, error)
Expand All @@ -42,6 +55,7 @@ type CInterface interface {
Delete(key string) bool

Get(key string) interface{}
GetUnexpired(key string) interface{}
Set(key string, val interface{})

GetString(key string) string
Expand Down Expand Up @@ -78,6 +92,18 @@ func (c *CCache) Get(key string) interface{} {
return nil
}

// GetNotExpired retrieves from the cache, without extending the expiry time, and
// if the existing TTL has already popped (but the item has not yet been reaped)
// we will return nil.
func (c *CCache) GetUnexpired(key string) interface{} {
if c.enabled {
if cached := c.cache.Get(key); cached != nil && !cached.Expired() {
return cached.Value()
}
}
return nil
}

func (c *CCache) Delete(key string) bool {
if c.enabled {
return c.cache.Delete(key)
Expand Down
16 changes: 16 additions & 0 deletions pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,19 @@ func TestResetCachesForNamespace(t *testing.T) {
assert.Nil(t, cacheNS1_b.Get("key1"))

}

func TestGetUnexpired(t *testing.T) {
ctx := context.Background()
cacheManager := NewCacheManager(ctx, true)
cache0, _ := cacheManager.GetCache(ctx, "ns1", "cacheA", 1, time.Nanosecond, true)
cache1, _ := cacheManager.GetCache(ctx, "ns1", "cacheB", 1, time.Hour, true)

cache0.Set("test", "will expire")
cache1.Set("test", "will not expire")

for cache0.GetUnexpired("test") != nil {
time.Sleep(1 * time.Millisecond)
}

assert.NotNil(t, cache1.GetUnexpired("test"))
}