title: Implementing Model-level caching
body: <p>I was posting some comments in a <a href="https://stackoverflow.com/questions/2915395/cache-layer-for-mvc-model-or-controller">related question</a> about MVC caching and some questions about actual implementation came up. How does one implement a Model-level cache that works transparently without the developer needing to manually cache, yet still remains efficient?</p>
  
  <blockquote>
    <p>I would keep my caching
    responsibilities firmly within the
    model. It is none of the controller's
    or view's business where the model is
    getting data. All they care about is
    that when data is requested, data is
    provided - this is how the MVC
    paradigm is supposed to work.</p>
  </blockquote>
  
  <p>(Source: <a href="https://stackoverflow.com/questions/2915395/cache-layer-for-mvc-model-or-controller/2915469#2915469">Post by Jarrod</a>)</p>
  
  <p>The reason I am skeptical is because caching should usually not be done unless there is a real need, and shouldn't be done for things like search results. So somehow the Model itself has to know whether or not the SELECT statement being issued to it is worthy of being cached. Wouldn't the Model have to be astronomically smart, and/or store statistics of what is being most often queried over a long period of time in order to accurately make a decision? And wouldn't the overhead of all this make the caching useless anyway?</p>
  
  <p>How would you uniquely identify a query from another query (or more accurately, a result set from another result set)? What about if you're using prepared statements, with only the parameters changing according to user input?</p>
  
  <p>Another poster said this:</p>
  
  <blockquote>
    <p>I would suggest using the md5 hash of
    your query combined with a serialized
    version of your input arguments.</p>
  </blockquote>
  
  <p>Is the minuscule chance of collision worth worrying about?</p>
  
  <p>Conceptually, caching in the Model seems like a good idea to me, but it seems in practicality and due to the nature of caching the developer should have direct control over it and explicity code it into the controller logic.</p>
  
  <hr>
  
  <p><strong>Update for Bounty</strong></p>
  
  <p>I am indeed using an extremely lightweight ORM somewhat similar to ActiveRecord but is capable of doing complex joins and subqueries without the <code>n^2</code> problem. I built it myself, so it is flexible and isn't restrictive in terms of relations or column names, and I just want to understand how I should implement the caching mechanism.</p>
  
  <p>Following the advice of the helpful people, I would take a hash (probably md5) of the query concatenated with a list of its parameters, and use this as the key for that particular data store. Should I implement the caching individually in the Model classes that require it, or should it be part of the ORM layer?</p>
  
  <p>How do I know when it should be invalidated? Would I have to parse the UPDATE/DELETE/INSERT queries and sub in parameters manually to find out which records are being modified? Or worse, do additional queries whenever data is modified to keep track of which things have changed and what should be invalidated?</p>
  
  <p>I will award the bounty to whoever can give me a clear conceptual explanation (whether or not this is really necessary/efficient to be done transparently), and if so, has some implementation details for the Model caching. I am using PHP and MySQL if that helps to narrow your focus.</p>