-
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
12 changed files
with
393 additions
and
49 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,30 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
var ( | ||
ErrStaleCache = errors.New("item's ttl has expired") | ||
ErrNotFound = errors.New("item not found") | ||
) | ||
|
||
type Option func(*Cache) | ||
|
||
type Cache interface { | ||
// Get retrieves the cached value for the given key. | ||
// If the key is not found, it returns ErrNotFound or ErrStaleCache if the item's ttl has expired. | ||
Get(ctx context.Context, key string) (any, error) | ||
// Set stores the value in the cache with the given key. | ||
// If ttl is 0, the item will be cached indefinitely. | ||
Set(ctx context.Context, key string, value any, 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) || errors.Is(err, ErrStaleCache) | ||
} |
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,64 @@ | ||
package cache | ||
|
||
import ( | ||
"context" | ||
"github.com/pkg/errors" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type item struct { | ||
value any | ||
expiresAt *time.Time | ||
} | ||
|
||
// timeGetter is a function that returns the current time. | ||
type timeGetter func() time.Time | ||
|
||
type InMemoryCache 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() *InMemoryCache { | ||
return &InMemoryCache{ | ||
timeGetter: time.Now, | ||
} | ||
} | ||
|
||
func (c *InMemoryCache) Get(ctx context.Context, key string) (any, error) { | ||
rawItem, ok := c.cache.Load(key) | ||
if !ok { | ||
return nil, ErrNotFound | ||
} | ||
i, ok := rawItem.(*item) | ||
if !ok { | ||
return nil, errors.New("unexpected item type found in cache") | ||
} | ||
|
||
if i.expiresAt != nil && i.expiresAt.Before(time.Now()) { | ||
c.cache.Delete(key) | ||
return nil, ErrStaleCache | ||
} | ||
|
||
return i.value, nil | ||
} | ||
|
||
func (c *InMemoryCache) Set(ctx context.Context, key string, value any, ttl time.Duration) error { | ||
if ttl < 0 { | ||
return errors.New("ttl must be greater than 0") | ||
} | ||
|
||
i := &item{ | ||
value: value, | ||
} | ||
if ttl > 0 { | ||
expiresAt := c.timeGetter().Add(ttl) | ||
i.expiresAt = &expiresAt | ||
} | ||
c.cache.Store(key, i) | ||
|
||
return nil | ||
} |
Oops, something went wrong.