SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    996
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)

    MVC and caching best practices?

    What's the best way to achieve this?


    My current process is:

    -Controller performs query (mostly via models)
    -Controller initiates view
    -Controller passes set of models to the view
    -Controller displays view


    What I want to do, is cache view and just output it without re-doing the queries.


    What's the best technique for this?

    The simplest one would be something like:


    PHP Code:
    if (file_exists($cachefile)) { 
        
    //display cache
    }
    else {
        
    //do above process and save output to file

    (obviously need a flag to reset the cache when something changes)


    But it somewhat doesn't feel right giving the responsibility of caching to the controller.

    Should the caching be done in the view? If so, the query needs to be run in the view, which definately isnt right.


    I guess my question is, is the controller the 'correct' place to deal with the caching.


    *obviously in the real world, a cache object would deal with the file IO but i wanted to keep my example simple

  2. #2
    SitePoint Zealot Amenthes's Avatar
    Join Date
    Oct 2006
    Location
    Bucharest, Romania
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would even put caching at the front controller or router layer, so that it maps a certain
    route to a static file if possible. But the controller has to save the actual cached file.

    You're probably confused because you want to cache the view output, which means
    caching both DB data as well as template markup. Having two layers involved, I believe
    the controller should coordinate the whole caching mechanism with possible front controller
    short circuiting, as I said above.

    What I'd do though is to have a cache Decorator for the model, which would expose the
    same interface as the model, just that it is aware of caching so it may serve contents
    from cache. The controller would use the model just as before, what it controls is what
    object is actually instantiated, the real model or the caching model.

    I'd also make the View part cache-aware, but only if you use layouts and parts of the page
    may be cached while others not. Although I'm not really sure caching view parts help that
    much when using PHP as a template language. On the other hand bypassing some MVC
    logic by serving whole pages from cache in the routing layer may be nice.
    I'm under construction | http://igstan.ro/

  3. #3
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    996
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    Thanks, some useful points

    The problem with putting it in the model is, in one case I query the database directly. It's a one off query for generating the menu on a client's site. It gets all the nested product categoires and the number of products in each category. Then does some display logic.

    The only slow part really is the query (~2 seconds, but for every page this is a huge overhead considering the rest of all the pages load in < 0.5s) but I may as well cache the final display rather than the query result to save that bit extra.

    There is no model for this query because it is just a one off and contains aggregates.

    I'm not really bothered about caching whole pages at the moment (and especially for this site) as they pull in dynamic items (5 random related products, etc) so it can't just be a whole cached page really.

    I think I'll just keep it in the controller for now unless I need something more widespread.

  4. #4
    SitePoint Addict Mastodont's Avatar
    Join Date
    Mar 2007
    Location
    Czech Republic
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Should the caching be done in the view?
    It depends on what do you want to cache.
    - db query -> cache in model
    - part of page -> cache in view or model
    - whole page -> cache in view

  5. #5
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TomB View Post
    Thanks, some useful points

    The problem with putting it in the model is, in one case I query the database directly. It's a one off query for generating the menu on a client's site. It gets all the nested product categoires and the number of products in each category. Then does some display logic.

    The only slow part really is the query (~2 seconds, but for every page this is a huge overhead considering the rest of all the pages load in < 0.5s) but I may as well cache the final display rather than the query result to save that bit extra.

    There is no model for this query because it is just a one off and contains aggregates.

    I'm not really bothered about caching whole pages at the moment (and especially for this site) as they pull in dynamic items (5 random related products, etc) so it can't just be a whole cached page really.

    I think I'll just keep it in the controller for now unless I need something more widespread.
    Caching is one of those things that has broad applications within MVC. You can use it within the model for your ACL or database query returns. You can use it within the controller for to cache routes, config files. You can also use it in your view to cache segments of html or entire pages. Where your caching is sort of important though. Caching view renders or routes in your model is sort of awkward because the model isn't concerned with html or routes. The model is only concerned with data. I would give each section its own caching capabilities.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •