-
Notifications
You must be signed in to change notification settings - Fork 0
Loader2 #3
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
Open
aryehlev
wants to merge
2
commits into
main
Choose a base branch
from
loader2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Loader2 #3
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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,50 @@ | ||
| package cache_go | ||
|
|
||
| // LoaderFunc is a function type that loads a value for a key. | ||
| // It returns the loaded value and a boolean indicating whether the value was found. | ||
| type LoaderFunc[K comparable, V any] func(key K) (V, bool) | ||
|
|
||
| // CacheWithLoader extends the Cache with a loader function that can fetch values from a backend. | ||
| type CacheWithLoader[K comparable, V any] struct { | ||
| *Cache[K, V] | ||
| loader LoaderFunc[K, V] | ||
| } | ||
|
|
||
| // NewWithLoader creates a new cache with the specified size and loader function. | ||
| func NewWithLoader[K comparable, V any](size uint, loader LoaderFunc[K, V]) (*CacheWithLoader[K, V], error) { | ||
| cache, err := New[K, V](size) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &CacheWithLoader[K, V]{ | ||
| Cache: cache, | ||
| loader: loader, | ||
| }, nil | ||
| } | ||
|
|
||
| // Get retrieves a value from the cache. If the value is not in the cache, | ||
| // it attempts to load it using the loader function. | ||
| func (cl *CacheWithLoader[K, V]) Get(key K) (V, bool) { | ||
|
|
||
| // If not in cache, try to load from backend | ||
| if cl.loader != nil { | ||
| value, found := cl.loader(key) | ||
| if found { | ||
| // Store the loaded value in the cache | ||
| cl.Cache.Set(key, value) | ||
| return value, true | ||
| } | ||
| } | ||
|
|
||
| // Try to get from cache first | ||
| value, found := cl.Cache.Get(key) | ||
| if found { | ||
| return value, true | ||
| } | ||
|
|
||
|
|
||
| // Return zero value if not found | ||
| var zero V | ||
| return zero, false | ||
| } | ||
This file contains hidden or 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,68 @@ | ||
| package cache_go | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestCacheWithLoader(t *testing.T) { | ||
| // Create a simple in-memory backend | ||
| backend := map[string]int{ | ||
| "key1": 42, | ||
| "key2": 84, | ||
| } | ||
|
|
||
| // Create a loader function that fetches from the backend | ||
| loader := func(key string) (int, bool) { | ||
| value, found := backend[key] | ||
| return value, found | ||
| } | ||
|
|
||
| // Create a cache with the loader | ||
| cache, err := NewWithLoader[string, int](100, loader) | ||
| if err != nil { | ||
| t.Fatalf("Failed to create cache: %v", err) | ||
| } | ||
|
|
||
| // Test getting a value that's not in the cache but is in the backend | ||
| value, found := cache.Get("key1") | ||
| if !found { | ||
| t.Errorf("Expected to find key1 via loader, but it wasn't found") | ||
| } | ||
| if value != 42 { | ||
| t.Errorf("Expected value 42 for key1, got %d", value) | ||
| } | ||
|
|
||
| // Test that the value is now cached | ||
| // We can verify this by checking if the value is still accessible | ||
| // even if we remove it from the backend | ||
| delete(backend, "key1") | ||
| value, found = cache.Get("key1") | ||
| if !found { | ||
| t.Errorf("Expected to find key1 in cache after loading, but it wasn't found") | ||
| } | ||
| if value != 42 { | ||
| t.Errorf("Expected value 42 for key1 from cache, got %d", value) | ||
| } | ||
|
|
||
| // Test getting a value that's not in the cache or backend | ||
| value, found = cache.Get("nonexistent") | ||
| if found { | ||
| t.Errorf("Expected not to find nonexistent key, but it was found with value %d", value) | ||
| } | ||
|
|
||
| // Test that the loader is not used when a value is already in the cache | ||
| // We'll set a value directly in the cache | ||
| cache.Set("key3", 123) | ||
|
|
||
| // Then add a different value to the backend | ||
| backend["key3"] = 456 | ||
|
|
||
| // When we get the value, it should come from the cache, not the backend | ||
| value, found = cache.Get("key3") | ||
| if !found { | ||
| t.Errorf("Expected to find key3 in cache, but it wasn't found") | ||
| } | ||
| if value != 123 { | ||
| t.Errorf("Expected value 123 for key3 from cache, got %d (should not have used loader)", value) | ||
| } | ||
| } |
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.
Critical issue: Incorrect order of operations in Get method.
The current implementation checks the loader before checking the cache, which defeats the purpose of caching and will cause performance issues. The cache should be checked first, and the loader should only be called on cache misses.
Apply this diff to fix the logic:
func (cl *CacheWithLoader[K, V]) Get(key K) (V, bool) { + // Try to get from cache first + value, found := cl.Cache.Get(key) + if found { + return value, true + } // If not in cache, try to load from backend if cl.loader != nil { value, found := cl.loader(key) if found { // Store the loaded value in the cache cl.Cache.Set(key, value) return value, true } } - // Try to get from cache first - value, found := cl.Cache.Get(key) - if found { - return value, true - } - - // Return zero value if not found var zero V return zero, false }📝 Committable suggestion
🤖 Prompt for AI Agents