在PHP中缓存对象的最佳方法? [英] Best method to cache objects in PHP?

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

问题描述

我目前正在开发一个大型网站来处理用户注册。为了争辩而建立的社交网站。但是,我注意到页面加载存在滞后,并认为是在页面上创建对象使事情变慢。

I'm currently developing a large site that handles user registrations. A social networking website for argument's sake. However, I've noticed a lag in page loads and deciphered that it is the creation of objects on pages that's slowing things down.

例如,我有一个Member对象,当使用作为构造参数传递的ID实例化时,它将在数据库中查询成员数据库表中该成员的行。不错,但这是在每次加载页面时创建的;然后说了不止一次,说了一次,调用了该特定成员的朋友的数组,因为为每个朋友创建了一个新的Member对象。

For example, I have a Member object, that when instantiated with an ID passed as a construct parameter, it queries the database for that members' row in the members database table. Not bad, but this is created each time a page is loaded; and called more than once when say, calling an array of that particular members' friends, as a new Member object is created for each friend.

因此,我在单个页面上最多可以有七个相同的对象,但是包含不同的属性。

So on a single page I can have upwards of seven of the same object, but containing different properties.

我要做的就是找到一种减少数据库负载的方法,并允许页面加载之间的持久对象。例如,要在登录时创建登录用户的对象(我可以这样做),然后将其存储在某个地方以供检索,这样我就不必在页面加载之间继续创建对象。

What I'm wanting to do is to find a way to reduce the database load, and to allow persist objects between page loads. For example, the logged in user's object to be created on login (which I can do) but then stored somewhere for retrieval so I don't have to keep re-creating the object between page loads.

什么是最佳解决方案?我看过Memcache,但由于它是第三方模块,因此我无法让网络托管服务商在此情况下安装它。在我的情况下,我有什么选择和/或最佳做法?

What is the best solution for this? I've had a look at Memcache, but with it being a third-party module I can't have the web host install it on this occasion. What are my alternatives, and/or best practices in my case?

预先感谢。

推荐答案

我认为通常最好缓存从数据库接收的数据,而不是PHP对象本身。无论哪种方式, APC Memcache XCache 是存储它的方式。

I believe it's generally better to cache the data received from the database, not the PHP object itself. Either way, APC, Memcache or XCache would be the way to store it.

这篇关于在PHP中缓存对象的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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