title: Cache Invalidation — Is there a General Solution?
body: <blockquote>
    <p>"There are only two hard problems in Computer Science: cache invalidation and naming things."</p>
  </blockquote>
  
  <p><em>Phil Karlton</em></p>
  
  <p>Is there a general solution or method to invalidating a cache; to know when an entry is stale, so you are guaranteed to always get fresh data?</p>
  
  <p>For example, consider a function <code>getData()</code> that gets data from a file.
  It caches it based on the last modified time of the file, which it checks every time it's called.<br>
  Then you add a second function <code>transformData()</code> which transforms the data, and caches its result for next time the function is called.  It has no knowledge of the file - how do you add the dependency that if the file is changed, this cache becomes invalid?</p>
  
  <p>You could call <code>getData()</code> every time <code>transformData()</code> is called and compare it with the value that was used to build the cache, but that could end up being very costly.</p>