views:

391

answers:

6

I'm trying to implement some kind of caching in a PHP script that will go out to many different clients/sites, which will be deployed by fairly non-technical users, using a variety of web hosts. Due to the non-technical nature of the users, I'd like to avoid asking them to tweak file permissions. The caching needs to be across sessions, so using session variables is out. If I was coding this in ASP I'd use application variables, but they don't exist in PHP (to my knowledge)

Does anyone have any suggestions on how to accomplish this?

Here are some possibilities I've considered, any comments on these would be useful:

  • Caching via files in system temp folders - I could use sys_get_temp_dir() (or some home rolled similar function on PHP4) to help find such a folder. The disadvantage here, is it probably wouldn't work on hosts using the openbase_dir restriction
  • Some website I looked at mentioned tricking PHP into making all user sessions share the same session state thereby forcing session variables to act like session variables. Of course I can't find that post now... This seems scary anyway. (I'll update this question with the link to this post once I find it again)
  • Use a third party data store like Amazon's Simple Storage Service - seems like overkill
  • Cache the data on a server I control, and have the client download new data from there on each hit.

Once again, any comments on these ideas or any new ones would be appreciated.

UPDATE: I tried using session_id() to use a shared session state, but it doesn't work. Each session is maintaining its own "GlobalCache", any ideas why?:

// This code doesn't work, but the similar code in my answer does!

function test() {
 if (!$_SESSION['session_count']) $_SESSION['session_count'] = 0;
 $_SESSION['session_count']++;


 $count = get_cache( 'count' );

 print "pre application count: $count<br>";

 if ( !$count )
  $count = 0;

 $count++;

 print "session_id: " . session_id() . "<br>";
 print "post application count: $count<br>";
 print "session_count: " . $_SESSION['session_count'] . "<br>";

 set_cache('count', $count);
}


function set_cache( $name, $value ) {
 $old_session = session_id();
 print "old_session (set): $old_session<br>";

 session_id("GlobalCache");
 print "new_session (set): " . session_id() .  "<br>";

 $_SESSION[$name] = $value;
 session_id( $old_session );
}

function get_cache( $name ) {
 $old_session = session_id();
 print "old_session (get): $old_session<br>";

 session_id("GlobalCache");
 print "new_session (get): " . session_id() .  "<br>";

 $value = $_SESSION[$name];
 session_id( $old_session );
 return $value;
}

session_start();
test();

UPDATE: some have suggested using memcached, which is in fact a great solution for some, but since I don't have control over the end server environment it isn't an option. The idea is to have a script that people can just FTP up to a shared hosting account that just works out of the box.

UPDATE: someone suggested creating my own cache folder with the script, but wouldn't I need to create such a folder inside a folder that already had write permissions?

UPDATE, SOLUTION FOUND: I ended up figuring out the problems in my global session script and have posted my own answer to that effect. Thanks for the help everyone.

A: 

I vote for "Cache the data on a server I control, and have the client download new data from there on each hit."

All other paths lead to certain madness.

JDrago
+1  A: 

You could use sessions with a fixed session key.

http://de.php.net/manual/en/function.session-id.php:

session_id([id]) is used to get or set the session id for the current session. If id is specified, it will replace the current session id. session_id() needs to be called before session_start() for that purpose.

Marcel
Marc - see my update above
Tristan Havelick
+1  A: 

Use your own cache dir in your application’s dir. Therewith you don’t have to struggle with various server and/or PHP settings and get the best portability.

Gumbo
And make sure that cache subdir is created by PHP during the first run, so it is the owner of the directory and can write to it later
Milan Babuškov
… and protect it from requests from the internet.
Gumbo
Gumbo: see my update above
Tristan Havelick
A: 

Check out memcached for php- it works really well.

http://www.php.net/memcache

Fergie
memcache is cool, but it isn't an option here as I don't control the sever where copies of my script will be deployed.
Tristan Havelick
+1  A: 
Tristan Havelick
A: 

I used the updated function by the author as a way to cache a simple database query result since I was expecting high load. I also stored a timestamp so that I could define how often the page would renew the database result rather than just taking the cached value.

I can tell you that this cache function had much much much worse performance than going straight for the database at each request. I actually killed the server. Once I switched back to simply querying the database the server got up to full speed and the pretty big load was not even noticed.

David Rinnan