在PHP中的请求之间存储对象而不进行序列化 [英] Storing objects between requests in PHP without serialising

查看:142
本文介绍了在PHP中的请求之间存储对象而不进行序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用PHP编写一个有状态的Web应用程序,其中该状态可能包含许多对象.目前,我将所有这些对象保留在$ _SESSION中,并在请求结束时将它们序列化.这有点麻烦,因为序列化整个会话需要几秒钟,而反序列化则需要更多时间.

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.

我想尝试APC,因为我希望这些对象只是被复制而不是序列化. ,看来,如果我喂apc_store($object)一个对象,则似乎在将其传递给另一个进程之前无论如何都要对其进行序列化. (有故事说原始值和数组无需序列化即可被复制,但这对我来说不是一个合适的解决方案,因为我的会话对象有很多(循环)引用.)

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.)

我的问题:是否存在一种已知的方法来保持对象在PHP中的持久性,而不必在每次请求后都对它们进行序列化?我听说有传言说 HipHop 解释器可以帮助解决这个问题,但我还没有在网上找不到任何可行的例子.有人可以告诉我PHP是否有可能吗?

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?

(我知道,我应该用Java或其他支持无TCP连接的持久实例的语言编写该项目)

(I know, I should be writing this project in Java or another language that supports persistent instances without a TCP connection)

推荐答案

每当您需要存储(冻结)对象时,都需要对其进行序列化.这与存储(APC,会话文件,数据库等)无关,这是因为脚本进程将终止,并且下次启动时,对象需要重新恢复生命.

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.

众所周知,PHP序列化不是最快的.您可以使用其他替代实现,例如 igbinary PHP扩展 .它在透明会话处理旁边提供了序列化/反序列化功能.也许这实际上对您的情况很有帮助.

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.

相关:什么是php_binary序列化处理程序?

这篇关于在PHP中的请求之间存储对象而不进行序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