Page caching mod for sNews 1.7

Comments (33)

Hi again, as promised, I have completed my caching mod for sNews 1.7, and here is my initial release.

What the hell is caching and do I need it?
Well, basically what we're doing with this mod is saving a page's output into a raw HTML document, and we serve that page until there's a change that would require hitting the database again. If your site is like mine and gets a few comments a day and you're adding a few articles a day, then yes, this mod would likely help you considerably.

Uh, what? I need an example please.
Fair enough, say you have an article with 20 comments, every time someone visits that page, you're querying the database for the article, all the comments, as well as anything else on the page (site title, description, categories list, pages, recent comments list, recent articles, my related posts mod, etc). With this mod the first time someone calls the page all information would be pulled from the database and the resulting HTML will be saved into your "cache" folder, and then subsequently served to users after that, up until a new comment is posted, an article is added/edit/deleted, etc.

Sound good? Great, this is another super easy mod to implement, first download the package and unzip.

VERSION history
[1.0.3] - added code to strip failed comments
[1.0.2] - fixed the check for approved comments, should properly empty the cache now
[1.0.1] - added "remember me" function for my sticky forms mod
[1.0.0] - initial release

Now modify the following variables inside the mod.cache.php file. They are self-explanatory, and the comments inside the file should assist you.

$cache_dir = '/home/mysite/cache';
$cache_keep_days = '7';
$cache_polls = '0';

Cool, now upload mod.cache.php to the same directory as your snews.php and index.php files. Create the "cache" directory you specified now as well, if you haven't already.

Now open your index.php file add add the following at the top, just below your snews.php include (and poll include if you have it), like the following;

<?php
include('snews.php'); 
include('mod.cache.php');
//include("mod_poll/poll_functions.php"); //uncomment if you're using my polls mod
startCache();
?>

Now add this to the very bottom of your index.php file;

<?php
outputCache();
?>

Yeah, that's it, upload index.php and browse your site when you're not logged in, you'll see your cache directory start filling up and notice your MySQL database getting only one query per page once cached. Edit an article or post a comment and watch the cache folder empty.

bookmark / share this: Bookmark and Share
rated 4.33/5 (6 votes)


33 comments

Add a new comment »

Poppoll Poppoll said:
Dec 12th, 2009 at 9:01 am

Thanks Matt
Added.
PP


Sven / Philippe Sven / Philippe said:
Dec 14th, 2009 at 3:56 am

uh oh... message was not sent.
Another try:

Hi Matt
Nice job you've done with that Mod.
It works as the files are created but I'm not sure it's gonna work as of on 1and1 shared servers.
I'm receiving a message sayin it's not allowed there. :-D
Gonna see if it can be fixed by a php.ini.


slemborg slemborg said:
Dec 14th, 2009 at 6:14 am

Another mod, i'm gonna try this out later, not that my site get's overrun, but from time to time, snews kinda "burps".


Matt Matt said:
Dec 14th, 2009 at 7:26 am

@ Sven, does 1and1 run PHP in safe mode or something?

@ Slemborg, I hear you, my site doesn't really either, but it doesn't really hurt to cache content when no DB connection is really needed.


Matt Matt said:
Dec 14th, 2009 at 8:50 am

Oops, I left out the bit to set the "remember me" checkbox to checked or unchecked for my sticky comments mod, that has been updated now, v1.0.1.


Sven / Philippe Sven / Philippe said:
Dec 14th, 2009 at 10:33 am

I suppose Matt.
They don't give any information.
.cache files are created but can't be read.


Matt Matt said:
Dec 14th, 2009 at 10:40 am

That's interesting, what are the cache folder and .cache file permissions?

You can check and see if you're running PHP in safe mode, just create a file called phpinfo.php, put this in it;

<?php
echo phpinfo();
?>

Then upload it and check if safe_mode is Off or On.

BTW, safe_mode is being removed from PHP 6, thankfully.


Sven / Philippe Sven / Philippe said:
Dec 14th, 2009 at 11:09 am

Made it working by moving the outputCache() call in another place. Don't ask why now it works now. No please. :-D


Sven / Philippe Sven / Philippe said:
Dec 14th, 2009 at 11:25 am

