Skip to content

Instantly share code, notes, and snippets.

@banderson623
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banderson623/6ac34f08f58a99b6f0be to your computer and use it in GitHub Desktop.
Save banderson623/6ac34f08f58a99b6f0be to your computer and use it in GitHub Desktop.
Simple example of how caching works
// 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;
};
@banderson623
Copy link
Author

You can copy and paste this into a javascript terminal and see how it works:

primesUpTo(100)
// => VM305:33 cache miss on 100
// => [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

@banderson623
Copy link
Author

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" and cache 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

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