G1 built in caching hack.

iturgeon

Joined: 2004-07-21
Posts: 7
Posted: Tue, 2007-03-13 18:42

I have a client that uses g1.5.4(They want the direct image linking ability). They have high traffic with 3k+ images on a shared host (dreamhost).

The site was dropping to useless during high load. Sometimes completely unresponsive main page, and 15s+ album pages.

I kept pushing g2 because of its better handeling of large galleries and built in caching, but their workflow relies on directly linking to the images, so I looked into optimizing g1. I looked at http://codex.gallery2.org/Gallery1:Performance_Tips. They didn't have huge albums, although the page doesn't define what a "large album" is. They werent using the album tree, but were using the accurate photo count item.

I turned off any and all frivolous features that didn't have to do with uploading and displaying photos. It was still crawling.

I looked to the suggested crontab caching technique next. It worked, and the main page loaded quickly. However randomly throughout the day, the cache file would be 0 bytes and the gallery would be just as useless again.

So I took to the code, and added a built in caching technique. I added caching to albums.php and view-album.php. It supports multi-page main pages and album pages, rebuilds the caches automatically and has kept g1 useable on the current host for my client.

I thought I'd share the caching code so the others out there that have to or must use g1 can hopefully use it.

I haven't spent any time beautifying, but the code is working..

album.php:
I re-purposed the cache file detection function at the top of the page
---
if (empty($gallery->session->username)) {
/* Get the cached version if possible */
if (!getRequestVar('gallery_nocache')) {
$cache_now = time();
if(getRequestVar('albumListPage') > 0){
$cPage = getRequestVar('page');
}
elseif (isset($gallery->session->albumListPage)) {
$cPage= $gallery->session->albumListPage;
}
else {
$cPage = 1;
}
$cacheFileName = "cache-albums" . $cPage . ".html";
$cache_file = dirname(__FILE__) . '/cache/' . $cacheFileName;
// file exists, check its creation date see if we need to rebuild it
if (fs_file_exists($cache_file)) {
$cache_stat = @stat($cache_file);
if ($cache_now - $cache_stat[9] < (60 * 60)) { // if the cache file is less then 60 minutes old just read it
if ($fp = fopen($cache_file, "rb")) {
if ($fp = fopen($cache_file, "rb")) {
while (!feof($fp)) {
print fread($fp, 4096);
}
fclose($fp);
printf("<!-- From %s, created at %s -->", $cacheFileName, strftime("%D %T", $cache_stat[9]));
exit();
}
}
}
// the cache file is too old, buffer to create it
else{
$outputBuffering = true;
ob_start();
}
}
// the cache file doesnt exist, buffer to create it
else{
$outputBuffering = true;
ob_start();
}
}
}
----

and at the bottom:
---
<?php if (!$GALLERY_EMBEDDED_INSIDE) { ?>
</body>
</html>
<?php }
// if output buffering was enabled, deal with it here
if($outputBuffering == true){
$fileContents = ob_get_contents();
ob_end_clean();
// output the contents to the user
echo $fileContents;
echo "<!-- new cache file created -->";
// write the contents to a cache file
$fp = fopen(dirname(__FILE__) . '/cache/'. $cacheFileName, "wb");
fwrite($fp, $fileContents);
fclose($fp);
}
?>
----

 
Tim_j
Tim_j's picture

Joined: 2002-08-15
Posts: 6818
Posted: Tue, 2007-03-13 19:00

Hello,

can you provide a unified diff ?

thanks,
Jens
--
Last Gallery v1 Developer and v1 translation manager.

 
h0bbel
h0bbel's picture

Joined: 2002-07-28
Posts: 13451
Posted: Tue, 2007-03-13 19:01

Also, you can do direct linking in G2 as well?


h0bbel - Gallery Team
If you found my help useful, please consider donating to Gallery
http://h0bbel.p0ggel.org

 
Tim_j
Tim_j's picture

Joined: 2002-08-15
Posts: 6818
Posted: Tue, 2007-03-13 19:16

True! forget my post.

Move over to G2.

Jens
--
Last Gallery v1 Developer and v1 translation manager.

 
iturgeon

Joined: 2004-07-21
Posts: 7
Posted: Wed, 2007-03-14 00:20

There are various reasons they want to use g1, but can you point me in the direction of whatever it is I need to do direct image linking (not through url rewrite or using the protection of the g2 DownloadItem module)?

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Tue, 2007-05-01 15:55

speaking of g2...

> (not through url rewrite or using the protection of the g2 DownloadItem module)?

what's the problem with mod_rewrite? the URLs look like normal static image URLs with it... and direct linking is no problem at all.
and performance isn't a big factor. with g2's fast-download cache (always enabled), core.DownloadItem requests are very fast.

--------------
Documentation: Support / Troubleshooting | Installation, Upgrade, Configuration and Usage