-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
Cache currentTime and buffered from Flash #3705
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* @file flash-cache.js | ||
* | ||
* Auto-caching method wrapper to avoid calling through to Flash too | ||
* often. | ||
*/ | ||
|
||
/** | ||
* Returns a new getter function that returns a cached value if | ||
* invoked multiple times within the specified duration. | ||
* | ||
* @param {Function} getter the function to be cached | ||
* @param {Number} cacheDuration the number of milliseconds to cache | ||
* results for | ||
* @return {Function} a new function that returns cached results if | ||
* invoked multiple times within the cache duration | ||
*/ | ||
export default function timeExpiringCache(getter, cacheDuration) { | ||
const result = function cachedGetter() { | ||
const now = new Date().getTime(); | ||
|
||
if (now - result.lastCheckTime_ >= cacheDuration) { | ||
result.lastCheckTime_ = now; | ||
result.cache_ = getter(); | ||
} | ||
return result.cache_; | ||
}; | ||
|
||
result.lastCheckTime_ = -Infinity; | ||
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. When you want to be absolutely certain! 😆 |
||
return result; | ||
} |
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,50 @@ | ||
/* eslint-env qunit */ | ||
import timeExpiringCache from '../../../src/js/tech/flash-cache.js'; | ||
import sinon from 'sinon'; | ||
|
||
QUnit.module('Flash Cache', { | ||
beforeEach() { | ||
this.clock = sinon.useFakeTimers(); | ||
}, | ||
afterEach() { | ||
this.clock.restore(); | ||
} | ||
}); | ||
|
||
QUnit.test('calls the getter on first invocation', function(assert) { | ||
let calls = 0; | ||
const cached = timeExpiringCache(() => calls++, 100); | ||
|
||
QUnit.equal(cached(), 0, 'returned a value'); | ||
QUnit.equal(calls, 1, 'called the getter'); | ||
}); | ||
|
||
QUnit.test('caches results', function(assert) { | ||
let calls = 0; | ||
const cached = timeExpiringCache(() => calls++, 100); | ||
|
||
for (let i = 0; i < 31; i++) { | ||
QUnit.equal(cached(), 0, 'returned a cached value'); | ||
} | ||
QUnit.equal(calls, 1, 'only called getter once'); | ||
}); | ||
|
||
QUnit.test('refreshes the cache after the result expires', function(assert) { | ||
let calls = 0; | ||
const cached = timeExpiringCache(() => calls++, 1); | ||
|
||
QUnit.equal(cached(), 0, 'returned a value'); | ||
QUnit.equal(cached(), 0, 'returned a cached value'); | ||
QUnit.equal(calls, 1, 'only called getter once'); | ||
this.clock.tick(1); | ||
|
||
QUnit.equal(cached(), 1, 'returned a value'); | ||
QUnit.equal(cached(), 1, 'returned a cached value'); | ||
QUnit.equal(calls, 2, 'called getter again'); | ||
|
||
this.clock.tick(10); | ||
QUnit.equal(calls, 2, 'only refreshes on-demand'); | ||
QUnit.equal(cached(), 2, 'returned a value'); | ||
QUnit.equal(cached(), 2, 'returned a cached value'); | ||
QUnit.equal(calls, 3, 'called getter again'); | ||
}); |
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
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.
Since both uses of this function pass
100
here, I wonder if having a default (i.e.cacheDuration = 100
) makes sense?