Corey
Corey

Reputation: 1195

Is there a downside to this type of caching in PHP?

Say, for example, I've got a blog post with some user comments.

What I want to do is serve a static page. If the user posts a comment, it will generate a new file (with the new content added) and serve that instead.

It's a very, very simple model. Almost too simple, so I'm wondering if there are downsides to it.

Upvotes: 1

Views: 112

Answers (4)

Alfred
Alfred

Reputation: 61771

It's a very, very simple model. Almost too simple, so I'm wondering if there are downsides to it.

It is almost perfect(maybe for simple websites/small blog perfect). But couple of downside to this approach:

  • the data is not stored inside memory, which is way faster then your disc.
  • Your site is static again with this approach.

Upvotes: 0

James
James

Reputation: 3275

Watch out for race conditions when 2 users post a comment at the same time ... maybe one comment would disappear from the page? (At least until that cached copy expires, anyway.)

Upvotes: 0

DampeS8N
DampeS8N

Reputation: 3621

It prevents you from having dynamic or randomized page parts. At least with a PHP only approach. Of course you can do random/dynamic items through JS or iframes. It also means that it becomes a bit harder to deal with updates from a CMS.

Even these down sides can be skirted with relative ease. Just treat the cache like you would memcached. Or better yet, use memcached. :)

Upvotes: 0

Ish
Ish

Reputation: 29536

Downside may be (there arn't many)

  • If there's any in-efficiency in clearing your cache, your user may see old cached pages

To avoid it:

  • set expire reasonable expire time (e.g. like few hours, 1 day NOT 1 YEAR)
  • remember to clear cache on each save operation

Upvotes: 1

Related Questions