-
Notifications
You must be signed in to change notification settings - Fork 45.6k
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
[simple-cache-provider] Use LRU cache eviction #12851
Merged
Merged
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.
Jump to
Jump to file
Failed to load files.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -189,4 +189,41 @@ describe('SimpleCacheProvider', () => { | |
fn(); | ||
} | ||
}); | ||
|
||
it('stays within maximum capacity by evicting the least recently used record', async () => { | ||
const {createCache, createResource} = SimpleCacheProvider; | ||
|
||
function loadIntegerString(int) { | ||
return Promise.resolve(int + ''); | ||
} | ||
const IntegerStringResource = createResource(loadIntegerString); | ||
const cache = createCache(); | ||
|
||
// TODO: This is hard-coded to a maximum size of 500. Make this configurable | ||
// per resource. | ||
for (let n = 1; n <= 500; n++) { | ||
IntegerStringResource.preload(cache, n); | ||
} | ||
|
||
// Access 1, 2, and 3 again. The least recently used integer is now 4. | ||
IntegerStringResource.preload(cache, 3); | ||
IntegerStringResource.preload(cache, 2); | ||
IntegerStringResource.preload(cache, 1); | ||
|
||
// Evict older integers from the cache by adding new ones. | ||
IntegerStringResource.preload(cache, 501); | ||
IntegerStringResource.preload(cache, 502); | ||
IntegerStringResource.preload(cache, 503); | ||
|
||
await Promise.resolve(); | ||
|
||
// 1, 2, and 3 should be in the cache. 4, 5, and 6 should have been evicted. | ||
expect(IntegerStringResource.read(cache, 1)).toEqual('1'); | ||
expect(IntegerStringResource.read(cache, 2)).toEqual('2'); | ||
expect(IntegerStringResource.read(cache, 3)).toEqual('3'); | ||
|
||
expect(() => IntegerStringResource.read(cache, 4)).toThrow(Promise); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just one thing - I think even if 1, 2, and 3 are also evicted I think this will still pass. We could verify that those don't throw Promises? |
||
expect(() => IntegerStringResource.read(cache, 5)).toThrow(Promise); | ||
expect(() => IntegerStringResource.read(cache, 6)).toThrow(Promise); | ||
}); | ||
}); |
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.
Rather than keeping and updating the 'size' property here, could we rely on the
size
property of themap
attribute of the cache?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.
Going to land this PR, we can fix these in a follow-up.