Sharing variables between sessions on same server

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
thyrsus
Forum Newbie
Posts: 3
Joined: Thu Jul 17, 2008 4:40 pm

Sharing variables between sessions on same server

Post by thyrsus »

I am creating a large multi-server app. My php running under Apache communicates through TCP/IP to a data server on another machine. To increase performance I cache a lot of data in associative arrays that I store in session variables so that it persists between pages. This requires all that data to be be serialized/unserialized at each page display. Also, my app is multi-user. It would be great if I didn't need to go to the data server for data that has already been cached by another user.

Are there any libraries or features that would allow in-memory variables to be scoped to the Apache server level so that they can be shared between sessions and also to avoid session serialization? Obviously, a shared semaphore or locking is required to prevent conflicts.

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

Re: Sharing variables between sessions on same server

Post by pickle »

Why are you serializing the arrays? They'll work just fine if you place them right in $_SESSION.

It looks like what you want is a data cache on the webserver so it doesn't need to access the data server. I know this kind of goes against the point of having a separate data & web server, but why not store the data you want to cache on the web sever?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
thyrsus
Forum Newbie
Posts: 3
Joined: Thu Jul 17, 2008 4:40 pm

Re: Sharing variables between sessions on same server

Post by thyrsus »

I am not serializing the arrays myself. I AM putting them in $_SESSION, which is automatically serialized by PHP.

My data server is a semantic data server/inference engine. In addition to saving transmission delay, caching also eliminates re-inferencing. A local copy of the data server is not sufficient, I'd like to keep the data in PHP variable form between page sends.
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: Sharing variables between sessions on same server

Post by WebbieDave »

If your configuration affords APC, you can look into apc_store and related functions.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Sharing variables between sessions on same server

Post by pickle »

PHP doesn't exist between page sends. You'll have to store the data outside of a PHP variable.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
thyrsus
Forum Newbie
Posts: 3
Joined: Thu Jul 17, 2008 4:40 pm

Re: Sharing variables between sessions on same server

Post by thyrsus »

APC IS the solution. Thank you very much for the help!!

It allows data to be cached and shared between users. It has limitations -- the cached data needs to have a simple structure -- but serialization before storing solves that.

The data is stored under keys. I can use my object ID as the key. This way I only need to unserialize/serialize the specific objects I need rather than the whole $_SESSION.
lhphp
Forum Newbie
Posts: 1
Joined: Thu Dec 04, 2008 8:50 pm

Re: Sharing variables between sessions on same server

Post by lhphp »

i also think APC should be a good choice
Post Reply