views:

31

answers:

3

I'd like to create something like a very basic chat application. I don't want to use a database, since it'd cause a heavy load on an already strained db. I also don't want to use a flat file, because it have a feeling that it'd become a mess or that it'll have lots of read/writes... So, I'm wondering if there is a way to have a variable that is accessible in any file and at any time.

A: 

You can't share variable values among separate requests - think of each request like the entire program is starting and finishing each time, even if there are several requests happening at once.

You could look into storing data in a cache layer (for example, memcached) however it sounds like you need to cache your database if it's under heavy load. I'd recommend caching your database (again memcached or file-based storage; serialize() data first) and then when that problem is solved store the chat data in the database (which is in turn cached). You need to store it persistently somewhere.

Ross
Thanks! I guess that I will do this.
kevin
+2  A: 

Well if you don't want a file, you're left with shared memory.

You could try PHP's shared memory functions, or use an extension like memcache or APC.

Paul Dixon
A: 

There isn't such thing. Try creating a basic file that saves serialized/json'd version of the variable you want, use php's flock to manage access to that file, cycle the file every hour/day. Since it's no big traffic simple app, I think this will be okay.

aularon