php object in memory

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
adamSpline
Forum Newbie
Posts: 11
Joined: Fri Sep 16, 2011 1:30 pm

php object in memory

Post by adamSpline »

Hi all,

I am new to this form, and to php. I am creating an app that will have lots of frequent (but small) requests coming into the server. I would like to keep some of the data in memory on the server so I do not need to constantly thrash the database.

In java this was pretty easy. I would just create a singleton static object that is shared among any given jsp/servlet. I would put data in that object which needs to be updated and accessed frequently.

I am new to php, but I have not found a straightforward way to do this. Any ideas?

Thanks,

-Adam
greip
Forum Commoner
Posts: 39
Joined: Tue Aug 23, 2011 8:23 am
Location: Oslo, Norway

Re: php object in memory

Post by greip »

Have a look at the Memcached library: http://www.php.net/manual/en/book.memcached.php Using memcached you can persist values in an in-memory database shared between all php-interpreters serving requests for you application.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: php object in memory

Post by Christopher »

It is is a single server application, then you can also use APC cache.
(#10850)
adamSpline
Forum Newbie
Posts: 11
Joined: Fri Sep 16, 2011 1:30 pm

Re: php object in memory

Post by adamSpline »

Hi all,

Thanks for the reply. I will look into both solutions. At this point it is a single server app.

-Adam
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: php object in memory

Post by pickle »

MySQL also has a MEMORY storage engine. You'll still be hitting the MySQL daemon, but won't be hitting the disk.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: php object in memory

Post by John Cartwright »

pickle wrote:You'll still be hitting the MySQL daemon, but won't be hitting the disk.
Which is where most of the delays are :D

I would definitely use memcache for any temporary data that can easily be queried, i.e., by a key. Anything that involves any querying complexity use MySQL memory tables.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: php object in memory

Post by John Cartwright »

Christopher wrote:It is is a single server application, then you can also use APC cache.
APC is definitely a requirement in any development as an opcode cache. As a typical key store, memcached is much faster as APC.
Post Reply