Author Topic: [MOD] Cache System for 4images v1.7  (Read 76697 times)

0 Members and 1 Guest are viewing this topic.

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
[MOD] Cache System for 4images v1.7
« on: June 19, 2005, 07:50:33 PM »
This is an adopted for 4images v1.7 cache feature from 4images v1.7.1


Step 1
Create a new file includes/cache_utils.php[/color]
With this code:
Code: [Select]
<?php
/************************************************************************
 *                                                                        *
 *    4images - A Web Based Image Gallery Management System               *
 *    ----------------------------------------------------------------    *
 *                                                                        *
 *             File: cache_utils.php                                      *
 *        Copyright: (C) 2002 Jan Sorgalla                                *
 *            Email: jan at 4homepages.de                                 *
 *              Web: http://www.4homepages.de                             *
 *    Scriptversion: 1.7.1                                                *
 *                                                                        *
 *    Adopted for 4images v1.7 by V@no                                    *
 *                                                                        *
 *    Never released without support from: Nicky (http://www.nicky.net)   *
 *                                                                        *
 **************************************************************************
 *                                                                        *
 *    Dieses Script ist KEINE Freeware. Bitte lesen Sie die Lizenz-       *
 *    bedingungen (Lizenz.txt) fN?r weitere Informationen.                 *
 *    ---------------------------------------------------------------     *
 *    This script is NOT freeware! Please read the Copyright Notice       *
 *    (Licence.txt) for further information.                              *
 *                                                                        *
 *************************************************************************/
if (!defined('ROOT_PATH')) {
  die("Security violation");
}

function 
create_cache_id($group$params null){
  $cache_id $group;
  if (is_array($params)) {
    $cache_id .= '.' implode('.'$params);
  } elseif (is_string($params)) {
    $cache_id .= '.' $params;
  }
  return $cache_id;
}

function 
get_cache_file($cache_id$lifetime null) {
  global $cache_enable$cache_lifetime$cache_path;

  if (!$cache_enable) {
    return false;
  }

  if (!$lifetime) {
    $lifetime $cache_lifetime;
  }

  $file $cache_path '/' $cache_id;

  if (!@is_readable($file)) {
    return false;
  }

  if ($lifetime == -|| (filemtime($file) + $lifetime) > time()) {
    if (!$fp = @fopen($file'rb')) {
        return false;
    }

    $data = @fread($fpfilesize($file));
    @fclose($fp);

    if (defined('PRINT_CACHE_MESSAGES') && PRINT_CACHE_MESSAGES == 1) {
      echo "Cache file '$cache_id' <span style='color:green'>used</span><br>";
    }

    // Replace session ids
    global $site_sess;
    $replace $site_sess->mode == 'cookie' '' '\1\2'.SESSION_NAME.'='.$site_sess->session_id;
    $data preg_replace(
      '#([\?|&])+(amp;)?%%%SID%%%#',
      $replace,
      $data
    
);

    return $data;
  }

  if (defined('PRINT_CACHE_MESSAGES') && PRINT_CACHE_MESSAGES == 1) {
    echo "Cache file '$cache_id' <span style='color:purple'>expired</span><br>";
  }

  return false;
}

function 
save_cache_file($cache_id$data) {
  global $cache_enable$cache_lifetime$cache_path;

  if (!$cache_enable) {
    return false;
  }

  $file $cache_path '/' $cache_id;

  if ($fp = @fopen($file'wb')) {
    // Replace session ids
    global $site_sess;
    $data str_replace(
      SESSION_NAME.'='.$site_sess->session_id,
      '%%%SID%%%',
      $data
    
);

    @flock($fpLOCK_EX);
    @fwrite($fp$data);
    @flock($fpLOCK_UN);
    @fclose($fp);

    if (defined('PRINT_CACHE_MESSAGES') && PRINT_CACHE_MESSAGES == 1) {
      echo "Cache file '$cache_id' <span style='color:red'>stored</span><br>";
    }

    return true;
  }

  @fclose($fp);

  return false;
}

function 
delete_cache_file($cache_id) {
  global $cache_enable$cache_lifetime$cache_path;

  if (defined('PRINT_CACHE_MESSAGES') && PRINT_CACHE_MESSAGES == 1) {
    echo "Cache file '$cache_id' <span style='color:red'>deleted</span><br>";
  }

  return @unlink($cache_path '/' $cache_id);
}

