Supersimple static file based (html) php site cach

2019-03-20 17:26发布

I have a website that basically only displays things without any forms and post-gets. This website is PHP based and hosted on shared hosting. It rarely changes. I would like to enable caching for this website. Its shared hosting so i need a solution that:

  • does not use Memcached
  • dont need to move my website to VPS
  • dont use APC or other things

So basically what i would like to acomplish is cache every subsite to HTML and tell PHP to get for 5 minutes the HTML cached version of current subsite and display it to user. And after 5 minutes to refresh the cache.

I've been looking for some while on the internets and there are some tutorials and frameworks that support this kind of kinky cache.

But what i need is just one good library that is extremely easy to use.

I imagine it to work in this way:

<?

if (current_site_cache_is_valid())
{
   display_cached_version();
   die;
}

..mywebsite rendering code

?>

So simple as it sounds but i hope some good fellow developer did library of this kind before. So do you know such ready to use, not very time consuming to implement solution?

5条回答
祖国的老花朵
2楼-- · 2019-03-20 17:59

Just to add a little more to nico's response to make it more useful for generic copy and paste use by saving the time of typing in individual cachefile names for each file saved.

Original:

$cachefile = 'cache/index-cached.html';

Modified:

$cachefile = $_SERVER['DOCUMENT_ROOT'].'/cache/'.pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_FILENAME).'-cached.html';

What this does is to take the filename of whatever file it is located in, minus the extension (.php in my case), and appends the "-cached.html" label and new extension to the cached file. There are probably more efficient ways of doing this, but it works for me and hopefully save others some time and effort.

查看更多
贪生不怕死
3楼-- · 2019-03-20 18:00

I have a simple algo for HTML caching, predicated on the following conditions

  • The user is a guest (logged on users have a blog_user cookie set)
  • The request URI is a GET that contains no request parameters
  • An HTML cache version of the file exists

then an .htaccessrewrite rule kicks in, mapping the request to a cached file. Anything else is assumed to be context-specific and therefore not cacheable. Note that I use wikipedia-style URI mapping for my blog so /article-23 gets mapped to /index.php=article-23 when not cached.

I use a single HTML access file in my DOCUMENT_ROOT directory and here is the relevant extract. It's the third rewrite rule that does what you want. Any script which generates cacheable O/P wraps this in an ob_start() ob_get_clean() pair and write out the HTML cache file (though this is all handled by my templating engine). Updates also flush the HTML cache directory as necessary.

RewriteEngine on
RewriteBase /

# ...
# Handle blog index
RewriteRule ^blog/$                                    blog/index               [skip=1]

# If the URI maps to a file that exists then stop.  This will kill endless loops

RewriteCond %{REQUEST_FILENAME}                        -f
RewriteRule ^blog/.*                                   -                        [last]

# If the request is HTML cacheable (a GET to a specific list, with no query params)
# the user is not logged on and the HTML cache file exists then use it instead of executing PHP

RewriteCond %{HTTP_COOKIE}                             !blog_user
RewriteCond %{REQUEST_METHOD}%{QUERY_STRING}           =GET                     [nocase]
RewriteCond %{DOCUMENT_ROOT}/blog/html_cache/$1.html   -f
RewriteRule ^blog/(article-\d+|index|sitemap.xml|search-\w+|rss-[0-9a-z]*)$ \
                                                       blog/html_cache/$1.html  [last]

# Anything else relating to the blog pass to index.php
RewriteRule blog/(.*)                                  blog/index.php?page=$1   [qsappend,last]

Hope this helps. My blog describes this in more detail. :-)

查看更多
smile是对你的礼貌
4楼-- · 2019-03-20 18:06

You should give skycache a try. edit : this project seems cool too: cacheme

Another solution is to use auto_prepend_file/auto_append_file. Something like what's described in this tutorial: Output caching for beginners

查看更多
beautiful°
5楼-- · 2019-03-20 18:13

This is how I normally do this, however I don't know your URL design nor your directory / file layout.

I do this with .htaccess and a mod_rewrite­Docs.

The webserver checks if a cached HTML file exists, and if yes, it's delivered. You can also check it's age.

If it's too old or if it does not exists your PHP script(s?) is started. At the beginning of your script you start the output buffer­Docs. At the end of your script, you obtain the output buffer and you place the content into the cache file and then you output it.

The benefit of this solution is, that apache will deliver static files in case they exist and there is no need to invoke a PHP process. If you do it all within PHP itself, you won't have that benefit.

I would even go a step further and run a cron-job that removes older cache-files instead of doing a time-check inside the .htaccess. That done, you can make the rewrite less complex to prefer a .php.cached file instead of the .php file.

查看更多
别忘想泡老子
6楼-- · 2019-03-20 18:18

It's a while since you asked this, but as this is still gathering search hits I thought I'd give you a better answer.

You can do static caching in PHP without .htaccess or other trickery. I found this at http://simonwillison.net/2003/may/5/cachingwithphp/ :

<?php
$cachefile = 'cache/index-cached.html';
$cachetime = 5 * 60;
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) {
    include($cachefile);
    echo "<!-- Cached copy, generated ".date('H:i', filemtime($cachefile))." -->\n";
    exit;
}
ob_start(); // Start the output buffer

/* The code to dynamically generate the page goes here */

// Cache the output to a file
$fp = fopen($cachefile, 'w');
fwrite($fp, ob_get_contents());
fclose($fp);
ob_end_flush(); // Send the output to the browser
?>
查看更多
登录 后发表回答