-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for defining TTL for image credentials cache
- Loading branch information
Showing
13 changed files
with
496 additions
and
79 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
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,33 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"math" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
var ( | ||
ErrNotFound = errors.New("item not found") | ||
) | ||
|
||
type Cache[T any] interface { | ||
// Get retrieves the cached value for the given key. | ||
// If the key is not found or expired, the method should return ErrNotFound. | ||
Get(ctx context.Context, key string) (T, error) | ||
// Set stores the value in the cache with the given key. | ||
// If ttl is 0, the item should not be cached and this method should return no error. | ||
Set(ctx context.Context, key string, value T, ttl time.Duration) error | ||
} | ||
|
||
// IsCacheMiss returns true if the error is a cache miss error. | ||
// This is a helper function to determine so users don't have to compare errors manually. | ||
func IsCacheMiss(err error) bool { | ||
return errors.Is(err, ErrNotFound) | ||
} | ||
|
||
// InfiniteTTL returns a time.Duration that represents an infinite TTL. | ||
func InfiniteTTL() time.Duration { | ||
return math.MaxInt64 | ||
} |
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,71 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
type item[T any] struct { | ||
value T | ||
expiresAt *time.Time | ||
} | ||
|
||
// timeGetter is a function that returns the current time. | ||
type timeGetter func() time.Time | ||
|
||
type InMemoryCache[T any] struct { | ||
cache sync.Map | ||
timeGetter timeGetter | ||
} | ||
|
||
// NewInMemoryCache creates a new in-memory cache. | ||
// The underlying cache implementation uses a sync.Map so it is thread-safe. | ||
func NewInMemoryCache[T any]() *InMemoryCache[T] { | ||
return &InMemoryCache[T]{ | ||
timeGetter: time.Now, | ||
} | ||
} | ||
|
||
func (c *InMemoryCache[T]) Get(ctx context.Context, key string) (T, error) { | ||
var defaultVal T | ||
rawItem, ok := c.cache.Load(key) | ||
if !ok { | ||
return defaultVal, ErrNotFound | ||
} | ||
i, ok := rawItem.(*item[T]) | ||
if !ok { | ||
return defaultVal, errors.New("unexpected item type found in cache") | ||
} | ||
|
||
if i.expiresAt != nil && i.expiresAt.Before(time.Now()) { | ||
c.cache.Delete(key) | ||
return defaultVal, ErrNotFound | ||
} | ||
|
||
return i.value, nil | ||
} | ||
|
||
func (c *InMemoryCache[T]) Set(ctx context.Context, key string, value T, ttl time.Duration) error { | ||
if ttl < 0 { | ||
return errors.New("ttl must be greater than 0") | ||
} | ||
if ttl == 0 { | ||
return nil | ||
} | ||
|
||
i := &item[T]{ | ||
value: value, | ||
} | ||
if ttl > 0 { | ||
expiresAt := c.timeGetter().Add(ttl) | ||
i.expiresAt = &expiresAt | ||
} | ||
c.cache.Store(key, i) | ||
|
||
return nil | ||
} | ||
|
||
var _ Cache[any] = &InMemoryCache[any]{} |
Oops, something went wrong.