Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Caching mod for sNews1.7  (Read 196 times)

sibas

  • Sr. Member
  • ****
  • Karma: 23
  • Posts: 451
    • www.simply4all.net
Caching mod for sNews1.7
« on: December 16, 2012, 02:19:01 PM »

Hello all, I was trying to use [MOD] Page caching mod for sNews
Unfortunately does not work as I want
So I try to make another one  :)

Lets start
Make one folder cache outside of web root i.e. C:\xampp\htdocs\cache
Open index.php and after include('snews.php');
Add
Code: [Select]
<?php
global $_ID;
$cachedir '../cache/'// Folder to cache files outside from web root
$cachetime 60// 4 minutes to cache files.
$page 'http://' $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; // Requested page
// $page = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'].serialize($_GET).serialize($_POST); // need more test!
$cachefile $cachedir.md5($page).'#'.$_ID.'#'.'.cache'// Cache file, load or create

if (_ADMIN) { // For ADMIN don't cache anything
    
if (_ADMIN && isset($_GET['action'])) { // delete all cache files when post new article/page or update/delete
        
$cfile '';
        foreach (
glob($cachedir.'/*.cache') as $cfile) {
        
unlink($cfile);
clearstatcache();
        }
    }
}else{
$ignore_list = array( // files to ignore from cashing
'/some-article/',
'/archive/'
);
$ignore_page false;

for (
$i 0$i count($ignore_list); ++$i) {
$ignore_page = (strpos($page$ignore_list[$i]) !== false) ? true $ignore_page;
}

$cachefile_created = ((file_exists($cachefile)) and ($ignore_page === false)) ? filemtime($cachefile) : 0;
clearstatcache();

if (isset(
$_POST['text']) && $cachefile_created) { // if user post a comment delete the file cache
unlink($cachefile);
$idfile '';
    foreach (glob($cachedir.'/*#'.$_ID.'#.cache') as $idfile) { // if user post a comment when is in comment page i.e c_2 delete all files cache with specific article id
        
unlink($idfile);
clearstatcache();
        }
}

if (isset(
$_POST['search_query']) && $cachefile_created) { // if user use a search delete the file cache
unlink($cachefile);
}

if (isset(
$_POST['contactform']) && $cachefile_created) { // if user send an email delete the file cache
unlink($cachefile);
}

if ((
file_exists($cachefile)) and time() - $cachetime $cachefile_created){ // read file from cache if is valid

//readfile($cachefile); // use this to read uncompressed cache file
readgzfile($cachefile); // use this to read compressed cache file
exit();
}
ob_start();
}
?>

In the end of this file after </html> add
Code: [Select]
<?php
if (_ADMIN) { // ADMIN, text, search_query, contactform, don't write anything in cache.
}elseif (isset($_POST['text'])) {
}elseif (isset(
$_POST['search_query'])) {
}elseif (isset(
$_POST['contactform'])) {
}else{
if (
$ignore_page === false){ // if we have files to ignore
//echo '<!-- cg '.date('H:i').' -->';
echo '<!-- CACHE: '.date('r'time()).' -->';
//use this for uncompressed files
/*
$fp = @fopen($cachefile, 'w');
@fwrite($fp, ob_get_contents());
@fclose($fp);
*/

// use this for compressed files
$fp gzopen($cachefile,'w1'); // write w1 to w9 compress level
gzwrite($fpob_get_contents());
gzclose($fp);  
}
ob_end_flush();
}
?>

thats all  ;)
« Last Edit: December 16, 2012, 02:37:16 PM by sibas »
Logged

Keyrocks

  • Doug
  • ULTIMATE member
  • ******
  • Karma: 449
  • Posts: 6019
  • Semantically Challenged
    • snews.ca
Re: Caching mod for sNews1.7
« Reply #1 on: December 17, 2012, 12:48:26 AM »

Karma+ for sharing your solution Sibas.  8)
Logged
Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU