PHP Cache

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
venkatsyss
Forum Newbie
Posts: 1
Joined: Tue Oct 26, 2010 2:20 am

PHP Cache

Post by venkatsyss »

Hi,

I have tried to implement php cache for my project however I could not able to find the solution to implement.The below functions used to represent the cache functions.

Code: Select all

var $cacheDir  = '../cache/'; // The default directory where to store chached files
var $cacheFile = '';       // The content of the actual cached file
var $timeOut   = 1000;     // The time how long the cached file remains reusable
var $startTime = 0;        // The startime to determine script execution time 
var $cache     = true;  

----------------------------------------------------------------------------------------
function getMicroTime() {
           list($usec, $sec) = explode(" ",microtime());
          return ((float)$usec + (float)$sec);
    }

----------------------------------------------------------------------------------------	
function startCache(){
      $this->startTime = $this->getMicroTime();
      if ($this->status){ 
         $this->cacheFile = $this->cacheDir.urlencode( $_SERVER['REQUEST_URI'] );
         if ( (file_exists($this->cacheFile)) && 
              ((fileatime($this->cacheFile) + $this->timeOut) > time()) )
         {
            //Read file from the cache
            $handle = fopen($this->cacheFile , "r");
            $html   = fread($handle,filesize($this->cacheFile));
            fclose($handle);
          
            // Display the content
            echo $html;

            //Display the rendering time
            echo '<p>Total time: '
                 .round(($this->getMicroTime())-($this->startTime),4).'</p>';
            
            //Exit from the code as we displayed cached data
            exit();
          }
          else
          {
             // Set to save generated content into file
             $this->caching = true;
			// ob_start();
          }
      }
    }
--------------------------------------------------------------------------------------------------------------
function endCache(){
      if ($this->status){
         if ( $this->caching )
         {
            //You were caching the contents so display them, and write the cache file
            $html = ob_get_clean();
            echo $html;
            $handle = fopen($this->cacheFile, 'w' );
            fwrite ($handle, $html );
            fclose ($handle);

            //Display the rendering time
            echo '<p>Total time: '.round(($this->getMicroTime()-$this->startTime),4).'</p>';
         }
      }       
    } 
I have tried using the above functions but could not able to find the solution.Please find me solution to solve the problem.

Thanks and Regards,
Venkatesan.R
Last edited by Benjamin on Tue Oct 26, 2010 6:29 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
Post Reply