Error: Could not fetch http://thedevnet.com/wp-content/plugins/gd-star-rating/css/gdsr.css.php?t=0&s=a10i10m20k20c05r05%23121620243046%23121620243240%23s1pchristmas%23s1pcrystal%23s1pdarkness%23s1poxygen%23s1goxygen_gif%23s1pplain%23s1ppumpkin%23s1psoft%23s1pstarrating%23s1pstarscape%23t1pclassical%23t1pstarrating%23t1gstarrating_gif&ver=1.7.2 for caching. You might need to exclude this file in WP Minify options. The Developer Network – Simple PHP Caching Snippet

Simple PHP Caching Snippet

When your site is under a heavy server load you will soon understand why it is important to cache your scripts. Caching will take a load off your server by serving up static content that doesn’t have to put the PHP processor under an intense strain. Your best bet for setting the speed at which this script caches your data is by putting it on a cron task.

So how might you do this? Well here I will show you a basic snippet that has worked well for me.

Note the ### DYNAMIC BEGIN –> ############################# part. It’s where you need to put any tracking scripts or what-not into.

Snippet (MUST GO AT VERY TOP OF YOUR SCRIPT):

  1. <?php
  2.  // Simple PHP Page Caching Snippet
  3.  // best to use cron and put this in a timer
  4.  // eg) /usr/bin/lynx –dump http://yoursite.com/scriptname.php?&cacheit=cache > /dev/null
  5.  // http://thedevnet.com/php/snippets/simple-php-caching-snippet/
  6.  
  7.  
  8.  // put your ini_set, ob_start, session_start after this line
  9.  ini_set('zlib.output_compression_level', 1);
  10.  ob_start('ob_gzhandler');
  11.  
  12.  
  13.  /* BEGIN –> Cache ->> above all else */
  14.  $cachedir = "./bobo_cache/";
  15.  $scriptname = $_SERVER['SCRIPT_NAME'];
  16.  
  17.  
  18.  function make_cache()
  19.  {
  20.   global $cachedir;
  21.   global $scriptname;
  22.  
  23.   $scripturl  = $_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
  24.   $data   = file_get_contents($scripturl."?&system=yes");
  25.  
  26.   // geez for compatibility purposes i do make sacrifices 8 (
  27.   $fp = fopen($cachedir.$scriptname,"w+");
  28.   flock($fp, LOCK_EX);
  29.   ftruncate($fp, 0);
  30.   fwrite($fp, $data);
  31.   fclose($fp);
  32.  }
  33.  
  34.  
  35.  if(@$_GET['cacheit'] == "cache")
  36.  {
  37.   make_cache();  
  38.   die();
  39.  }
  40.  
  41.  // uncomment the following line if you do not wish to use cron
  42.  // if(rand(1,50) == 10) make_cache(); // roll a 50 sided dice; if its a 10 then make cache
  43.  
  44.  /* END –> Cache ->> above all else */
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  # <!– BEGIN Caching Mechanism –>
  51.  // if you have pagination or something that you would like to continue to load dynamically
  52.  // say ?p=1 or what not might be good to add an exclusion to the if statement
  53.  // eg) if( @empty($_GET['system']) && @empty($_GET['p']) )
  54.  if( @empty($_GET['system']) )
  55.  {
  56.   ### DYNAMIC BEGIN –> #############################
  57.   // put your dynamic stuff here (tracking scripts, dynamic content, etc)
  58.  
  59.   ### <– DYNAMIC END   #############################
  60.  
  61.   // if the cache exists then use it and kill processing.
  62.   if(file_exists($cachedir.$scriptname))
  63.   {
  64.    include_once($cachedir.$scriptname);
  65.    die("\r\n\r\n<!– Bobo Cached Version –>");
  66.   }
  67.   // if it couldn't find a cache it'll continue loading as a failsafe
  68.  }
  69.  # <!– END Caching Mechanism –>
  70.  
  71.  
  72. ?>
GD Star Rating
loading...

Originally posted 2009-10-22 09:44:55.

Popularity: 7%

Posted by irbobo   @   23 July 2010

Like this post? Share it!

RSS Digg Twitter StumbleUpon Delicious Technorati Facebook

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

You must be logged in to post a comment.

Previous Post
« Zoom Crop Image
Next Post
Easily Zip Your Files Using PHP »
Powered by Wordpress   |   Lunated designed by ZenVerse