Skip to content

Instantly share code, notes, and snippets.

@soeffing
Last active November 25, 2015 10:28
Show Gist options
  • Save soeffing/eb97960957168604fdd3 to your computer and use it in GitHub Desktop.
Save soeffing/eb97960957168604fdd3 to your computer and use it in GitHub Desktop.
"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();
@soeffing
Copy link
Author

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:

screen shot 2015-10-29 at 18 15 15

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:

screen shot 2015-10-29 at 18 20 33

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment