-
Notifications
You must be signed in to change notification settings - Fork 184
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
feat(repository): cache engine-relevant methods #270
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1f9e834
feat(repository): cache engine-relevant methods
steebchen d5b231a
make cache time configurable, disable in tests
steebchen 14927e5
fix
steebchen 6946884
improvements
steebchen a0ad99f
switch lib, add extra test
steebchen 57fee54
add cache package :/
steebchen fa3166e
add stop
steebchen 0c68eb9
feat(repository): use generic cacheable function (#271)
steebchen ebd9fdc
Merge branch 'main' into feat/repository-cache
abelanger5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package cache | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
// item represents a cache item with a value and an expiration time. | ||
type item[V any] struct { | ||
value V | ||
expiry time.Time | ||
} | ||
|
||
// isExpired checks if the cache item has expired. | ||
func (i item[V]) isExpired() bool { | ||
return time.Now().After(i.expiry) | ||
} | ||
|
||
// TTLCache is a generic cache implementation with support for time-to-live | ||
// (TTL) expiration. | ||
type TTLCache[K comparable, V any] struct { | ||
items map[K]item[V] // The map storing cache items. | ||
mu sync.Mutex // Mutex for controlling concurrent access to the cache. | ||
stop chan interface{} // Channel to stop the goroutine that removes expired items. | ||
} | ||
|
||
// NewTTL creates a new TTLCache instance and starts a goroutine to periodically | ||
// remove expired items every 5 seconds. | ||
func NewTTL[K comparable, V any]() *TTLCache[K, V] { | ||
c := &TTLCache[K, V]{ | ||
items: make(map[K]item[V]), | ||
stop: make(chan interface{}), | ||
} | ||
|
||
go func() { | ||
// Create a new ticker to remove expired items every 5 seconds. | ||
ticker := time.NewTicker(5 * time.Second) | ||
defer ticker.Stop() | ||
|
||
for { | ||
select { | ||
case <-c.stop: | ||
return | ||
case <-ticker.C: | ||
c.mu.Lock() | ||
|
||
// Iterate over the cache items and delete expired ones. | ||
for key, item := range c.items { | ||
if item.isExpired() { | ||
delete(c.items, key) | ||
} | ||
} | ||
|
||
c.mu.Unlock() | ||
} | ||
} | ||
}() | ||
|
||
return c | ||
} | ||
|
||
func (c *TTLCache[K, V]) Stop() { | ||
close(c.stop) | ||
} | ||
|
||
// Set adds a new item to the cache with the specified key, value, and | ||
// time-to-live (TTL). | ||
func (c *TTLCache[K, V]) Set(key K, value V, ttl time.Duration) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
c.items[key] = item[V]{ | ||
value: value, | ||
expiry: time.Now().Add(ttl), | ||
} | ||
} | ||
|
||
// Get retrieves the value associated with the given key from the cache. | ||
func (c *TTLCache[K, V]) Get(key K) (V, bool) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
item, found := c.items[key] | ||
if !found { | ||
// If the key is not found, return the zero value for V and false. | ||
return item.value, false | ||
} | ||
|
||
if item.isExpired() { | ||
// If the item has expired, remove it from the cache and return the | ||
// value and false. | ||
delete(c.items, key) | ||
return item.value, false | ||
} | ||
|
||
// Otherwise return the value and true. | ||
return item.value, true | ||
} | ||
|
||
// Remove removes the item with the specified key from the cache. | ||
func (c *TTLCache[K, V]) Remove(key K) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
// Delete the item with the given key from the cache. | ||
delete(c.items, key) | ||
} | ||
|
||
// Pop removes and returns the item with the specified key from the cache. | ||
func (c *TTLCache[K, V]) Pop(key K) (V, bool) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
item, found := c.items[key] | ||
if !found { | ||
// If the key is not found, return the zero value for V and false. | ||
return item.value, false | ||
} | ||
|
||
// If the key is found, delete the item from the cache. | ||
delete(c.items, key) | ||
|
||
if item.isExpired() { | ||
// If the item has expired, return the value and false. | ||
return item.value, false | ||
} | ||
|
||
// Otherwise return the value and true. | ||
return item.value, true | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to add a benchmark test to see how many Gets/second we can handle. Don't imagine this will be a bottleneck anytime soon but would still be good to have a sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right, tracked via #282