开发者

How to Initialize Server only Once in PHP

开发者 https://www.devze.com 2022-12-11 19:16 出处:网络
I am porting an application from Java Servlet to PHP. What\'s the equivalent of Servlet.init() in PHP?

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?


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.


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.


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消