I had been posting some comments inside a related question about MVC caching plus some questions regarding actual implementation emerged. How do you implement one-level cache that actually works transparently with no developer requiring to by hand cache, but still remains efficient?
I'd keep my caching duties firmly inside the model. It's no controller's or view's business in which the model is getting data. All they are concerned about is that after information is asked for, information is provided - this is the way the MVC paradigm should really work.
(Source: Publish by Jarrod)
The main reason I'm skeptical happens because caching should not often be achieved unless of course there's a genuine need, and should not be accomplished for such things as search engine results. So in some way the Model itself needs to know set up Choose statement being released into it is worth being cached. Wouldn't the Model need to be astronomically wise, and/or store statistics of what's being most frequently queried on the lengthy time period to be able to precisely come to a decision? And wouldn't the overhead of this result in the caching useless anyway?
How does one distinctively identify a question from another query (or even more precisely, an effect set from another result set)? How about if you are using prepared claims, with just the parameters altering based on user input?
Another poster stated this:
I recommend while using md5 hash of your query coupled with a serialized version of the input arguments.
May be the minuscule possibility of collision worth worrying about?
Conceptually, caching within the Model appears like smart to me, however it appears in functionality and because of the character of caching the developer must have direct treatments for it and explicity code it in to the controller logic.
Update for Bounty
I'm indeed utilizing an very lightweight ORM somewhat much like ActiveRecord but is able to do complex joins and subqueries with no
n^2 problem. I built it myself, so it's flexible and is not limited when it comes to relations or column names, and Among the finest to know the way i should implement the caching mechanism.
Following a advice from the useful people, I'd have a hash (most likely md5) from the query concatenated with a listing of their parameters, and employ this because the key for your particular data store. Must I implement the caching individually within the Model classes that need it, or if it is area of the ORM layer?
How do you know when it ought to be invalidated? Would I must parse the UPDATE/Remove/Place queries and sub in parameters by hand to discover which records are now being modified? Or worse, do additional queries whenever information is modified to keep an eye on which everything has transformed and what ought to be invalidated?
I'll award the bounty to whomever can provide us a obvious conceptual explanation (whether it is actually necessary/efficient to become done transparently), and when so, has some implementation particulars for that Model caching. I'm using PHP and MySQL in the event that helps you to narrow your focus.