SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Please help to improve my pHp cache ?

  1. #1
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Please help to improve my pHp cache ?

    Hello,
    i have read a bit around in the internet about the php cache.
    at the moment i am using, this system to cache my pages:

    This is putted on the start of the page

    PHP Code:
    <?php
        
            
    // Settings
            
    $cachedir 'cache/'// Directory to cache files in (keep outside web root)
            
    $cachetime 600// Seconds to cache files for
            
    $cacheext 'html'// Extension to give cached files (usually cache, htm, txt)
        
            // Ignore List
            
    $ignore_list = array(
                
    'addedbytes.com/rss.php',
                
    'addedbytes.com/search/'
            
    );
        
            
    // Script
            
    $page 'http://' $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; // Requested page
            
    $cachefile $cachedir md5($page) . '.' $cacheext// Cache file to either load or create
        
            
    $ignore_page false;
            for (
    $i 0$i count($ignore_list); $i++) {
                
    $ignore_page = (strpos($page$ignore_list[$i]) !== false) ? true $ignore_page;
            }
        
            
    $cachefile_created = ((@file_exists($cachefile)) and ($ignore_page === false)) ? @filemtime($cachefile) : 0;
            @
    clearstatcache();
        
            
    // Show file from cache if still valid
            
    if (time() - $cachetime $cachefile_created) {
        
                
    //ob_start('ob_gzhandler');
                
    @readfile($cachefile);
                
    //ob_end_flush();
                
    exit();
        
            }
        
            
    // If we're still here, we need to generate a cache file
        
            
    ob_start();
        
        
    ?>
    MY HTML CODE Goes here .............

    and the code below is at the footer of my page.

    PHP Code:
    <?php
        
            
    // Now the script has run, generate a new cache file
            
    $fp = @fopen($cachefile'w'); 
        
            
    // save the contents of output buffer to the file
            
    @fwrite($fpob_get_contents());
            @
    fclose($fp); 
        
            
    ob_end_flush(); 
        
        
    ?>


    There are some things that i need and this code dont have them :

    - gzip
    - the expired cache is not autodeleted after it expire.

    Also wanted to ask, if this code is secure to use , if some one can suggest a better one or something to improve the current code it will be just great

    Thank you fro reading this post.
    Best Regards
    Meo

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,849
    Mentioned
    16 Post(s)
    Tagged
    1 Thread(s)
    If $cachedir is outside of (above) your website doc root, then why do you need to give it an md5()ed name?

    if the request /about.php resolves to:

    users/me/mywebsite/about.php

    Then why not fetch

    users/me/mycache/about.php

    Then there is no need to worry about deleting the files, they just overwrite themselves.

    I am just wondering, is there an express reason you md5() the file name?

  3. #3
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hello, thank your for the reply.
    about the md5 i just copied this from another website, and i see that is working. but need some little modifications,

    after reading a bit around i have added some things, below is the code :

    PHP Code:
    <?php
        
    // Settings
        
    $cachedir 'cache2/'// Directory to cache files in (keep outside web root)
        
    $cachetime 100// Seconds to cache files for
        
    $cacheext 'html'// Extension to give cached files (usually cache, htm, txt)

        // Ignore List
        
    $ignore_list = array(
            
    'addedbytes.com/rss.php',
            
    'addedbytes.com/search/'
        
    );

        
    // Script
        
    $page 'http://' $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; // Requested page
        
    $cachefile $cachedir md5($page) . '.' $cacheext// Cache file to either load or create

        
    $ignore_page false;
        for (
    $i 0$i count($ignore_list); $i++) {
            
    $ignore_page = (strpos($page$ignore_list[$i]) !== false) ? true $ignore_page;
        }

        
    $cachefile_created = ((@file_exists($cachefile)) and ($ignore_page === false)) ? @filemtime($cachefile) : 0;
        @
    clearstatcache();


        if (
    time() - $cachetime $cachefile_created) {

        
    //ob_start('ob_gzhandler');
        
    echo gzuncompress(file_get_contents($cachefile));
        
    //ob_end_flush();
        
    exit();

      } else {
        if(
    file_exists($cachefile) && is_writable($cachefile)) unlink($cachefile);
       }
        
        
    // If we're still here, we need to generate a cache file

        
    ob_start();
    ?>
    AT THE END OF PAGE
    PHP Code:
    <?php
         
    // Now the script has run, generate a new cache file
        
    $fp = @fopen($cachefile'w'); 

        
    // save the contents of output buffer to the file
        
    @fwrite($fpgzcompress(ob_get_contents(), 9));
        @
    fclose($fp); 

        
    ob_end_flush(); 

    ?>
    Is any thing wrong here ? i have added gzip function.. too.

    Thank you for looking into this.
    Best Regards
    AvinD

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,849
    Mentioned
    16 Post(s)
    Tagged
    1 Thread(s)
    Right, so you haven't written this code, you copied it and you want us to give you a hand to improve it for you?

  5. #5
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes if is possible.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •