有没有办法将 php 对象保留在内存中以避免磁盘读取和写入?
所以我有一个从磁盘 gnugpg 读取文件的对象,它似乎总是在主目录中创建一个 gnugpg 密钥环。
我想避免每次从 apache 调用 php 脚本时都必须加载这个对象。
有没有办法让 php 对象保留在内存中?
So I have an object that reads a file from disk gnugpg it appears to always create a gnugpg key ring in a home directory.
I want to avoid having to load this object every time a php script is called from apache.
is there away to have a php object stay in memory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果它是一个不占用太多内存并且可序列化的小对象,您可以将其存储在会话中:
然后通过调用检索该对象
但是,并非所有对象总是可序列化的,例如,如果该对象持有打开的文件句柄文件,需要一些额外的代码来在对象序列化时关闭文件,然后在对象反序列化时重新打开文件。
如果对象很大,那么最好使用适当的缓存工具(例如 memcached)来存储序列化对象,而不是会话。
If it's a small object that doesn't take up much memory and is serializable you could just store it in the session:
and then retrieve that object by calling
However not all objects are always serializable e.g. if the object holds a file handle to an open file, that would need to have some extra code to close the file when the object is serialized and then re-open the file when the object was unserialized.
If the object is large, then you would be better off using a proper caching tool like memcached to store the serialized object, rather than the session.