Cache for nested albums

Psychoman

Joined: 2006-07-20
Posts: 1
Posted: Thu, 2006-07-20 12:27

I use gallery 1 on a webserver I administrate myself.
The webserver hardware is slow (P2 450Mhz, 196MB RAM).
Caching the mainpage greatly improves the performance.

As I use a lot of nested albums it would be nice
to be aible to cache the nested album pages as well.

I organised the albums by year on the mainpage
and I nest albums by activity in every year category.
It will be rare to add an album in a past year category.

Caching the nested albums main page (not the album itself)
would greatly improve performance
and caching past year albums once in a while should be enough
since they don't change often.

 
kevin360

Joined: 2003-09-06
Posts: 50
Posted: Tue, 2006-08-22 00:26

Here's a method I came up with for doing it. You need to add some code to a php file and then use httrack and sed to create the cache files.

In view_album.php add this code just before $gallery->session->offlineAlbums[$gallery->album->fields["name"]]=true; (which is near the top):

if (empty($gallery->session->username)) {
    /* Get the cached version if possible */
    $cache_file = "cache.html";
    if (!getRequestVar('gallery_nocache')) {
        $cache_now = time();
        $cacheFileBaseNames = array(sprintf("cache-%s.html", $_SERVER['HTTP_HOST']), "cache.html");
        foreach ($cacheFileBaseNames as $cache_file_basename) {
//            $cache_file = dirname(__FILE__) . '/' . $cache_file_basename;
            $cache_file = $gallery->app->albumDir . '/' . $gallery->session->albumName . '/' . $cache_file_basename;
            if (fs_file_exists($cache_file)) {
                $cache_stat = @stat($cache_file);
                if ($cache_now - $cache_stat[9] < (60 * 60 * 25)) {
                    if ($fp = fopen($cache_file, "rb")) {
                        while (!feof($fp)) {
                            print fread($fp, 4096);
                        }
                        fclose($fp);
                        printf("<!-- From %s, created at %s -->",
                        $cache_file_basename, strftime("%D %T", $cache_stat[9]));
                        return;
                    }
                }
            }
        }
    }
}

This code is almost identical to the code in albums.php except for the one line commented out and changed. Now we just need to create the cache files for the nested album. This code below is a script and you'll need to modify it to fit your domain and the nested album directory name. Here are some things you'll have to change:

http://www.yourdomain.com/gallery -> url to your gallery
http://www.yourdomain.com/albums -> url to your albums directory
nested_album -> name of the nested album you want to cache

#!/bin/bash
# Remove old cache files
rm /to/your/albums_dir/nested_album/cache*html
# Clean the tmp directory
rm -rf /to/tmp/directory/*
# Create new cache files
/usr/local/bin/httrack -s0 http://www.yourdomain.com/gallery/nested_album "-*"  \
   "+*.yourdomain.com/gallery/nested_album*" -N "cache%[page].%t" \
   -O /to/tmp/directory/
# Fix links in cache files
cd /to/tmp/directory/
for name in cache*html; do
    cp -a $name ${name}.old
    sed -e "s/cache/http:\/\/www.yourdomain.com\/albums\/nested_album\/cache/" < ${name}.old > ${name}
done
rm *old

# Move over new cache files
mv /to/tmp/directory/cache*html /to/your/albums_dir/nested_album/

The addition to view_album.php is set to pull up any cache file that is less then 25 hours old. So if you do a cron job every night it'll just pull up the cached files.

From my own little tests it looks like about a 2x increase in speed to pull up the nested album page.