4images Forum & Community
4images Modifications / Modifikationen => Mods & Plugins (Releases & Support) => Topic started by: V@no 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:<?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 == -1 || (filemtime($file) + $lifetime) > time()) {
if (!$fp = @fopen($file, 'rb')) {
return false;
}
$data = @fread($fp, filesize($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($fp, LOCK_EX);
@fwrite($fp, $data);
@flock($fp, LOCK_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:@include(ROOT_PATH.'config.php');
Add above://-----------------------------------------------------
//--- 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://-----------------------------------------------------
//--- Show Categories ---------------------------------
//-----------------------------------------------------
Add above:$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:$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:$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://-----------------------------------------------------
//--- Show Categories ---------------------------------
//-----------------------------------------------------
Add above:$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:$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:$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:include(ROOT_PATH.'includes/page_header.php');
Add below:$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:$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:$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:
<?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_confirm, realpath($cache_path)));
show_form_footer($lang['submit'], "");
show_admin_footer();
?>
-
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
-
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 ;)
-
What do this Mod???
-
What do this Mod???
cashing your site
from version 1.7.1 this mod installed
-
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?
-
V@no: exuse me, but there is true, that 4images working very slow..
-
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 ;)
-
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
-
on version 1.7.1 if i not caching any pages --> script crashing mysql
on version 1.7 i can install caching mod too.
-
well, then assume u didnt get my point in my first reply...:?
-
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..
-
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....
-
i understand this, but i repeating.. this problem with 1.7.1 too :cry:
site working slowly, all caching are "on", but..
-
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
-
well, see, v1.7.1 is not only optimized with session handlers, it also has new template engine, and the entire code was little bit tweaked and optimized.
-
is there a drastic speed difference after installing this mod? :?:
-
i tested this MOD on 1.7ver. with 7000 IP per day, nothing good..
+50% speed after you install 1.7.1 only.
-
Dear All,
is it normal, that the cache files have 0 Byte?
The files are saved by me in folder "cache" but they have all 0 Byte...
The gallery is also not really quicklier.
I have already check all my steps and can´t find a mistake...
Sorry for my bad english and thanks for your help.
André
-
Hi there,
is there a way to ignore the user_loginform.html from the cache???
It where better for the PM Mod....
-
sorry .. but what does this MOD do ?
what do you mean by Cache system for 4images
-
do you have 4images 1.71?
then you don´t need this Mod... (o:
Read the Cache.english.txt in the Docs Folder...
-
No I don't .. I have v1.7 only ..
and can't change it coz I have lots of changes inside it like MODs ..
-
And you don´t now caching? 8O
Taken from the Cache.english.txt
The caching system saves results generated by the
script as static html files to the disk.
The next time a visitor requests a page, the result can be
fetched from the cache and must not be generated again.
This also has some "disadvantages": Dynamic data such as
hits, ratings, random images and the "who's online" list
will also be cached. This dynamic data never changes
until a cache file is invalidated and generated again.
-
I don't untill you've told me :D
thanks for your reply
I am caching now ;)
-
After re-do the step 1 i have this problem on my site. With the old cache_utils.php i have not this problem...
(http://www.terraristik-galerie.de/sonstiges/umlaut.gif)
*EDIT*
and now i have see i can´t log in and logout....i have upload the old cache_utils.php
-
As I mentioned before, I dont think this problem is related directly to this mod. Try to uninstall this mod (at least undo Step 1, 2 and 3 - asuming you see this problem on index.php, if not undo the changes in the file where the problem is)
Second, I see no such problem on your site neither in IE nor in FF.
But, I've noticed another thing that only showed in FF: there are some characters showed on top of each page. . I've experienced such myself in the past, and it turned out caused by my php editor that saved files in Unicode (UTF-8) encoding and added "BOM" identifications at the beginning of each file. Normaly any editor that supports Unicode/UTF8 does not show these characters, but PHP seems to treat them as a plane text and send them to the browser as plain text.
So, make sure you save your files in 8-bit ecoding (ISO) and not in unicode.
P.S. if you copy text from a web page that uses Unicode or UTF-8 and then paste it into notepad/wordpad and save it, it will save the file in Unicode or UTF-8 encoding.
-
Hi,
you have not seen the Problem while i have upload the old cache_utils.php.
For you i upload the new with the problems. Please look again and see the german version of the site.....
P.S. if you copy text from a web page that uses Unicode or UTF-8 and then paste it into notepad/wordpad and save it, it will save the file in Unicode or UTF-8 encoding.
i use notepad for all the files.....
-
i use notepad for all the files.....
Make sure you save files in ANSI encoding. But be carefull, you might endup with a space after each letter...
Your site is using ISO-8859-1 (Western European) encoding, yet, the text comming from the server is in Unicode with the BOM identification.
Try to use some more relible php editor (i.e. http://www.mpsoftware.dk/phpdesigner.php) ;)
-
hhmm, i don´t understand this. i have install the php editor what you say and i have open the index.php with it. i have save it and upload to my site. but no changes i can see.
i have test wordpad and the same.
i don´t understand this all....
-
Its not only the index.php its all files that you've edited recently, including cache_utils.php
-
you mean i must open all my php files with the new editor, save it and upload it and then it works????
i have test it with the index,php, cache_utils.php and the lang/main.php
*EDIT*
i have open the index.php with the notepad and i have save the file in unicode....
this is the result...
(http://www.terraristik-galerie.de/sonstiges/unicode.gif)
-
Hallo,
ich habe den Grund und die Funktion des Mods nicht verstanden, könnte mir das jemand erklären?
I not yet understood the reason and the function, could explain that someone to myself?
Gruß
michi-w.
-
Hallo,
ich habe den Grund und die Funktion des Mods nicht verstanden, könnte mir das jemand erklären?
I not yet understood the reason and the function, could explain that someone to myself?
Gruß
michi-w.
Der Mod speichert relevante Sachen in einem Ordner deines Webspace. Diese Sachen werden eine Zeitlang gespeichert und wenn ein User innerhalb dieser Zeit deine Seite wieder besucht werden diese Dinge aus diesem Ordner geholt und nicht neu geladen. Das beschleunigt den Seitenaufbau.
-
Danke!
-
would it be possible to empty the cache directory automaticly ?
Like every 24 hrs ?
Otherwise i think this directory can grow to immense proportions if you don't empty it from ACP regulary.
-
will a script for cronjob satisfy you?
-
yes, that would be very nice !
-
Ok, here is a unix command that will remove any files that were modifyed more then 24 hours ago:[qcode] find /full/local/path/to/your/4images/folder/cache -maxdepth 1 -type f -daystart -ctime +1 -delete[/qcode] but before you do that, fist test it with this command:[qcode] find /full/local/path/to/your/4images/folder/cache -maxdepth 1 -type f -daystart -ctime +1 -print[/qcode]make sure it printed the correct files ;)
-
works perfect ! thx a lot !
-
the test with the -print worked fine, but when i use -delete i get an email with :
find: invalid predicate `-delete'
i suppose this means '-delete' is not valid ?
----------------------------------------------------------------------
find /home/*****/public_html/cache -maxdepth 1 -type f -daystart -ctime +1 -delete
-
Ok, then first try this:[qcode] find /full/local/path/to/your/4images/folder/cache -maxdepth 1 -type f -daystart -ctime +1 -exec rm {} \; -print[/qcode]
If that doesnt work, then try this:[qcode] find /full/local/path/to/your/4images/folder/cache -maxdepth 1 -type f -daystart -ctime +1 | xargs rm[/qcode]
-
I apologize for this stupid question, but this is the first time I use a cron job (in cpanel, right?).
Please tell me if this path would be correct (I have the 4images installed in gallery folder on my home directory):
/public_html/gallery/cache
or it has to be like this?
full/local/public_html/gallery/cache
Addition: Ok! I got it it should have been /home/cpanelusername/public_html/gallery/chache.
How do I actually see the -print results? Is there a page where I should check? Or it should be in the cpanel?
Addition: Well, I didn't notice there is an email address you can set to receive the output. Thanks!
Thank you V@no!
-
There are two options:
1) you must have SSH access to your server and run that command from the shell.
2) use this command instead:[qcode] find /full/local/path/to/your/4images/folder/cache -maxdepth 1 -type f -daystart -ctime +1 -print > /tmp/cronoutput.txt[/qcode]
The /tmp/cronoutput.txt file must be exists before you execute the command. Once cronjob executed this command, it should save output into the /tmp/cronoutput.txt file.
-
I've tryed all your commands. Only the print ones worked for me. There is a documentation about cronjob cpanel UNIX commands syntaxes?
How do I use SSH on my site? Can you teach me please?
-
:wink:
For me, this is your code it finally worked!
find /home/cpanelusername/public_html/gallery/chache -maxdepth 1 -type f -daystart -ctime +1 -exec rm {} \; -print
Thank you V@no!
PS: any hints about documentation and SSH? (previous post)
-
Can you teach me please?
You are misstaken if you think I know alot about linux...I'm total lamer and know only what I had to do once, nothin beyond that.
-
would it be possible to empty the cache directory automaticly ?
without doing that from ACP ? and without cronjon ?
-
no
-
Installed this mod but something odd happens after a couple of tries.
my index page comes up blank. any ideas? Just a white page for the index.
-
Hello
I have 1.7.2 when i set activate Mod Limit Category Per Page doesn't work :? ,work only first page ,i see second page but when i click do ex. 2 or 3 i'm redirect to first :cry:
-
I hope you didnt install this mod on v1.7.2....this mod is only for v1.7 and v1.7.2 already comes with cache system build-in. If you have problem with build-in cache system, then start a new topic in troubleshooting section.
-
I hope you didnt install this mod on v1.7.2....this mod is only for v1.7 and v1.7.2 already comes with cache system build-in. If you have problem with build-in cache system, then start a new topic in troubleshooting section.
I now is already comes but this topic is for Cache :wink: , ok new topic http://www.4homepages.de/forum/index.php?topic=13251.new#new
-
Thankyou for this Mod V@no, You have been great as ever :)
I was able to bring down my server load from 90% to 0.90%
Thank you :mrgreen:
-
It appears Firefox browser doesn't like it with Gzip compression enabled.
You have to disable compression for it to work in your settings.
IE doesn't seem to have an issue with this.
Installed this mod but something odd happens after a couple of tries.
my index page comes up blank. any ideas? Just a white page for the index.
-
FF and any other modern browser have no issues with Gzip compression, unless the Gzip compressed page is corrupted (i.e. it has some non-compressed data before or after compressed data)
-
Interesting. 8O
When I disable this mod it works fine with compression. When I enable this MOD I get the "blank page" in FF. I disable compression it works fine. IE has no issues either way.
FF and any other modern browser have no issues with Gzip compression, unless the Gzip compressed page is corrupted (i.e. it has some non-compressed data before or after compressed data)
-
as I said, different browsers handle differently corrupted pages...so, basicaly what I'm trying to say, is that your gallery most probably produces corrupted gzip compressed data...
-
What I'm saying is without this MOD in place Gzip enabled compression works fine across all browsers.
However, when I install this MOD it doesn't. :?
as I said, different browsers handle differently corrupted pages...so, basicaly what I'm trying to say, is that your gallery most probably produces corrupted gzip compressed data...
-
can I test it?
-
rss feeds mods break too.
-
this mod helps me a lot (i am still on 1.7 within runcms 1.5)! Thanks! I had trouble with video categories. Now it's much better! Thanks a lot again.