Uh oh... I suppose your cache Mod is on here and new comments are displayed.

Try to post a comment there: http://on-air.hiseo.fr/fatras/temps-de-chargement-et-referencement/.

New comments aren't displayed.


Matt Matt said:
Dec 14th, 2009 at 11:43 am

Sven,

Yes, I'm running it on here no problem.

It looks like the cache isn't being emptied on new comments, do you have comment moderation enabled or do you allow new comments to post immediately?

Did you verify you're not in safe mode?


Sven / Philippe Sven / Philippe said:
Dec 14th, 2009 at 11:54 am

Yeap safe mode is off and new comments are posted immediatly.


Matt Matt said:
Dec 14th, 2009 at 12:05 pm

Oh crap, I was checking the wrong setting!

Change

    if (s('mail_on_comments') == 'off') {
        $moderateOff = true;
    }

to

   if (s('approve_comments') != 'on') {
        $moderateOff = true;
   }

I will fix and update package now.


Sven / Philippe Sven / Philippe said:
Dec 14th, 2009 at 12:11 pm

and now it works!


Matt Matt said:
Dec 14th, 2009 at 12:12 pm

Haha, thanks for being the guinea pig, as always! :)


toolman toolman said:
Dec 30th, 2009 at 6:40 pm

This modification is great for me. Significantly reduce the burden on the database.
Unfortunately, my site is running statistics (chCounter) and
[MOD] Page view counter for sNews 1.7 only (http://snewscms.com/forum/index.php?topic=8089.0)
Is there any easy way to update only those data?
For example, everything between
&lt;span class="statV"&gt;
and
&lt;/span&gt;
will be updated.

I should be grateful for your response.

P.S.
Translation to English: translate.google.pl


Matt Matt said:
Dec 30th, 2009 at 8:55 pm

Hi toolman,

Yes, it's easy enough to do, provided you have the functions that output the data. If you look at the code inside mod.cache.php, you can see where we strip the dynamic content with a preg_replace, and then update it with the correct content, whether it's the user's IP or the show_poll function.

To do this, you need to uniquely identify the content you're replacing. Say, for example, the page counter, wrap the number in HTML comments, like

<!-- START_COUNTER -->
php function that outputs the counter code
<!-- END_COUNTER -->


then strip it inside mod.cache.php for storage;

$cache = preg_replace('/<!--\sSTART_COUNTER\s-->(.*?)<!--\sEND_COUNTER\s-->/s', '<!--COUNTER-->', $cache);

and finally add the counter back in when the cached page is served;

$cache = preg_replace('/(<!--COUNTER-->)/e', 'counterFunction($variables)', $cache);


Because you're not querying the article database, you'll need to write a separate function to get the counter, as shown with the "counterFunction" I added above.

Does that make sense?


pATRIC pATRIC said:
Jan 25th, 2010 at 6:55 am

Making a day of it ;) ... Third Matt-Mod now. this one is awsome... continuing for more goodies, hehe.. Thanks, Matty.


Tatsu Tatsu said:
Mar 29th, 2010 at 12:42 pm

Howdy,

damnit once again me :/ sorry Matt, but I dont see nothing :/

<?php
include('snews.php');
include('mod.cache.php');
include("mod_poll/poll_functions.php"); //uncomment if you're using my polls mod
startCache();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

and added the output

<div id="footer">
<p>This site is powered by <a href="#" title="zenEngine" onclick="target='_blank';">zenEngine</a> | <?php login_link(); ?></p>
<?php
outputCache();
?>
</div>

