tags:

views:

93

answers:

5

I am porting an application from Java Servlet to PHP. What's the equivalent of Servlet.init() in PHP?

Basically, the page uses a table that needs to be calculated. It's quite expensive operations. I am using a LAMP. I wonder if there is a way to initialize the table just once at Apache startup?

+3  A: 

You need something like http://www.zend.com/products/platform/. Unlike application servers PHP does all processing over every request. There is no concept of persistence without something like the zend platform.

Myles
We are cheap so commercial product is out of the question. Do you know any open source equivalent?
ZZ Coder
I don't know of an open source alternative. Since session management is out of the question, another option you might try, that I hesitate to suggest, is to store the values in a database. Either through serializing a PHP object or some other means.
Myles
+3  A: 

You can do this in a few ways but none that really mimic the way the Servlet.init() method you're familiar with works. You could either initialize the value and store it in the session but there would be one create penalty for each user or use a cache that your application would have access to and wouldn't be unique to each user. Zend has a pretty easy to use interface for caches that works with both memory-based (like with APC or eAccelerator) and file-based caching.

Chris Williams
Session would not work because each user normally just comes to the page once. Cache might work. Will look into it.
ZZ Coder
Try using ZendCache if you're using the Zend Framework (open source, free) and use the file back end to see if that works for you. If so, you can easily switch it to a mem cache back end to increase performance. You can also use APC and eAccelerator without having to include the Zend Framework.
Chris Williams
+1  A: 

PHP doesn't have an application scope. You could abuse $GLOBALS, but better is to store the data in $_SESSION or using Zend_Cache.

BalusC
+1  A: 

You want a single table that's shared across all sessions? PHP tries hard to make sessions completely isolated. Even static variables are initialized per-page. You could use APC, memcache or similar.

Tim Sylvester
+1  A: 

Perhaps the table can be initialized from a file on the server?

For example, if the table has to be rebuilt every time something is updated, then the PHP code, during initialization:

compare data source update against file update time
if file needs update, then 
    do number crunching
    write into file
else
    read file

This would have to be supplemented by logic which prevents the file from being used while it is updated, a Concurrency 101 exercise.

wallyk
File wouldn't work because the I/O operation is where the delay is, which I am trying to get rid of.
ZZ Coder
Surely the file i/o is faster than the computation of the data? At least that's how I interpreted "it's quite expensive operations." Surely you don't think that reading a file is too expensive to do during a query?
wallyk
If you have root (or fuse) access to your server, you could save the file in a tmpfs mount (so it's always in memory). There may be easier ways to force it to stay in memory..
Brendan Long