function 
delete_cache_group($group) {
  global $cache_enable$cache_lifetime$cache_path;

  $handle = @opendir($cache_path); 

  while ($file = @readdir($handle)) { 
    if (is_dir($file) || $file{0} == ".") {
      continue;
    }

    if (strpos($file$group) === 0) {
      unlink($cache_path '/' $file);
    }
  }

  if (defined('PRINT_CACHE_MESSAGES') && PRINT_CACHE_MESSAGES == 1) {
    echo "Cache group '$group' <span style='color:red'>deleted</span><br>";
  }
}

function 
clear_cache() {
  global $cache_enable$cache_lifetime$cache_path;

  $handle opendir($cache_path);

  while ($file = @readdir($handle)) {
    if (is_dir($file) || $file{0} == ".") {
      continue;
    }

    unlink($cache_path '/' $file);
  }
}

?>



Step 2
Open global.php
Find:
Code: [Select]
@include(ROOT_PATH.'config.php');
Add above:
Code: [Select]
//-----------------------------------------------------
//--- Cache -------------------------------------------
//-----------------------------------------------------
include(ROOT_PATH.'includes/cache_utils.php');
// Initialize cache configuration
define('PRINT_CACHE_MESSAGES', 0); //show some debug messages
$cache_enable = 1;
$cache_lifetime = 3600; // 1 hour
$cache_path = ROOT_PATH.'cache';

$cache_page_index = 1; //index.php
$cache_page_categories = 1; //categories.php
$cache_page_top = 1; //top.php
//End Initialize cache configuration



Step 3
Open index.php
Find:
Code: [Select]
//-----------------------------------------------------
//--- Show Categories ---------------------------------
//-----------------------------------------------------

Add above:
Code: [Select]
$cache_id = create_cache_id(
  'cat.page.index',
  array(
    $user_info[$user_table_fields['user_id']],
    isset($user_info['lightbox_image_ids']) ? substr(md5($user_info['lightbox_image_ids']), 0, 8) : 0,
    $config['template_dir'],
    $config['language_dir']
  )
);
if (!$cache_page_index || !$content = get_cache_file($cache_id)) {
// Always append session id if cache is enabled
if ($cache_page_index) {
  $old_session_mode = $site_sess->mode;
  $site_sess->mode = 'get';
}

ob_start();


Step 3.1
Find:
Code: [Select]
$site_template->print_template($site_template->parse_template($main_template));(if u have more then one instance of this line, then find the last one, close to the end of the file)

Add below:
Code: [Select]
$content = ob_get_contents();
ob_end_clean();

if ($cache_page_index) {
  // Reset session mode
  $site_sess->mode = $old_session_mode;
  save_cache_file($cache_id, $content);
}
} // end if get_cache_file()
echo $content;



Step 4
Open categories.php
Find:
Code: [Select]
//-----------------------------------------------------
//--- Show Categories ---------------------------------
//-----------------------------------------------------

Add above:
Code: [Select]
$cache_id = create_cache_id(
  'cat.page.categories',
  array(
    $user_info[$user_table_fields['user_id']],
    $cat_id,
    $page,
    $perpage,
    isset($user_info['lightbox_image_ids']) ? substr(md5($user_info['lightbox_image_ids']), 0, 8) : 0,
    $config['template_dir'],
    $config['language_dir']
  )
);

if (!$cache_page_categories || !$content = get_cache_file($cache_id)) {
// Always append session id if cache is enabled
if ($cache_page_categories) {
  $old_session_mode = $site_sess->mode;
  $site_sess->mode = 'get';
}

ob_start();


Step 4.1
Find:
Code: [Select]
$site_template->print_template($site_template->parse_template($main_template));(if u have more then one instance of this line, then find the last one, close to the end of the file)

Add below:
Code: [Select]
$content = ob_get_contents();
ob_end_clean();

if ($cache_page_categories) {
  // Reset session mode
  $site_sess->mode = $old_session_mode;

  save_cache_file($cache_id, $content);
}

} // end if get_cache_file()

echo $content;



Step 5
Open top.php
Find:
Code: [Select]
include(ROOT_PATH.'includes/page_header.php');
Add below:
Code: [Select]
$cache_id = create_cache_id(
  'top',
  array(
    $user_info[$user_table_fields['user_id']],
    $cat_id,
    $config['template_dir'],
    $config['language_dir']
  )
);

