问题
I am writing a stateful web application in PHP in which the state potentially contains lots of objects. At this moment, I am keeping all these objects in $_SESSION and have them serialised at the end of the request. This is a bit of a pain, because serialising the entire session takes seconds, and unserialising it takes even more time.
I wanted to try APC, because I hoped that the objects are then just being memcopied, instead of serialised. Alas, it seems that if I feed apc_store($object)
an object, it seems to serialise it anyway before passing it to another process. (The story goes that primitive values and arrays are being memcopied without serialisation, but that is not a relevant solution for me, since my session objects have a lot of (circular) references.)
My question: Is there a known way of keeping objects persistent in PHP without having to serialise them after every request? I've heard rumours that the HipHop interpreter can help with this, but I haven't found any working examples on the net. Can somebody tell me if it is possible in PHP at all?
(I know, I should be writing this project in Java or another language that supports persistent instances without a TCP connection)
回答1:
Whenever you need to store (freeze) an object, it needs to be serialized. That's independent to the storage (APC, session files, database etc.), it's because the script process will terminate and next time it starts, the objects need to come to life again.
So things can not be kept in a "run-state", objects will always be serialized to be stored.
It's known that PHP serialization is not the fastest. There are alternative implementations to it, you can use, for example igbinary PHP extension. It offers a serialize /deserialize function next to transparent session handling. Maybe this is actually helpful for your scenario.
In any case: The more you store inside the session, the more you need to un-freeze or wake-up at the beginning of the request, the more time it will take.
Related: What is the php_binary serialization handler?
来源:https://stackoverflow.com/questions/6863494/storing-objects-between-requests-in-php-without-serialising