Caching

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Caching

Post by alex.barylski »

Most of us have used Smarty, maybe even it's caching facilities to some limited success.

How do you go about caching only *parts* of a webpage? I've heard this is possible, but admittedly haven't put much time into thought on how this might be done. I've always seen Smarty as either a all or nothing caching solution.

Meaning, you cache the entire page (when feasable) or not at all, in the case of dynamic web applications. For instance, you would never cache a FORM because it's field data is changed constantly.

AJAX is a solution to this problem as it allows you to possibly cache a FORM and fill it's dynamic contents using AJAX...but I'm curious, what kind of caching techniques have you employed using Smarty or not (just in general) at the PHP level (Squid, etc not needed).

I once considered implementing an event based cache system where by everything was cached as a static page, only when that pages associated datasources changed, did the template get re-generated. This system made awesome sense in theory but it turned out to be more complicated to implement than I thought.

Thoughts? :)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Just thought I'd share:

http://msdn2.microsoft.com/en-us/librar ... nter).aspx

An interesting read although it doesn't transcend into PHP 100% still interesting... :)

Hmmmmmm....flexible, powerful and efficient caching systems *drools*
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

How do you go about caching only *parts* of a webpage?
The trick is to find a fast method of retrieving these static components and getting them on to the dynamic web page. If they're super-static, nothing needs to happen as they're already on the template. If they're static but come from a "dynamic" datasource, you need to capture the output, write it to a fast cache, and perform retrieval accordingly.
For instance, you would never cache a FORM because it's field data is changed constantly.
Not necessarily. Suppose I was coding a "New Product" form. If I play my cards right, any volatile data can be associated to the user's session, eliminating the need to change the form at all.

Of course, if you're editing a product description, you would not want to cache the form, since it would be changed soon!

It seems to me that you're having an unusually tough time in this area, especially because of your suggestion of using AJAX (probably a bad idea). Could you elaborate?
I once considered implementing an event based cache system where by everything was cached as a static page, only when that pages associated datasources changed, did the template get re-generated. This system made awesome sense in theory but it turned out to be more complicated to implement than I thought.
I have implemented this. See XHTML Compiler.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

View Helpers ring a bell?

A lot of these issues are easily solved by breaking down templates, and recognising a View is just an "assembly" stage. During assembly you have a conveyer belt (the templating engine) and numerous factories (View Helpers) or just simple manual work (View data in the "main" template). Since a helper can be any generic object registered to the main View, there's no limit to what presentational work or logic they can perform.

The only hard bit is knowing when a View object is starting to leak into other areas (like querying a Model). At this point having a controller plugin architecture could help since controllers are generally all geared up to access Models.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Input is always appreciated, but I was kind of hoping for more a technical disscussion than theory or architectural topics, such as views and their helpers.

What I mean is, when caching in Smarty, why are the cache keys MD5'd (if memory serves correct). Wouldn't it make more sense to use something like a Windows registry key, where 'cache' is the base directory and keys are folders and files are values...?

Perhaps it's the overhead in creating folders to reflect cache, in which case why not just link cache "states" togather in a chain using a single long filename.

What metrics (right word) do you use to determine if a request (or part of it) are cachable?

For instance, $_GET['page'] might be the sole value which you rely on to set or retreive a cache, using it's value as your cache key...

Make sense? Any ideas on this matter?

Greater the detail the better :)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

why are the cache keys MD5'd
I would guess that this enables a cache key to be an arbitrary string of arbitrary length, and not just a valid filename. Certainly would make selectively clearing the cache difficult.
What metrics (right word) do you use to determine if a request (or part of it) are cachable?
Assuming that metrics = heuristics and request = output, I would say you're out of luck. As a developer, you need to determine for each of your types of pages how long the data lasts, whether or not it's worth caching, and how to determine when the cache is invalid.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Generally when you want to go down the technical discussion, you want to provide a base where you have begun. Provide some code, and some of your implementation ideas and we can get started from there.

I'm not too sure what your talking about when you say using $_GET to determine is a request is cachable. The request should have no idea if the response was cached.

Typically in the past when I wanted to cache only specific parts of the response I would use view helpers or tags to notify the view to not include specific parts of the template.

For instance,

Code: Select all


<html>
   <body>
      Hi I'm Jcart and I'm the best!

     <nocache>
        This area will not be cached, heres something dynamic <?php echo $this->dynamic; ?>
     </nocache>
   </body>
</html>
and using MVC architecture, your controller would check to see if the request has expired or whatever.

Code: Select all

<?php

class IndexController extends Northern_Controller
{ 
   public function IndexAction()
   {
      if (!$this->_view->isCached() or $this->_cacheExpired())
      {
         // fetch dynamic results
   
         $this->_view->set('dynamic', 'this is some cool dynamic testing');
      }
      
      $this->getResponse()->appendBody($this->_view->render());
   }

   private function _cacheExpired()
   {
       //for event based caching.. 
   }
}
I'm not so sure you'll get specific implementations of a caching system without forking something up yourself first.
Last edited by John Cartwright on Sat Mar 24, 2007 5:54 pm, edited 1 time in total.
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

The trick is to find a fast method of retrieving these static components and getting them on to the dynamic web page.
I'm trying just the opposite. HTML beats PHP for retrieving static content. I'm using Ajax to inject the dynamic components into plain old HTML files.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Buddha443556 wrote:
The trick is to find a fast method of retrieving these static components and getting them on to the dynamic web page.
I'm trying just the opposite. HTML beats PHP for retrieving static content. I'm using Ajax to inject the dynamic components into plain old HTML files.
and thus relying on javascript.. boo hiss! :wink:
wei
Forum Contributor
Posts: 140
Joined: Wed Jul 12, 2006 12:18 am

Post by wei »

from the user stand point (i.e. the developer using a cache mechanism), adding things and retrieving things from the cache are the easy parts, invalidating the cache is the hardest task. The invalidation can be as simple as a expiration timeout or complex business logic. The task of invalidation could be to flush the cache or delete a set of cached data.


from the cache storage point of view, distribution and retrieval from multiple cache storage could be very tricky or dead simple. Cache should be design as a simple high performance and volatile storage, that is, no guarantee are made on the availability or durability of the data cached. System such as memcache (a simple memory based cache using sockets for communication) is very effective and simple to use.

In addition, there are caching strategies such as First-In-First-Out bases, or Least-Recently-Used, and others
http://faq.javaranch.com/view?CachingStrategies

is this what are you after or something else?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

IMO, caching is a multi-tier question: There's caching data, and caching output.

If your template is doing any more than just "including" chunks of content/markup that can be cached as solid files, then you should be caching at a lower level (i.e. data model).

I used a write-through cache in my data model once - it invalidated the cache when writes were made, and generated the cache if it was either old or missing on a read request. Worked like a charm.
User avatar
serfczar_
Forum Commoner
Posts: 34
Joined: Sun Feb 25, 2007 5:27 pm
Location: USA, Texas
Contact:

Post by serfczar_ »

I was wondering if a cache system would be right for me. The only true dynamic thing on the page will probably be the

Code: Select all

<div></div>
that will contain the pages to be navigated to, dynamically reloaded to be the appropriate page with AJAX to eliminate the use of frames.

Example, div contains page1, i click link for page2, page2 shows up in the div that had page1 in it.

Would a cache system really save me that much bandwidth/processor usage?
Post Reply