Last active
November 25, 2015 10:28
-
-
Save soeffing/eb97960957168604fdd3 to your computer and use it in GitHub Desktop.
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
"use strict"; | |
function cache_store(callback, key, value) { | |
callback('store ' + value + ' to cache' ); | |
}; | |
function slow_provider_function(callback, key) { | |
const providerResult = 'provider result'; | |
setTimeout(function() { | |
console.log('slow result fetched'); | |
callback(providerResult); | |
}, Math.floor((Math.random()*1000)+1)); | |
}; | |
function cache_retrieve(callback, key) { | |
const cacheResult = 'cache result'; | |
setTimeout(function() { | |
console.log('fast result fetched'); | |
callback(cacheResult); | |
}, Math.floor((Math.random()*500)+1)); | |
} | |
function memoize(slow_function, key) { | |
let callback_called_once = false; | |
let execute_callback = function(result) { | |
// Check if callback was already called | |
if (callback_called_once === false) { | |
callback_called_once = true; | |
console.log(result); | |
} | |
} | |
return function() { | |
cache_retrieve(function(result) { | |
execute_callback(result); | |
}, key); | |
slow_function(function(result) { | |
execute_callback(result); | |
cache_store(console.log, key, 'slow provider value'); | |
}, key); | |
} | |
} | |
const faster_function = memoize(slow_provider_function, 'key'); | |
faster_function(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Answer for bonus question:
Short Answer: 74 seconds long should be the TTL of the cache to obtain 95% accuracy.
Explanation:
I assumed an exponential decay and used the following equations:
N0 is the initial quantity
N(t) is the quantity that has not decayed after a time t
T is the mean lifetime of the decaying quantity
Since you gave me the half-life of the cache I could calculate the mean lifetime with this equation:
The mean lifetime of a cache entry is around 1442.695 seconds
With that I had everything together for the equation above:
N0: 100 (cache entries)
N(t): 95 (since we want that 95 times out of 100 (N0) cache entries can be retrieved at any time)
T: 1442.695 seconds
Now I had to rearrange the equation to be able to calculate t which would be the time after which at least 95% of all cache entries would be still valid.
After rearranging the equation, I got this: t = - (ln 0.95 * 1442.695)
Which is 74 seconds