homepage Welcome to WebmasterWorld Guest from 54.167.185.110
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
scubby




msg:3521786
 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.

 

PHP_Chimp




msg:3521900
 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.

scubby




msg:3521929
 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.

lammert




msg:3522098
 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.

PHP_Chimp




msg:3522255
 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 :)

scubby




msg:3522397
 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?

scubby




msg:3522421
 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.

coopster




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

Have a look at SQLite [php.net].

borntobeweb




msg:3522839
 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();
if(strlen($origID))
session_write_close();

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

// Get cached value or set it if first time.
if(isset($_SESSION['magic']))
$magic = $_SESSION['magic'];
else {
session_unset();
$magic = 243;
$_SESSION['magic'] = $magic;
}
session_write_close();

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

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

} else { // Start brand new session.
session_start();
session_unset();
session_regenerate_id();
}

echo "magic number is $magic";
?>

scubby




msg:3522879
 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