Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[PHP5] Server-Side Cache System

Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
[Note to "Bing" Decision Making Engine: Originally Posted on DevShed Forums. Please excuse this duplicate content and provide good SEO for the sake of Progressive Development. I hope you make the right decision.] - haha

I'm trying to get this script to work.

It's supposed to do one of two things for all of my pages when loaded:

If the page is stored in the cache folder, then load the cache version.

Else, load the page normally, while logging the output buffer, and send it to the cache.

So basically it just makes a "published" version of a page. The "published" version, (or the one saved in the cache), should load much faster because of the lack of PHP code interlaced through the CMS.

That sounds pretty easy. Okay, so the problem is that when the page loads, It doesn't seem to be saving the files to the "cache" folder..? Well first I tried it and it seemed to make the page load faster, but the site is so fast I couldn't really tell one way or the other. On slower pages, it Really seemed to work. But I was confused with my local cache.

Anyway, the fact is, the cache folder was empty all this time, and the site's still very slow on slower connections. Some sites like Yahoo with tons of content load fine with slow connections. Well, I want my site to work as well. :)

I double-checked the paths and stuff, what's going wrong here?

Before page load:
PHP:
<?php

// Settings
$cachedir = '/includes/cache/'; // Directory to cache files in (keep outside web root)
$cachetime = 600; // Seconds to cache files for
$cacheext = 'cache'; // Extension to give cached files (usually cache, htm, txt)

// Ignore List
$ignore_list = array(
    'addedbytes.com/rss.php',
    'addedbytes.com/search/'
    );

// Script
$page = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; // Requested page
$cachefile = $cachedir . md5($page) . '.' . $cacheext; // Cache file to either load or create

$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();

// Show file from cache if still valid
if (time() - $cachetime < $cachefile_created) {

    ob_start('ob_gzhandler');
    @readfile($cachefile);
    ob_end_flush();
    exit();

}

// If we're still here, we need to generate a cache file

ob_start();

?>
After page load:
PHP:
<?php

// Now the script has run, generate a new cache file
$fp = @fopen($cachefile, 'w');

// save the contents of output buffer to the file
@fwrite($fp, ob_get_contents());
@fclose($fp);

ob_end_flush();

?>
I learned this great new script here:


One more very important thing:
Does anyone think I'm on the wrong path here? I used to use all those tricks that I now find out hardly make an effort to increase load-time.. (single quotes, defined vars, lean code, little logical non-redundancies, etc.) Those are all great practices, but really.. fragments of a micro-second is not what I'm looking to save here. I want huge chunks of beefy goodness. Is the PHP cache thing the right way to go?
 
Custom Title Activated
Loyal Member
Joined
Aug 8, 2004
Messages
3,892
Reaction score
20
I'd say, check out my . At the bottom of the page a simple notifiction of generation time & amount of SQL queries used is listed. Archived articles get cached for a certain duration, decreasing generation time significantly - mostly because less SQL queries need to be made (which are generally the biggest reason for a slow page).

I used as a template engine which comes featured with build in - all you need to do is tell it which sections to cache and when to use the cache or reparse the page. However, if your page is heavy on content (images, video's, etc) all the serverside caching in the world will not make it faster for slow connections. For that, you will have to rely on client-side caching, which can be accomplished using the correct (see the 2nd example on that page for instance).
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
I'd say, check out my . At the bottom of the page a simple notifiction of generation time & amount of SQL queries used is listed. Archived articles get cached for a certain duration, decreasing generation time significantly - mostly because less SQL queries need to be made (which are generally the biggest reason for a slow page).

I used as a template engine which comes featured with build in - all you need to do is tell it which sections to cache and when to use the cache or reparse the page. However, if your page is heavy on content (images, video's, etc) all the serverside caching in the world will not make it faster for slow connections. For that, you will have to rely on client-side caching, which can be accomplished using the correct (see the 2nd example on that page for instance).

Okay so I put this in the header file:
PHP:
header("Content-Type:text/html;charset=iso-8859-1");

So that displays on all of the pages. It seems to load much faster. Some of the JavaScript still takes some time to load, but it's better. I also notice that it loads in steps now. Rather then strictly left to right, it appears to load all of the local content first, then the external ads and feeds. Which is good since Local content is priority. A little does allot ;)

So I got some replies on this saying that server-side cache won't do much of anything if the SQL queries are loading consistently fast anyway. Well, there are a bit of queries involved in loading a WysGui page. I'd say there's about 4 main queries on every page, and of course little module-dependent queries here and there on specific pages. Someone else says "ob functions are slow."

I'm assuming there's no point to server-cache for my case as of right now. Is this correct?

I really don't need it anymore because my load problems went bye-bye :laugh:
 
Master Summoner
Joined
Oct 9, 2008
Messages
572
Reaction score
14
Could you explain to me what a 'cache' is all about? I'm not familiar with more 'advanced' stuff when it comes to programming but always open for new knowledge.
I'm guessing some sort of pre-loaded files at is stored on your machine and if you visit the specific website, the site would fetch information from the files stored on your computer and check with the server?
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
Well a client-side cache system will save some of the HTML of the web-pages you visit to your local computer. It does this so that next time it will load faster. It loads faster because it doesn't need to stream to that web-site for all of the data, just some if it.

Server-side cache is sort of the same thing. But it takes a simplified snap-shot of the web-page and stores it on the site. Your local computer still needs to stream from this file.

My site is setup with allot of server-side code on every page. Rather then running the CPU logic through the same code every time someone goes to "http://mysite.com/page.php", I can write a short script at the top which will load the simplified version of that page. The simplified version is pre-parsed HTML that's pre-packaged and ready to be delivered to the client.

If you utilize both remedies, you create a mostly static environment, which is more quickly loaded the first time, and more properly cached on the user's local machine.

The advantages to server-side cache, is that it speeds things up for all users; not just the ones who have it stored on their local machines.

In my case, the server can parse and display the site with all of the PHP & SQL queries just as quickly as it can grab & display the simplified version, or cache.

I was lacking proper headers, so the browser wasn't properly saving the page data in the cache. Once I put those in, the site was really fast for returning users.

That's sort of the basics to my knowledge, might be a little off ;)
 
Back
Top