Gallery3:Modules:cache - Gallery Codex
Personal tools

Gallery3:Modules:cache

From Gallery Codex

Gallery 3 Super Recycler

Cache module for Gallery 3

Description

Database Info will allow Gallery 3 administrators to see the size of their gallery 3 database as well as the number of tables in it.

WARNING

This module is highly experimental and in an early stage of development. It most likely will break your Gallery site in one way or another, and it could potentially erase your entire file system. That said, it works for me --
your mileage may vary.

METHOD

The Recycler module works pretty much the same way as Wordpress Super Cache. For visiting guests, a static HTML file is served immediately, if it exists in the cache, and PHP is never invoked. This will drastically reduce CPU usage on a relatively busy site, as the Kohana framework is a real power drainer.

For registered users who are logged on, there is half caching with minimal PHP execution. This is to preserve user names and user settings, and to ensure security. Half cached pages are always checked against Gallery session ID.

Cached pages will respect locale (language setting) if set with the module Language Flags, that is, it will build separate cache copies from each locale, plus the default locale. A Chinese user will thus always see Chinese pages (if Chinese is an option) rather than a mix of Swedish, Arabic and English pages.

Cached pages also respect current host, in case you have multiple hosts connected to the same Gallery installation. This may or may not be unnecessary.

As of now, all cached pages will be flushed whenever there is a change on the page, for instance when a comment is submitted. Cached pages will exist for 31 days unless there is a change. This could easily be changed in the code for those who want. Photo sites typically do not change that much for long periods.

Future enhancements obviously include an interface for setting parameters and a more fine grained flush control. As for now, I need to collect bugs for various situations, things that shouldn't be cached and so on, and get some general impressions of how it is working out in different settings.

Cached pages will obviously undo any dynamic content whatsoever. Random images in the sidebar will remain the same for the same page, and hits will not be recorded by Gallery -- module makers should think in terms of implementing things in AJAX/javascript instead of relying on Kohana.

INSTALLATION INSTRUCTIONS

This module does not yet have an interface. It must be installed manually.

  1. Copy the cache folder to your Gallery modules folder.
  2. Locate a file local.php in your gallery root directory. If there is no such file, create it.

Included in this distribution is a copy of local.php with the following lines:

     <? defined("SYSPATH") or die("No direct script access");
     // error_reporting(E_ALL);
     // ini_set('display_errors', true);

    // date_default_timezone_set("Europe/Stockholm");

    require MODPATH . "cache/index.php";

Only the first and the last lines are of importance. The first line makes sure we are part
of the Gallery system, and the second line makes the call to the cache module.
It doesn't hurt to set a default timezone; it is actually required in PHP 5 but is not set by Gallery.

  1. Locate your .htaccess file in the Gallery root directory. If there is no .htaccess file, create one.

If there already is a .htaccess file, the last lines will read something like this:

     # <IfModule mod_rewrite.c>
     # RewriteEngine On
     # RewriteBase /
     # RewriteCond %{REQUEST_FILENAME} !-f
     # RewriteCond %{REQUEST_FILENAME} !-d
     # RewriteRule ^(.*)$ index.php?kohana_uri=$1 [QSA,PT,L]
     # RewriteRule ^$ index.php?kohana_uri=$1 [QSA,PT,L]
     # </IfModule>

All of these lines need to be uncommented. Uncomment by removing the hash signs (or create the lines if necessary):

     <IfModule mod_rewrite.c>
     RewriteEngine On
     RewriteBase /
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteRule ^(.*)$ index.php?kohana_uri=$1 [QSA,PT,L]
     RewriteRule ^$ index.php?kohana_uri=$1 [QSA,PT,L]
     </IfModule>

What this does is to get rid of the index.php part of the Gallery URL. It serves no function and shouldn't be there at all, but it is a Kohana thing.

If your Gallery 3 installation is not in a root directory, but in a separate directory, the Rewrite statement on the third line must be rewritten as in the following examples:

     # Gallery 3 URL RewriteBase line
     # ============= ====================
     # http://example.com/gallery3 RewriteBase /gallery3
     # http://example.com/~bob/photos RewriteBase /~bob/photos
     # http://gallery3.example.com/ RewriteBase /

Although not required, you might as well add (or uncomment) these lines:

     <IfModule mod_expires.c>
     ExpiresActive On
     # Cache all files for a month after access (A).
     ExpiresDefault A2678400
     # Do not cache dynamically generated pages.
     ExpiresByType text/html A1
     </IfModule>

Next, we will add rules for the Recycler system to the .htaccess file:

     # BEGIN G3Recycler
     <IfModule mod_rewrite.c>
     RewriteEngine On
     RewriteBase /
     AddDefaultCharset UTF-8
 
     RewriteCond %{REQUEST_URI} (.*)(//)(.*)
     RewriteRule ^(.*) %1/%3 [L]

     RewriteCond %{QUERY_STRING} ^page=([0-9]+)$
     RewriteRule ^(.*)$ - [env=page:%1]

     RewriteCond %{REQUEST_METHOD} !POST
     RewriteCond %{HTTP_COOKIE} !^.*(g3_recycler_user).*$
     RewriteCond %{HTTP:Cookie} !g3_recycler_basket
     RewriteCond %{HTTP:Cookie} g_locale=([^;]+) [OR]
     RewriteCond %{HTTP:Cookie} !g_locale
     RewriteCond %{DOCUMENT_ROOT}/var/cache/recycler/%{HTTP_HOST}/%1/$1/%{ENV:page}/index.html -f
     RewriteRule ^(.*) /var/cache/recycler/%{HTTP_HOST}/%1/$1/%{ENV:page}/index.html? [NS,L]
     </IfModule>
     # END G3Recycler

These must be written before Gallery's rewrite statements, at the top of the .htaccess document.

The rewrite base must be rewritten here as well, if your Gallery installation is not in the root directory. If your Gallery 3 installation resides in a folder gallerytest, then the rules will look like this:

     # BEGIN G3Recycler
     <IfModule mod_rewrite.c>
     RewriteEngine On
     RewriteBase /gallerytest
     AddDefaultCharset UTF-8

     RewriteCond %{REQUEST_URI} (.*)(//)(.*)
     RewriteRule ^(.*) %1/%3 [L]

     RewriteCond %{QUERY_STRING} ^page=([0-9]+)$
     RewriteRule ^(.*)$ - [env=page:%1]

     RewriteCond %{REQUEST_METHOD} !POST
     RewriteCond %{HTTP_COOKIE} !^.*(g3_recycler_user).*$
     RewriteCond %{HTTP:Cookie} !g3_recycler_basket
     RewriteCond %{HTTP:Cookie} g_locale=([^;]+) [OR]
     RewriteCond %{HTTP:Cookie} !g_locale
     RewriteCond %{DOCUMENT_ROOT}/gallerytest/var/cache/recycler/%{HTTP_HOST}/%1/$1/%{ENV:page}/index.html -f
     RewriteRule ^(.*) /var/cache/recycler/%{HTTP_HOST}/%1/$1/%{ENV:page}/index.html? [NS,L]
     </IfModule>
     # END G3Recycler

The 4:th line from the beginning and the 4:th line from the end have been changed.

Note that all of this assumes you have not moved your var folder or anything else.

You are now ready to try it out. Make sure compression is not enabled in Gallery 3.