This is the sort of thing we call a scalability killer. Loading everything into memory on one machine and then adding locks presumably for writing to this memory means you are limiting any sort of horizontal scalability and you are slowing down the one machine you do have with lock contention. But yes, there are a number of ways to do this. PHP has all the low-level functions you need to create, access and lock shared memory segments. If you look at pecl/apc you can use the apc_store/apc_fetch calls to shove an entire array or object into shared memory just once at the beginning and then pull it out very quickly directly from memory on each request. Or you could write yourself a simple extension that creates the appropriate structures in memory in the MINIT hook. But, consider scalability issues. If you are at the point where you are looking at concepts like this, you are probably also at the point where you need to start thinking about horizontal scaling, load balancers, failover, etc. A replicated database backend with a replicated copy of the data on each web server gets you data distribution and robust locking on the write to the master while all the reads from the local clients are likely to come right out of the database's memory-based request cache. The performance hit for this extra data replication and scalability is probably smaller than you think and the added value is extensive. -Rasmus Joe Wollard wrote: > Aiguo, > > >> To achieve good performance, I want to load the >> whole dictionary into memory when the "application" starts (e.g., when >> the web server starts... > > > I'm not sure how ASP works, but in PHP you can't load something into > memory when the web server starts and keep it there. PHP 'compiles' > each script upon request and then frees up the memory it occupied. I'd > think that this would be counter productive if your looking to achieve > good performance because you'll be re-building the dictionary on every > page load. > > > It might be better to let another application load the data into memory > and hold it there instead of making PHP load the whole thing every time > a user requests the page/data. If it were me I think I'd simply load > the dictionary into a database and build a simple library or class to > perform queries and calculations against the dictionary. > > Assuming your class definition takes care of connecting to the database > server you could lookup a word in almost the exact way you demonstrated. > e.g. > $dict = new dictionary; > $definition = $dict->lookup($word); > > > If you'll need this object to be available on every page the user sees > you could always make a 'common.php' (or whatever) which would create > the object and then include that at the top of each page. > > >> For a complete solution, it should support object locking/exclusive >> access as well. > > > Maybe I've been a spoiled PHP programmer for too long. ;-) I'm not sure > what object locking/ exclusive access is but from the sounds of it, > there isn't a way to do this in PHP that I'm aware of. This is assuming > that you want to be able to basically remove the object from the global > scope. In PHP objects are globally accessible from the moment they're > created to the moment they're destroyed. > > Good luck! > -Joe > > > On Jul 27, 2005, at 8:40 PM, Aiguo Fei wrote: > >> In ASP.Net there is the concept of "application" which is a virtual >> directory on a Web server. An application can have application-wide >> shared data/objects, which can be accessed by any script of that >> application (i.e., scripts under the virtual directory). I have gone >> through several PHP books, haven't seen it mentioned anywhere that PHP >> has similar facility. I don't know if anyone has written an extention >> to do something similar. Or any experience/suggestion on this matter. >> >> To make it clear, consider the following example. I want to do >> web-based dictionary. To achieve good performance, I want to load the >> whole dictionary into memory when the "application" starts (e.g., when >> the web server starts, or triggered when the first script under a >> certain directory is requested), and build a lookup table; then create >> a globally-accessible object, let's say, Application["my_dict"]; and >> it provides a function to do the dictionary lookup. In a script, one >> could do something like: >> $definition=Application["my_dict"]->lookup( $word ); >> >> For a complete solution, it should support object locking/exclusive >> access as well. >> >> -- >> PHP General Mailing List (http://www.php.net/) >> To unsubscribe, visit: http://www.php.net/unsub.php >> >> > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php