在不使用序列化的情况下在PHP中缓存对象 [英] Cache Object in PHP without using serialize

查看:102
本文介绍了在不使用序列化的情况下在PHP中缓存对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用PHP脚本创建的复杂对象.我正在寻找一种存储此对象的方法,以使后续请求不必重新创建它,也不必花费时间进行反序列化和重建.使用xdebug,我发现我花费了整个请求时间的一半来构建这个对象.即使我将对象显式存储在APC(或内存缓存)中,反序列化和加载所有类的时间也几乎与最初创建对象的时间一样长.

I have a complex object that I create in a PHP script. I am looking for a way to store this object such that subsequent requests do not have to recreate it, or spend time unserializing and rebuilding it. Using xdebug I find that I spend half of the entire request time building this object. Even when I store the object explicitly in APC (or memcache), the time to unserialize it and load all of the classes takes almost as long as creating the object in the first place.

我不知道是否可以存储并稍后在PHP中加载已编译"对象.这可能吗?还有其他解决方案吗?

I do not know if it is possible to store and later load a "compiled" object in PHP. Is this possible? Are there other solutions?

我不确定这是否可行,但是我认为我应该问社区.

I am not sure that this is possible, but I thought I should ask the community.

该对象是一个二叉树,并用作决策树.该代码基本上是从树中快速返回答案所需的API.所有这一切都需要以越来越高的速度执行,因此我正在尝试尽可能地提高性能.

The object is a binary tree, and used as a decision tree. The code is basically an API that is required to quickly return an answer from the tree. This all needs to perform at an ever increasing rate so I am trying to maximize the performance wherever possible.

推荐答案

据我所知,不进行序列化就不可能在PHP中缓存对象.但是,总的来说,缓存机制(APC,Memcache等)实际上是在尝试删除数据库连接,而不是提高性能(从而降低总体数据库负担).这绝对是关于Drupal如何使用memcache等的方法.换句话说,缓存机制应该允许您扩展,尽管它们可能并不能特别提高性能.
实施缓存机制应该使您可以更轻松地向外扩展,即使单个连接的每台计算机的性能也不比以前好.在一定的阈值下,数据库性能将急剧下降,并且缓存机制应有助于缓解该问题.

As far as I'm aware, it's not possible to cache objects in PHP without serializing. In general, however, caching mechanisms (APC, Memcache, etc) are really trying to remove the db connection(s) more than improve performance (and thereby decrease the overall DB strain). This is definitely how memcache, et al are employed with regards to Drupal. In other words, the caching mechanisms should allow you to scale, though they may not particularly improve performance.
Implementing a caching mechanism should allow you to more easily scale outward, even if the performance per machine is no better than before for a single connection. At a certain threshold, DB performance will degrade sharply, and the caching mechanisms should help alleviate that issue.

这篇关于在不使用序列化的情况下在PHP中缓存对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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