Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 837767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:11:44+00:00 2026-05-15T05:11:44+00:00

I was posting some comments in a related question about MVC caching and some

  • 0

I was posting some comments in a related question 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?

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.

(Source: Post by Jarrod)

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?

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?

Another poster said this:

I would suggest using the md5 hash of
your query combined with a serialized
version of your input arguments.

Is the minuscule chance of collision worth worrying about?

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.


Update for Bounty

I am indeed using an extremely lightweight ORM somewhat similar to ActiveRecord but is capable of doing complex joins and subqueries without the n^2 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.

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?

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?

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:11:46+00:00Added an answer on May 15, 2026 at 5:11 am

    Your post only makes any sense if the model is a trivial ORM. And there are lots of reasons why that’s a bad thing. Try thinking about the model as if it were a web service.

    Caching is the responsiblity of the model.

    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?

    But the inputs to the model uniquely define its output.

    If you’re using the same model to retrieve the contents of a shopping basket and to run a search on your product catalog then there’s something wrong with your code.

    Even in the case of the shopping basket, there may be merit in caching data with a TTL of less than the time taken to process a transaction which would change its contents, in the case of the catalog search, caching the list of matching products for a few hours will probably have no measurable impact on sales, but trade-off well in reducing database load.

    The fact that you are using a trivial ORM out of the box does not exclude you from wrapping it in your own code.

    Wouldn’t the Model have to be astronomically smart, and/or store statistics

    No. You make the determination on whether to cache, and if you can’t ensure that the cache is consistent then enforce a TTL based on the type of request.

    As a general rule of thumb, you should be able to predict appropriate TTLs based on the SELECT query before binding any variables and this needs to be implemented at design time – but obviously the results should be indexed based on the query after binding.

    Should I implement the caching individually in the Model classes that require it, or should it be part of the ORM layer?

    For preference I would implement this as a decorator on the model class – that way you can easily port it to models which implement a factory rather than trivial ORM.

    C.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Posting a stack overflow question on stackoverflow.com, how amusing :-) I'm running some recursive
After posting this question and reading that one I realized that it is very
I'm posting some strings in my Session with the call request.getSession().setAttribute(key, value); And making
I need to define a hash for posting some ajax data using jQuery. The
I'm porting some MATLAB functions to Scilab . The cool thing is that there
I am porting some queries from Access to T-SQL and those who wrote the
I'm porting some code from BSD sockets to Winsock, and I'm not sure how
I'm in the process of porting some ANSI C++ code to C#... and this
Recently, when porting some STL code to VS2008 I wanted to disable warnings generated
For posting AJAX forms in a form with many parameters, I am using a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.