Saturday, September 4, 2010
 

Power of versioning caching part 2

In previous article i talked about versions based caching. This time we can even go more and cache with http headers based on cache version. How to do it?

Everything is quiet simple. First thing i found was http://www.jonasjohn.de/snippets/php/caching.htm

Most important part is:

$ExpireTime = 12000;
header('Cache-Control: max-age=' . $ExpireTime); // must-revalidate
header('Expires: '.gmdate('D, d M Y H:i:s', time()+$ExpireTime).' GMT');
header('ETag: ' . $cacheKey); 

These four lines sets appropriate headers. Cache-Control: max-age, determines how long cache can be valid. Expires determines then cache expires. And muost important part is "ETag". So from where comes $cacheKey value. Some code example from my pervious article:
 

$cache = CSCacheAPC::getMem(); 

$cacheVersion = $cache->getCacheVersion('last_hits_version',time(),600);

if (($Result = $cache->restore(md5($cacheVersion.'_lasthits_view_url'.'_page_'.$Params['user_parameters_unordered']['page']))) === false)
{ 
//... Some logic


In generally $cacheKey is calculated memory hash key. This time we rewrite block code like this:
 

<?php

$cache = CSCacheAPC::getMem(); 
$cacheKey = md5('version_'.$cache->getCacheVersion('last_uploads').'lastuploads_view_url'.'_page_'.$Params['user_parameters_unordered']['page']);
    
$ExpireTime = 12000;
$currentKeyEtag = md5($cacheKey.'user_id_'.erLhcoreClassUser::instance()->getUserID());
header('Cache-Control: max-age=' . $ExpireTime); // must-revalidate
header('Expires: '.gmdate('D, d M Y H:i:s', time()+$ExpireTime).' GMT');
header('ETag: ' . $currentKeyEtag); 

$iftag = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? $_SERVER['HTTP_IF_NONE_MATCH'] == $currentKeyEtag : null;         
if ($iftag === true)
{   
    header ("HTTP/1.0 304 Not Modified");
    header ('Content-Length: 0');
    exit;
}

if (($Result = $cache->restore($cacheKey)) === false)
{         
    //.. Some buisiness logic    
    $cache->store($cacheKey,$Result);
}   

And now we have headers powered caching using same cache versions powered headers. This time bandwidth is equal 0!!!. And then content expires, after new image upload. Expires and ETag automatically. So this method have advantage over over solutions because, we can calculate page version without making any call to database or any hard calculations, just using cache versions. Is'nt that great?
 

Back »

Comments: 5

Leave a reply »

 
  • gambetta videos

    japan grope video electronegativity video intercepting video transmission absol video.

     
     
  • kristine diether video

    jimmy greaves vidoe alhaji vidoes disgust video dare junkies youtube.

     
     
  • bob fosse video

    lepton video taser demonstration video mynt utube ferocious music video.

     
     
  • epione video

    free fex vidoes eightsomes video insolence video indies video.

     
     
  • exceecild

    why not:)

     
     
  • Leave a Reply
    Your gravatar
    Your Name
     
     
     
     
     
 
About Remdex site

Simple site for simple peoples.

Get in touch

E-mail: remdex@gmail.com