if (!$cache_page_top || !$content = get_cache_file($cache_id)) {
if ($cache_page_top) {
  // Always append session id if cache is enabled
  $old_session_mode = $site_sess->mode;
  $site_sess->mode = 'get';
}

ob_start();


Step 5.1
Find:
Code: [Select]
$site_template->print_template($site_template->parse_template($main_template));(if u have more then one instance of this line, then find the last one, close to the end of the file)

Add below:
Code: [Select]
$content = ob_get_contents();
ob_end_clean();

if ($cache_page_top) {
  // Reset session mode
  $site_sess->mode = $old_session_mode;

  save_cache_file($cache_id, $content);
}

} // end if get_cache_file()

echo $content;



Step 6
Create a new folder cache[/color] in your 4images root directory. Make sure its writible (CHMOD 777)


Step 7 (optional)

Create a new file admin/plugins/clear_cache.php (create plugins folder if u dont have it yet) with the folowing code inside:
 
Code: [Select]
<?php // PLUGIN_TITLE: Clear Cache 
/************************************************************************** 
 *                                                                        * 
 *    4images - A Web Based Image Gallery Management System               * 
 *    ----------------------------------------------------------------    * 
 *                                                                        * 
 *             File: clear_cache.php                                      * 
 *        Copyright: (C) 2002 Jan Sorgalla                                * 
 *            Email: jan@4homepages.de                                    * 
 *              Web: http://www.4homepages.de                             * 
 *    Scriptversion: 1.7.1                                                * 
 *                                                                        * 
 *    Never released without support from: Nicky (http://www.nicky.net)   * 
 *                                                                        * 
 ************************************************************************** 
 *                                                                        * 
 *    Dieses Script ist KEINE Freeware. Bitte lesen Sie die Lizenz-       * 
 *    bedingungen (Lizenz.txt) f?tere Informationen.                 * 
 *    ---------------------------------------------------------------     * 
 *    This script is NOT freeware! Please read the Copyright Notice       * 
 *    (Licence.txt) for further information.                              * 
 *                                                                        * 
 *************************************************************************/ 

$nozip 1
define('IN_CP'1); 
$root_path "./../../"
define('ROOT_PATH'$root_path); 
require(
ROOT_PATH.'admin/admin_global.php'); 

