Making an imageboard in PHP out of boredom. At the moment it consists of one script which does everything e.g. handling post data and displaying posts/threads.
However, I was about to convert it so that it writes static html when a new post/thread is created, but I'm stuck on implementing board pages. Writing n pages of html out every time a post it added to the front page seems like a bad idea.
How would I implement it more efficiently?
Whenever a post is made, the contents of each of the n pages changes, therefore they must be re-written. The rewriting will (neglecting any I/O overhead and the cost of fetching a static page, etc) probably take approximately n times the amount of time it'll take to just output the page dynamically whenever it's fetched, so caching the page will only be an effective optimization when your view/post ratio is above n (for every post, there are n views).
The only way to be more efficient is to dynamically keep track of this post/view ratio, but that in itself requires you to not serve pages statically, so it can't be done at runtime; you'd have to compute it from your logs.
In general, for slow boards where you're posting more than you're viewing, there won't be enough traffic for you to notice the performance difference. For fast boards (you probably won't ever have one) you'll probably see a fuckton more views than posts, so caching the output is more effective.
Oh, you know. A "better" way to cache would be to only dynamically generate the static pages when they're requested, but this means that you'll have to do some nonesense with either error handlers or some nonesense to keep track of which pages you need to recache or whatever. Pain in the ass, and probably not worth it.
tl;dr: PREMATURE OPTIMIZATION IS STUPID JUST CACHE ALL N PAGES AND FIX IT LATER IF IT'S TOO SLOW.