homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Please tell me PHP can cache data between requests

 5:57 pm on Dec 6, 2007 (gmt 0)

Is there a way to store data in cache between requests in PHP?

The data needs to be accessible application wide, Session won't work as it's user-specific.

I can't seem to find a way to do this other than some obscure module called APC that isn't packaged with the PHP library. This needs to run on a variety of web servers, so we cannot guarantee that the server will have that module installed.

All I need to cache is one small string. It only changes occasionally, but takes a while to calculate, so it needs to be cached.



 8:01 pm on Dec 6, 2007 (gmt 0)

Could you not just write the value to a file then call it from that file when needed? You could also check the make time for the file to see if you need to recalculate the value.


 8:25 pm on Dec 6, 2007 (gmt 0)

Is that a no?

ASP(.NET) has had a globally accessible object called Application since inception where you can store application specific data. This is one of the most commonly used objects in the framework. I find it odd that PHP doesn't have an equivalent.

Writing to a file is brittle because I may or may not have write access to the file system. Plus, that's pretty messy.


 1:10 am on Dec 7, 2007 (gmt 0)

Plus, that's pretty messy.

Not in Linux and other *nix versions. In the *nix concept a file is used for all I/O and storage, not only disk storage. Think about /dev/ where all character and block devices are accessed and /proc which contains all kernel and process related data.

The concept of a file is much more than data storage on a disk. It could be RAM for example if part of the file system maps to a RAM disk or shared memory.


 8:28 am on Dec 7, 2007 (gmt 0)

What is a cache?

Its just a file that is stored somewhere. Although you dont need to write that file to a specific location, as the program you are using does that for you, it is still just a file located on a storage medium.
So with php you have the flexibility to call the file what you want, know where it is and be able to access all sorts of information about that file. If it was cached within php you would not know the exact name of the file, or all sorts of information about that file; so this system is similar, just better as you have complete control over building your own cache :)


 2:08 pm on Dec 7, 2007 (gmt 0)

I don't have any control over building my own cache if I don't have write access...

Like I said, this needs to be run on a variety of web servers, and I cannot guarantee that I will have write access.

Besides, I don't need "complete control". The ability to place a runtime object in a global application store (which is totally managed by the subsystem) and quickly retrieve it unchanged is quite a bit more intuitive than having to manually serialize the data, write it out manually to the file system, and then having to deserialize it manually to retrieve it.

I guess this is just a shortcoming of PHP, I'll have to try and find another way around it. Unless some has another idea?


 2:58 pm on Dec 7, 2007 (gmt 0)

Another idea:

Is there some directory somewhere that is guaranteed to have write permissions? That would work.


 4:30 pm on Dec 7, 2007 (gmt 0)

Have a look at SQLite [php.net].


 12:28 am on Dec 8, 2007 (gmt 0)

I've been kicking around the idea of using a named session as cache... The idea being that you create a session with a specific ID and the web server keeps it for you (indefinitely?). I'm not sure how efficient it is, but here's what i have so far (assuming you want to store magic=243 in the cache):

// Close current session.
$origID = session_id();

// Start "cache" session without cookies.
$origSetting = ini_set('session.use_cookies', false);

// Get cached value or set it if first time.
$magic = $_SESSION['magic'];
else {
$magic = 243;
$_SESSION['magic'] = $magic;

// Restore original session.
ini_set('session.use_cookies', $origSetting);
if(strlen($origID)) { // The session we closed above.

} elseif(isset($_COOKIE['PHPSESSID'])) { // Start from user cookie.

} else { // Start brand new session.

echo "magic number is $magic";


 1:56 am on Dec 8, 2007 (gmt 0)

That doesn't look too bad, provided that it can keep the data in memory indefinitely...

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved