2
<?php
// cache - will work online - not locally
// location and prefix for cache files
define('CACHE_PATH', "siteCache/"); 

// how long to keep the cache files (hours)
define('CACHE_TIME', 12); 
// return location and name for cache file 
function cache_file() 
{ 
    return CACHE_PATH . md5($_SERVER['REQUEST_URI']); 
}

// display cached file if present and not expired 
function cache_display() 
{ 
    $file = cache_file(); 
    // check that cache file exists and is not too old 
    if(!file_exists($file)) return; 
    if(filemtime($file) < time() - CACHE_TIME * 3600) return; 
    // if so, display cache file and stop processing
    readfile($file);
    exit; 
} 

// write to cache file 
function cache_page($content) 
{ 
    if(false !== ($f = @fopen(cache_file(), 'w'))) { 
        fwrite($f, $content);
        fclose($f); 
    } 
    return $content; 
} 

// execution stops here if valid cache file found
cache_display(); 

// enable output buffering and create cache file 
ob_start('cache_page');
?>

This is the cache code that I am using in a dynamic website in db file. And every page contains this code at top.

<?php session_start();
include("db.php"); ?>

Pages are being cached and its working but on form submission, on user login, on variable passing through pages, nothing is happening. Old pages are being displayed. How do I use this caching code so that it may work but site remain functional as well.

I wonder how wordpress plugins do it. Wp Super Cache and W3T Cache cache everything, yet blog remains functional. Should I selectively use it at parts of website.

Like this:

<?php
// TOP of your script
$cachefile = 'cache/'.basename($_SERVER['SCRIPT_URI']);
$cachetime = 120 * 60; // 2 hours

// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile))) {
    include($cachefile);
    echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))." -->";
    exit;
}

ob_start(); // start the output buffer
// Your normal PHP script and HTML content here
// BOTTOM of your script
$fp = fopen($cachefile, 'w'); // open the cache file for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close the file
ob_end_flush(); // Send the output to the browser
?>

But it will not work as well, because its about pageURL (whole page caching), not the selective content from page.

Please advise. Is there any easy script do do this. Pear::Cache_Lite seems good but its looks difficult to implement.

Update: I have used Cache_Lite. Its the same. Caches everything or included php file. There are few configuration options to play with. But if used as a whole, it will also ignore get, post, session data updates...and will show previous cached pages unless they are deleted.

4

1 回答 1

0

I think you could separate display from logic.

I mean, change the action attribute of the form and point it to a php that does not have cache logic (and you must check the referer or other parameter, using tokens or sessions, or something, to avoid security issues like CSRF).

Other thing that I want to point out is you should look to cache only the most visited pages (i.e. the homepage), generally you don't have a "one size fits all" with caching, and it is better not to worry about pages that don't have speed/load issues. Or it may be better to cache the data if your speed issues comes from a database query (you should profile your application before implementing caching).

Other approach that migth work is checking the request method and disable the cache if it is post (given that all your forms use the POST method) using $_SERVER['REQUEST_METHOD'] == 'POST'.

于 2013-01-26T23:36:04.213 回答