Persisting initialization data across requests

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
dewittsc
Forum Newbie
Posts: 2
Joined: Sun Sep 25, 2005 3:41 pm

Persisting initialization data across requests

Post by dewittsc »

Is there any way to keep "application" initialization data persistent across requests without using the session? That is, is there anything similar to a servlet's init() method for a PHP "application"? I would like to load the INI data for my PHP "application" once and keep it resident rather than having to parse/load it on EVERY request. Since this data is not user specific, using the session is really not appropriate. I would like a way to keep this data persistent across requests to my scripts or at the very leats across N number of requests (e.g. every 5000 requests).

I am using PHP installed as an Apache module. A colleague of mine mentioned this type of things was possible using PERL BEGIN/END blocks and setting the MaxRequestsPerChild value of Apache to something sufficiently large, thereby amortizing the one-time startup overhead across N number of requests. Anyone know if something like this is possible in PHP as well?

Thanks,
Scott
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you could store a serialization of it fairly easily, which basically would just require you to get and unserialize it.. there's shared memory too, but most servers don't have it installed that I know of.. http://php.net/shmop
dewittsc
Forum Newbie
Posts: 2
Joined: Sun Sep 25, 2005 3:41 pm

Persisting initialization data across requests

Post by dewittsc »

Can you clarify "serializing"? By reading it from a file each time, I pretty much am already just "deserializing" in a sense. This is what I'm trying to avoid. And I didn't want to use shmop either since I don't control the PHP dist installed on the server. What I'd really like is to be able to allow my PHP "application" have a persistent address space just like a Servlet does. Is there no way to do this? Am I really stuck with calling parse_ini_file on every request?[/quote]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Re: Persisting initialization data across requests

Post by feyd »

dewittsc wrote:Can you clarify "serializing"?
serialize()
dewittsc wrote:What I'd really like is to be able to allow my PHP "application" have a persistent address space just like a Servlet does. Is there no way to do this?
There was a tool released a while ago called phplet that does similar things, unfortunately, you'll have an ~unterminating script running on your host then basically. Plus, you'll need to bind to a different port which most hosts don't like.
dewittsc wrote:Am I really stuck with calling parse_ini_file on every request?
probably so.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Can we ask why you want to do this? Is it a matter of performance? If that's the case, that's not the way to get there with PHP and it's "shared nothing" way of scaling/working. I'm expanding the conversation somewhat, but stick with me.

The kind of thing you are talking about does make sense and is very appropriate in some cases, but you see it a lot where the language in use is more tightly coupled with the web server. The problem with this model is that scaling the application to work from a web farm becomes difficult as then all of the servers have to persist the same information. A cluster wide "in-memory" method of persistence is a tough nut to crack.

With PHP, what is normally done along these lines is an accellerator. The data still won't be persisted, BUT, the overhead of parsing is removed. Removing that overhead can really affect performance positively.

However, if you really don't have control of the PHP configuration, then serialize() really does make a lot of sense. Especially if the data set is complex.

Cheers
Post Reply