-
-
Save banderson623/6ac34f08f58a99b6f0be to your computer and use it in GitHub Desktop.
// A place to store results | |
var memory = {}; | |
var primesUpTo = function(maxNumber){ | |
// set up something that is very unique to this request | |
var cacheKey = 'primes:max:' + maxNumber; | |
// Get the result from memory | |
var result = memory[cacheKey]; | |
// check if have a stored version of this in memory | |
// if the result was `undefined` we have yet to request it. | |
var isPresent = typeof(result) !== "undefined"; | |
if (!isPresent){ | |
// We have not saved the results yet... this is a "Cache Miss" | |
console.log("cache miss on " + maxNumber); | |
// here we are making our "expensive call" (aka something that takes a lot of time) | |
result = _getPrimes(maxNumber); | |
// Now we want to store this with our cache key | |
memory[cacheKey] = result; | |
} else { | |
console.log("Cache Hit " + maxNumber); | |
} | |
// This was either set during the cache HIT (line 10) or in the cache MISS (line 21) | |
return result; | |
}; | |
// STOP READING HERE ................ | |
// this is just a function that takes a while to return the result | |
// calculating primes | |
var _getPrimes = function(max) { | |
var sieve = [], i, j, primes = [];for (i = 2; i <= max; ++i) {if (!sieve[i]) {primes.push(i);for (j = i << 1; j <= max; j += i) {sieve[j] = true;}}}return primes; | |
}; | |
From @ckolderup
any time there's a
cache miss
the program has to basically go find out what the real value is, and then usually the cache will take that and store it for next time
cache hit
just means "the cache had what I wanted" andcache miss
means "the cache didn't have what I wanted" since you generally are talking in terms of data sets that are impossible to fully pre-calculate
More complex caching infrastructures:
then the cache usually just has some set of rules that decides how much data it's going to store and for how long. Sometimes it'll remove things from the cache based on how long it's been since they've been requested or they'll only store some preset # of things and delete the least-recently-requested ones in order to make room for a new one
You can copy and paste this into a javascript terminal and see how it works: