FastCache-Talk

Summary: Talk page for FastCache.
Maintainer: Eemeli Aro
Users: +1 (View / Edit)

This space is for User-contributed commentary and notes. Please include your name and a date along with your comment.

Comments

To make FastCache recipe compatible with PHP5.5 (for PmWiki 2.2.58+), and overcome "Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /pmwiki.php" errors, replace the markup line in fastcache.php (line#28) with :

Markup_e( 'nofastcache', 'directives', '/\\(:nofastcache:\\)/i', "PZZ( \$GLOBALS['FastCacheValid'] = FALSE )" );


I thought to share my Nginx configuration for using FastCache. It was tricky to set up!


server {
  listen 80;

  # Compression
  gzip on;
  gzip_http_version 1.1;
  gzip_vary on;
  gzip_comp_level 6;
  gzip_proxied any;
  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js;
  gzip_buffers 16 8k;
  gzip_disable "MSIE [1-6]\.(?!.*SV1)";

  root /Depot/Applications/PmWiki; # change this to your PmWiki installation root

  # Remember: Nginx picks the best plain location matching only *after* all regex matching is tested.

  # Send .php URIs to PHP via FastCGI
  location ~ \.php$ {
    try_files $uri =404; # security issue!
    include fastcgi_params;
    fastcgi_pass 127.0.0.1:9000; # php-fpm socket
    #fastcgi_split_path_info ^(.+\.php)(/.+)$; # must have "cgi.fix_pathinfo = 0;" in php.ini
  }

  location / {
    # Bypass FastCache
    if ($request_method = POST) {
      rewrite ^(.*) /pmwiki.php?n=$pmwiki_group%2f$pmwiki_page last;
    }
    if ($cookie_PHPSESSID != "") {
      rewrite ^(.*) /pmwiki.php?n=$pmwiki_group%2f$pmwiki_page last;
    }
    if ($args != "") {
      rewrite ^(.*) /pmwiki.php?n=$pmwiki_group%2f$pmwiki_page last;
    }

    # Try PmWiki FastCache first
    try_files /pub/htmlcache/$pmwiki_group.$pmwiki_page.html /pmwiki.php?n=$pmwiki_group%2f$pmwiki_page;
  }

  # The root is the Main/HomePage wiki
  location = / {
    # Bypass cache
    if ($request_method = POST) {
      rewrite ^(.*) /pmwiki.php?n=Main/HomePage last;
    }
    if ($cookie_PHPSESSID != "") {
      rewrite ^(.*) /pmwiki.php?n=Main/HomePage last;
    }
    if ($args != "") {
      rewrite ^(.*) /pmwiki.php?n=Main/HomePage last;
    }

    try_files /pub/htmlcache/Main.HomePage.html /index.php;
  }

  # Add browser caching to pub resources
  location /pub/ {
    if ($request_uri ~* \.(jpg|jpeg|gif|png|css|js|ico)$) {
      expires 365d;
    }
  }
}

# PmWiki pattern (/Group/Page or /Group.Page)
map $uri $pmwiki_group {
  ~^/(?<g>[^/.]+)[/.] $g;
}
map $uri $pmwiki_page {
  ~^/([^/.]+)[/.](?<p>[^/]+)$ $p;
}
Contributed by Tal Liron, May 1 2013.

My fastcache installation uses php to serve cached pages instead of mod-rewrite.
fastcache.php does not correctly detect when I am logged in. I fixed my problem by checking for the session id in $_REQUEST.

    #In my config.php
    if (!empty($_REQUEST[ini_get('session.name')])) {
      $FastCacheValid=FALSE;
    }

I noticed fastcache checks for the existance of the session id in mod rewrite, but not in php.
Comment by MagicBeanDip 2007-11-02

Should now work as intended. --EemeliAro January 15, 2008, at 11:41 AM

Thanks very much for this recipe. I had to modify it to work on Apache for Windows, presumably because fopen and rename behave differently.

  1. Instead of passing 'x' to fopen, I had to pass 'w'.
  2. rename() didn't work so I had to first do unlink() of the old cache file.
    1. Please provide details of how to make the unlink change? Perhaps EemeliAro can update the script to handle errors more elegantly by working around them?

Also, this doesn't seem to be properly WikiFarm compatible since duplicate page names will be cached in the same place. I haven't figured out an elegant fix for this yet. Comment by Mani Varadarajan 2008-03-06.

Actually, multiple farms are supported. Just set a different $FastCacheDir for the different farms. --EemeliAro March 09, 2008, at 02:11 PM

Thank you for this. In my case, regrettably, I cant run under Apache and I found that FastCache made little performance boost without mod_rewrite. Setting $EnableHTMLCache=1 in pmwiki.php also appeared to make no difference in my case (though I am using (:title:) which may interfere with it). I then tried Cookbook.StaticPages and it seems to make a *huge* difference in the time it takes to open a page in a browser. In my case the pages are infrequently edited but frequently viewed. (Ben, 21 Dec 2008).


I have Fastcache installed and I have $EnableHTMLCache set to 1 in pmwiki.php, but I'm not using mod_rewrite since I'm not familiar enough with that yet. I had expected to be able to observe HTML files added to the htmlcache folder, and I do see that happening. However, it's not retaining those files for long. This is regardless of whether those pages were edited later. Right now there are only six html files in the cache. I do seem to be getting a boost in performance, but I also turned on Cloudflare caching a couple of days ago, so the boost may be due to Cloudflare more than FastCache. I'm just wondering why the htmlcache folder doesn't retain the HTML versions of pages until an author action takes place, upon which point it should overwrite the file with a new file bearing the same name. Why would the htmlcache ever get smaller, in other words? - - - Thanks, dbmurray

FastCache invalidates all entries whenever a single page is updated. Is this what you're seeing? -- jb


Talk page for the FastCache recipe (users).