if (
$config['language_dir'] == 'deutsch') { 
  $lang_clear_cache   'Cache leeren'
  $lang_clear_confirm 'Wollen Sie das Cache-Verzeichnis leeren (%s)?'
  $lang_clear_success 'Cache-Verzeichnis geleert'
} else { 
  $lang_clear_cache   'Clear Cache'
  $lang_clear_confirm 'Do you want to clear the cache directory (%s)?'
  $lang_clear_success 'Cache directory cleared'


show_admin_header(); 

if (
$action == "clearcache") { 
    @set_time_limit(0); 
    clear_cache(); 
    $msg $lang_clear_success


if (
$msg != "") { 
    printf("<b>%s</b>n"$msg); 


show_form_header($HTTP_SERVER_VARS['PHP_SELF'], "clearcache"); 
show_table_header($lang_clear_cache); 
show_description_row(sprintf($lang_clear_confirmrealpath($cache_path))); 
show_form_footer($lang['submit'], ""); 


show_admin_footer(); 
?>

Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline ID25

  • Full Member
  • ***
  • Posts: 125
    • View Profile
Re: [MOD] Cache System for 4images v1.7
« Reply #1 on: August 15, 2005, 12:51:42 AM »
4images working very slowly, when many images loaded to the base,
without this MOD - site not working

~ 7000 IP per day
~ 23000 files in database
Server: Pentium4 3.2mhz, Ram: 1Gb, linux
only one 4images script on this server!

I think, there is BUG on 4images..
crashing mysql this script

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: [MOD] Cache System for 4images v1.7
« Reply #2 on: August 15, 2005, 03:28:23 AM »
Well, since v1.7 was using mysql to store sessions, its no wonder that mysql couldnt handle so many requests.
I've experienced 7000+ unique visitors per day ones, and can tell you, the pages were opening in about 2-3 minutes! At first I thought someone is hacking the server...(server P4 3.0Ghz HT, 2Gb RAM on Linux FC3 smp)

P.S. please stop looking for bugs in v1.7 ;)
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline JensF

  • Addicted member
  • ******
  • Posts: 1.028
    • View Profile
    • http://www.terraristik-galerie.de
Re: [MOD] Cache System for 4images v1.7
« Reply #3 on: August 15, 2005, 03:36:37 PM »
What do this Mod???
Mit freundlichem Gruß
Jens Funk



-> Sorry for my bad English <-

Offline ID25

  • Full Member
  • ***
  • Posts: 125
    • View Profile
Re: [MOD] Cache System for 4images v1.7
« Reply #4 on: August 15, 2005, 07:19:48 PM »
What do this Mod???
cashing your site
from version 1.7.1 this mod installed

Offline ID25

  • Full Member
  • ***
  • Posts: 125
    • View Profile
Re: [MOD] Cache System for 4images v1.7
« Reply #5 on: August 15, 2005, 07:40:38 PM »
At first I thought someone is hacking the server...

NO someone hacking server, but 4images(1.7 and 1.7.1) without this MOD working bad(page opening ~2-3min. or not responding)
i think there must be big bug somewhere in this script...
maybe need remove ~5 tables from mysql or something... i dont know, there you are professional with PHP scripting.

other scripts for images work faster and no crashing mysql, why? maybe you know that..

If you know why, maybe you can post there MOD or something, who made better for speed?

Offline ID25

  • Full Member
  • ***
  • Posts: 125
    • View Profile
Re: [MOD] Cache System for 4images v1.7
« Reply #6 on: August 15, 2005, 11:26:29 PM »
V@no: exuse me, but there is true, that 4images working very slow..

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: [MOD] Cache System for 4images v1.7
« Reply #7 on: August 15, 2005, 11:50:25 PM »
v1.7 is outdated and u cant expect much out of it. Install v1.7.1 then we'll talk if it slow or not ;)
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline ID25

  • Full Member
  • ***
  • Posts: 125
    • View Profile
Re: [MOD] Cache System for 4images v1.7
« Reply #8 on: August 16, 2005, 12:38:50 AM »
v1.7 is outdated and u cant expect much out of it. Install v1.7.1 then we'll talk if it slow or not ;)
at this time i have 1.7.1 installed in my site(working slow), but planning to install 1.7(removing now bugs updeiting with mods)
because on this version many mods and etc

Offline ID25

  • Full Member
  • ***
  • Posts: 125
    • View Profile
Re: [MOD] Cache System for 4images v1.7
« Reply #9 on: August 16, 2005, 12:40:24 AM »
on version 1.7.1 if i not caching any pages --> script crashing mysql
on version 1.7 i can install caching mod too.

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: [MOD] Cache System for 4images v1.7
« Reply #10 on: August 16, 2005, 12:43:55 AM »
well, then assume u didnt get my point in my first reply...:?
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline ID25

  • Full Member
  • ***
  • Posts: 125
    • View Profile
Re: [MOD] Cache System for 4images v1.7
« Reply #11 on: August 16, 2005, 12:55:28 AM »
Hmz..
after you make caching system, 4images woking slow,
much better, but slowly :(

why others scripts for images working not slow?
mysql tables many, but working in normal mode..
and there is no need caching system..
i don't know this why :(
this script beautiful, but need something..

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: [MOD] Cache System for 4images v1.7
« Reply #12 on: August 16, 2005, 01:03:07 AM »
ok, let me repeate it for u:
4images v1.7 was using MySQL database to store and handle sessions, thats why your mysql server is getting crashed by too many connections....
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline ID25

  • Full Member
  • ***
  • Posts: 125
    • View Profile
Re: [MOD] Cache System for 4images v1.7
« Reply #13 on: August 16, 2005, 01:07:12 AM »
i understand this, but i repeating.. this problem with 1.7.1 too :cry:
site working slowly, all caching are "on", but..

Offline ID25

  • Full Member
  • ***
  • Posts: 125
    • View Profile
Re: [MOD] Cache System for 4images v1.7
« Reply #14 on: August 16, 2005, 07:26:59 PM »
Maybe you can post there, how i can change in 1.7 version
that MySQL database to NOT store and handle sessions ?

There is good feature to all 4images administrators, because
if you installed 20-50 MODS, you can't rewrite all php to 1.7
if you want new 1.7.1 script.

Thank you V@no for all helps and wonderfull script