问题描述
- 什么是合适的衡量方法一个PHP对象的实际大小个字节/千字节?
- What's an appropriate way of measurea PHP objects actual size inbytes/kilobytes?
问原因:
我正在将memcached用于Web应用程序中的缓存存储,非技术客户将使用它.但是,由于memcached具有最大大小为1mb ,那么从一开始就设置一个函数就可以用来衡量所选对象/数组/数据集的大小,以防止它们变大,那将是很好的.
Reason for asking:
I am utilizing memcached for cache storage in my web application that will be used by non-technical customers. However, since memcached has a maximum size of 1mb , it would be great to have a function set up from the beginning that I can be used to measure size of selected objects/arrays/datasets, to prevent them from growing to big.
请注意,我仅打算将其用作警报/诊断工具,以随着时间的推移跟踪缓存性能和存储可能性.我假设计算每个memcached set/add调用的速度会稍微放慢速度.
Note that I am only planning on using this as a alert/diagnostic tool to keep track of the cache performance and storage possibilities over time. I assume that calculating speed on each memcached set/add call would slow down things a bit.
我也知道将大型数据集存储在memcached中会带走将内容存储在RAM中的整个想法,这就是为什么我需要事先了解以防止客户建立大型数据集的原因.
I am also aware of storing big datasets in memcached takes away the whole idea of storing things in the RAM, and that is exactly why I need to know in beforehand to prevent customers building up to big datasets.
非常感谢
推荐答案
好吧,由于Memcached不存储原始对象(它实际上存储有serialiezd版本),因此您可以执行以下操作:
Well, since Memcached doesn't store raw objects (it actually stores the serialiezd version), you can do this:
$serializedFoo = serialize($foo);
if (function_exists('mb_strlen')) {
$size = mb_strlen($serializedFoo, '8bit');
} else {
$size = strlen($serializedFoo);
}
这篇关于PHP:度量对象/数组的大小(以千字节为单位)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!