Implementing a caching system

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
ben.i
Forum Newbie
Posts: 1
Joined: Thu Jun 07, 2007 2:46 pm

Implementing a caching system

Post by ben.i »

There are some sections of code on several of my sites that are used on virtually every page, and which use a couple of mysql queries.

The data being queried doesn't really change very often, however, so I was thinking that it would be much better to just write it to a file after it changes, and then just include that cached file instead anywhere I need it. I know that mysql will just cache the query, but I'd rather not have to hit the database at all for something that may only change once every week or two.

Anyway, I'm not exactly sure what is "safe" and what isn't. I think if someone can answer the following questions for me I'll be able to work it out from there. Or, if someone already knows of some code I can use for this purpose, I won't have to (although it would be nice to know the answer to these anyway).

1) When someone is reading from the cached file, and I try to open it to overwrite using fopen, will it let me do so?
Will that have any effect on the reading of the file?

2) When I'm overwriting the file, and I simultaneously try to read it with fopen, will it let me? Will it pause until the file is closed or give an error? Will it start reading the possibly-incomplete file? If so, it seems like it would just keep reading until I closed the file for writing. What happens exactly?

Hopefully these questions aren't too stupid. It's been a while since I've had to work with stuff like this. If anyone can steer me in the right direction, it would be greatly appreciated.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You should do some searching on PHP caching and file locking to find out more detailed information. There is a lot of code out there that works well. Take a look at the Zend Framework cache class. I believe that is is pretty much state-of-the-art.
(#10850)
Post Reply