Persistent objects?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
impromptu77
Forum Newbie
Posts: 2
Joined: Thu Oct 04, 2007 6:08 pm

Persistent objects?

Post by impromptu77 »

I'm trying to maintain a lot of common objects that will be used by many pages, but am trying to avoid the hit of reloading the information from a file or database for every page request.

For example, if I want to build an NLS such that I can make a call to get a phrase/word in a particular language, it will require maintaining a translation file with 1,000 terms and every time a page is requested, I want to just have that in memory for quick access rather than rereading it from disk/database.

Another example is I have global properties for my web site that are stored in the database, but they are used so frequently that I'd like to avoid the current overhead of querying the database on every page request.

Any suggestions on how to do this?

(Moderators, I'm not sure if this posting should be under Theory and Design - please feel free to move this to the appropriate location)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Save it in the session or put it into a faster form -- like a file. For the file you could put it in code or config or YAML or XML or serialize it or ...

But with PHP everything has to load every request. The choice is what sub-system to use. If you need more speed then put the above files on a RAM disk.
(#10850)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

or in memcached
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Does this site receive a lot of traffic? If not, selecting 1 field (the serialized object) from a database using a query won't be that much hassle at all.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
impromptu77
Forum Newbie
Posts: 2
Joined: Thu Oct 04, 2007 6:08 pm

Post by impromptu77 »

Yes, the site receives a fair amount of traffic and I don't want to use unnecessary database connections if I can avoid it. I hadn't heard of memcached before, will look into that. Thanks for all the suggestions so far!
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

If you're connecting to the database anyway, I would consider using a HEAP table. It resides entirely in memory and the query overhead would be minimal. Maybe even faster than reading a static file!
Post Reply