Patrick Griffith
Patrick Griffith

Reputation: 43

Generating Static HTML Files From the Database

I have a website that is fairly database intensive, so I'm trying to cut back on database usage where possible. One place I want to do that is on each thread of my forum.

Instead of dynamically generating each thread each time it is viewed, I was thinking about generating a static version of each thread which would be overwritten any time a new post was made. Each thread would be stored in a /html/forum folder, and threads that hadn't been edited in 3 or more days would be moved to a /html/forum/archive folder (so file_exists doesn't have to search through 5,000 html files each time for the commonly viewed threads).

Here is a rough example of how the new thread page would look:

require_once('header.php');
if(file_exists('/html/forum/'.$thread_id.'.html'))
{
    require_once('/html/forum/'.$thread_id.'.html');
}
elseif(file_exists('/html/forum/archive/'.$thread_id.'.html'))
{
    require_once('/html/forum/archive/'.$thread_id.'.html');
}
else
{
    //display something about how the thread doesn't exist
}
require_once('footer.php');

The forum is just one example, but I was thinking about doing this with the majority of pages on my site. Are there any major drawbacks or advantages of this method over dynamically generating the content each time?

Thanks!

Upvotes: 4

Views: 612

Answers (2)

Alex Howansky
Alex Howansky

Reputation: 53533

Using static files like this isn't a bad idea at all, but don't bother with the archive subdir. Instead, split the cached files into subdirs by some abstract value, like the last digit of the thread id or the first two characters of the md5() hash of the thread id. So you get:

/1/121.html
/1/301.html
/2/92.html
/3/13.html

This will keep your files-per-subdir down. You may want to go more levels depending on how may files you expect to have:

/2/1/121.html
/0/1/301.html
/9/2/92.html
/1/3/13.html

Alternatively, you might want to put this static content into something like a Memcache -- then you won't have to worry about filenames at all, just index by the thread id. You could even put the content into your SQL database -- at least in this case, you're doing just one single-row query instead of a large join.

Upvotes: 2

ashein
ashein

Reputation: 487

I would generally stick with the dynamic page generation, because if you have, say, a thread with 10 pages, you will have to catch lots of events which trigger your cache update. These are not limited only to new post. You'll have to catch all CRUD operations on posts. Imagine the cache update if some posts at page 1 get deleted. Moreover, the user stats (num of posts total, online status etc) should usually be fresh for the viewers. So my vision is that the benefit of performance isn't worth here.

Upvotes: 3

Related Questions