Nothing is displaying...but I see the cashe folder has something inside it :( Whats wrong here :S


Matt Matt said:
Mar 29th, 2010 at 12:50 pm

Hi Tatsu, Not sure if this will really help, but the outputCache function should be called at the very bottom of your index.php, after all other HTML.


Tatsu Tatsu said:
Mar 29th, 2010 at 3:03 pm

Howdy, Ive putted the code at the end, after the </html>, and yet nothing? Does it even show anything when you are not logging in as admin? xD

Damnit... :D


Matt Matt said:
Mar 29th, 2010 at 3:22 pm

Tatsu, I just looked at your site real quick and I'm seeing the cache timestamp at the bottom.

Does it only give you a blank page on the first view? What browser are you using?


Tatsu Tatsu said:
Mar 29th, 2010 at 3:25 pm

Im using, Firefox...and no, I dont get a blank page at first use. Really dont know whats wrong?! Do you have a messenger? :D would be faster, but if you have time and if you wish afkors! :D

enj0y matt


Matt Matt said:
Mar 29th, 2010 at 3:32 pm

I'm about to leave work, shoot me a message through the contact form and I'll email you back later tonight if I get a chance.


toolman toolman said:
Apr 29th, 2010 at 8:44 am

Hello. It's me again.
I use a modified "Page view counter for sNews 1.7" (http://snewscms.com/forum/index.php?topic=8089.0)
Unfortunately, I have a problem with the refresh counter.
I added a function StartCache() line:
$cache = str_replace('<!--COUNTER-->', $views, $cache);
and to OutputCache() line:
$cache = preg_replace('/<!--\sCOUNTER_START\s-->(.*?)<!--\sCOUNTER_END\s-->/s', '<!--COUNTER-->', $cache);

Cache file now contains a <!--COUNTER--> , but unfortunately does not display the counter in your browser.
What am I doing wrong?


Matt Matt said:
Apr 29th, 2010 at 8:49 am

Toolman,

Looks like there's no spaces in the counter comment, i.e.

    ↓       ↓
<!-- COUNTER -->


toolman toolman said:
Apr 29th, 2010 at 4:52 pm

It does not matter if there are spaces if they do not. Not working. Even the comments are not working. I get a notification e-mail about new comments, but the IP location is empty, and the admin panel no comments for approval.
This is my second approach to this modification and the subsequent failure: /
Too bad, unless the surrender.


Matt Matt said:
Apr 29th, 2010 at 7:46 pm

Toolman, how is $views being set? If it's not explicitly called on a cached file, it won't exist, because the articles function is never called, as it's served directly from cache.

As for the IP address, I believe there is an error in the default snews.php because every time I've installed it, there has been no IP logged.

Not sure why unapproved comments aren't showing up, this mod shouldn't affect that.

Yeah, a lot of people seem to have trouble with this mod, I'm not sure why, I'm unable to produce any of the same errors as others, but this approach does make it difficult to modify and produce dynamic content, I've even stopped using it on my own site recently because I keep adding & changing things.


toolman toolman said:
Apr 30th, 2010 at 1:34 am

Even when turned off approving comments, do not they write in the database, although the notification comes.
If you wish, I will give you access to my test installation.

Regards.


Matt Matt said:
Apr 30th, 2010 at 9:38 am

Toolman, you can send me the snews.php, index.php, and mod files here;

I will try to have a look some time to see if I can figure it out, but I can't make any promises, I simply don't have much free time available.


Tatsu Tatsu said:
May 30th, 2010 at 4:37 pm

Howdy Matt,

once again me =) long time no see :P. I would only ask something. In Firefox 3.6 I dont see <-CACHE->, but in Internet Explorer I get that output, when I see the page code =) its a little bit strange isnt it? have the same problems with bookmarks hack :P what can be here wrong?


Matt Matt said:
Jun 1st, 2010 at 8:05 am

Hi Tatsu,

The software is browser non-specific, so I wouldn't think it could be anything with sNews or any mod.

Sounds like Firefox is caching the pages locally, but I couldn't say for sure.


Tatsu Tatsu said:
Jun 7th, 2010 at 1:48 pm

Yo Matt,

really dont have any idea whats wrong? In the previus version of FF worked all OK, but now, nothing works :S. Don`t know whats the issue.

Hope I will find out =)

Grettz


Matt Matt said:
Jun 9th, 2010 at 10:07 am

I'm not sure Tatsu, apparently a lot of people have had problems with this mod.

It works fine on my stock sNews test site, but once you start adding other hacks & mods, it seems to break things.



Write a comment

* = required field

:

:

:

:

You may insert urls in plain text, urls will be automatically linkified for trusted users and on seasoned posts only. All first comments are moderated, so use your email if you want to be remembered.


Back to top