Super simple static file (html) php cache site

I have a website that basically shows only those things that do not have any forms and messages. This site is based on PHP and is hosted on a shared hosting. He rarely changes. I would like to enable caching for this website. Its shared hosting, so I need a solution that:

  • does not use memcached
  • No need to move my site to VPS
  • Do not use APC or other things.

So, basically, what I would like to learn is the cache of each child site in HTML and tell PHP to get an HTML-cached version of the current child site within 5 minutes and display it to the user. And after 5 minutes to update the cache.

I searched some of them on the internet and there are some tutorials and frameworks that support this bunch of cache.

But I need only one good library that is very easy to use.

I assume it works this way:

<? if (current_site_cache_is_valid()) { display_cached_version(); die; } ..mywebsite rendering code ?> 

As simple as it sounds, but I hope some good developer has made such a library before. Do you know that such ready-to-use solutions are not too time-consuming to implement?

+6
source share
5 answers

This is how I usually do it, however I do not know your URL design and your directory / file layout.

I do this with .htaccess and mod_rewrite & shy; Docs .

The web server checks to see if the cached HTML file exists, and if so, it is delivered. You can also check the age.

If it is too old or it does not exist, your PHP script (s?) Is launched. At the beginning of your script, you run the & shy; output buffer Docs . At the end of your script, you get the output buffer and put the contents in a cache file, and then you output it.

The advantage of this solution is that apache will provide static files if they exist, and there is no need to invoke the PHP process. If you do all this in PHP itself, you will not have this benefit.

I would even take one more step and run a cron job that deletes old cache files instead of checking the time inside .htaccess . To do this, you can make rewriting less complicated to prefer the .php.cached file instead of the .php file.

+5
source

I have a simple algorithm for caching HTML based on the following conditions

  • The user is a guest (registered users have a blog_user cookie set)
  • Request URI is a GET that does not contain request parameters
  • There is a version of the HTML cache file

then the .htaccess rewrite rule is applied, matching the request with the cached file. Everything else is assumed to be context sensitive and therefore not cacheable. Note that I use Wikipedia-style URI mapping for my blog, so /article-23 displayed in /index.php=article-23 when not cached.

I use one HTML access file in the DOCUMENT_ROOT directory and here is the corresponding extract. This is the third rewrite rule that does what you want. Any script that generates cached O / P wraps this in a pair of ob_start() ob_get_clean() and writes out an HTML cache file (although all this is handled by my template engine). Updates also clear the HTML cache directory if 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. :-)

+3
source

It has been a while since you asked about this, but since it still collects search queries, I thought I would give you a better answer.

You can do static caching in PHP without using .htaccess or other cheating. 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 ?> 
+2
source

Just add a little more nico's answer to make it more useful for general copy and paste, saving the time it takes to enter separate cache names for each saved file.

Original:

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

Modified:

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

This is done in order to take the file name of any file in which it is located, minus the extension (.php in my case) and add the label “-cached.html” and the new extension to the cached file. There are probably more effective ways to do this, but it works for me and hopefully saves time and effort for others.

+2
source

You must give skycache a try. edit: this project looks cool too: cacheme

Another solution is to use auto_prepend_file / auto_append_file . Something like what is described in this lesson: Caching output for beginners

+1
source

Source: https://habr.com/ru/post/905309/


All Articles