Jump to content

php partial cache


stegers1972

Recommended Posts

Hello ;

 

I am trying to cache part of a php page, the part of the php page is in the header. Whenever I run it the first time, page displays correctly. However when I run it for a second time , reading from cache file, the php page breaks down after displaying the cache file. What am I doing wrong, is this because of the fclose () ?

<?php
class cache
{
    var $cache_dir = './cachemenu/';//This is the directory where the cache files will be stored;
    var $cache_time = 1000;//How much time will keep the cache files in seconds.
    
    var $caching = false;
    var $file = '';

        function cache()
    {
        //Constructor of the class
        $this->file = $this->cache_dir . md5($_SERVER['REQUEST_URI']);
        if ( file_exists ( $this->file ) && ( fileatime ( $this->file ) + $this->cache_time ) > time() )
        {
            //Grab the cache:
            $handle = fopen( $this->file , "r");
            do {
                $data = fread($handle, 8192);
                if (strlen($data) == 0) {
                    break;
                }
                echo $data;
            } while (true);
            fclose($handle);
            exit();
        }
        else
        {
            //create cache :
            $this->caching = true;
            ob_start();
        }
    }
    
    function close()
    {
        //You should have this at the end of each page
        if ( $this->caching )
        {
            //You were caching the contents so display them, and write the cache file
            $data = ob_get_clean();
            echo $data;
            $fp = fopen( $this->file , 'w' );
            fwrite ( $fp , $data );
            fclose ( $fp );
        }
    }
}
?>

I i include the file in the pages where I want to use it and run it on the specific page with 

<?php $ch = new cache(); ?>

 //Content goes here 

<?php $ch->close(); ?>

What am I doing wrong ?

 

Thanks;

Pascal

Link to comment
Share on other sites

You're exit;ing if you want to show the cached version. Which would be alright except you miss out on the rest of the content.

 

Your scheme won't work: for the code after $ch->clone() to run, all the code before it must also run. Which will defeat the purpose of caching.

Try something else: move the header/menu thing into its own file, then write a function to either include a file or use a cached version if available (basically what you have now).

1. The header/menu thing has its own file and doesn't actually do anything involving caching or whatever. Just does its header/menu stuff.

2. The main file calls some function, like runfile("header.php"), knowing that one way or another that file's contents will be outputted

3. That function does your caching bit, which it can do by using your cache class or not (I'd do the latter)

 

Also,

- That's PHP 4-style code. You're not still using PHP 4, right?

- The whole fopen/fread/fclose loop can be replaced with one call to file_get_contents, or readfile since all you're doing is outputting the contents.

- You should be using filemtime() for the modification time and not fileatime() for the access time which may not even be supported by the filesystem.

Edited by requinix
Link to comment
Share on other sites

Thanks for your, so going with PHP5 I created following :

<?php
$cache_file = 'cachemenu/content.cache';
if(file_exists($cache_file)) {
  if(time() - filemtime($cache_file) > 86400) {
     // too old , re-fetch
     $cache = file_get_contents('$cache_file');
     file_put_contents($cache_file, $cache);
  } else {
     // cache is still fresh
  }
} else {
  // no cache, create one
  $cache = file_get_contents('includes/menu.php');
  file_put_contents($cache_file, $cache);
}
?>

It creates the cachefile correctly, but does not output it. What am I doing wrong ?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.