在PHP中缓存变量 [英] Caching variables in PHP

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

问题描述

长话短说,我正在寻找一种快速有效地存储大多数布尔变量的最佳方法,例如:

Long story short, I am looking for the best way to quickly and efficently store, mostly, boolean variables, like:

  • 当前用户是否浏览过此页面? (布尔值)
  • 当前用户是否为此页面投票? (再次为布尔值)
  • 此用户今天有多少次得分才能投票? (整数)

这些变量将仅存储一天,也就是说,将在每天的午夜将其删除.

These variables are going to be stored only for ONE day, that is at midnight each day they will be removed.

我可以想到五种方法来完成此任务,但是我不知道如何正确地对其进行速度测试,因此我当然可以在此方面提供一些帮助.

I can think of five ways to accomplish this, but I don't know how to properly speedtest them, so I could certainly use some help with this.

第一个想法是将一些变量存储在诸如<?php $___XYZ = true;这样的文件中,然后将其包含并返回$___XYZ.问题是,很可能将有数百个这样的变量,这可能会占用很多空间(因为,如果我错了,请纠正我,每个文件至少要占用约4KB的空间,具体取决于分区格式).最大的优点是易于访问,易于使用,并且易于在一天开始时清除整个内容(只需删除包含内容的整个文件夹).访问速度有任何问题吗?

The first idea is to store some variable in a file like this <?php $___XYZ = true;, then include it and return $___XYZ. The problem is, most likely there are going to be hundreds of these variables and this can take potentially a lot of space (since, correct me if I am wrong, each file takes minimum ~4KB of space, depending on partition format). Big plus is ease of access, easy to work with, and easy to clear the whole thing at the beginning of a day (just delete the whole folder with contents). Any problems with speed of access?

我可以按照以下方式将变量组存储在一个文件中:

I could store groups of variables in one file, in such fashion:

0:1
1:1
14:0
154:0

然后使用fgets查找和读取变量,但是编写中间文件又如何呢?可以有效使用fwrite吗?我不确定这种方法是否比1.更好,但是您怎么看?

Then use fgets to find and read the variable but what about writing mid-file? Can fwrite be used effectively? I am not really confident this way is much better than 1., but what do you think?

使用apc_store和其他存储,修改和访问数据.我在这里有三个问题-我在某处读到启用APC会严重降低您的站点速度,有时在缓存方面存在一些奇怪的问题,并对如何有效地仅删除每日"缓存并保留我可能拥有的其他内容感到好奇缓存了吗?并存储了成千上万个变量,效果如何?

Use apc_store and others to store, modify and access the data. I have three concerns here - I read somewhere that enabling APC can seriously slow down your site, that there are sometimes strange problems with caching, and am curious about how to effectively remove only the "daily" cache, and leave anything else I might have cached? And how fine is it with hundreds of thousands variables stored in it?

我可以创建一个包含两行(名称和变量)的表,但是...我觉得与上述选项相比,它会非常缓慢.

I could create a table with two rows (name and variable) but... I have this feeling it will be painfully slow when compared to any from the above options.

总结一下-在PHP中以哪种方式存储变量是最好的?也许还有更好的东西?

To sum it up - which of these ways to store variables in PHP is the best? Or maybe there is something even better?

推荐答案

要进行概要分析,可以使用 Xdebug ,它将分析信息存储在已定义的文件夹中,并使用 webgrind 来查看分析数据.

For profiling, you can use Xdebug, which stores profiling informations in the defined folder, and use webgrind to view the profiling data.

我在php.ini中的xdebug设置:

My settings in php.ini for xdebug:

zend_extension=C:/WEB/PHP-ts/php_xdebug-2.1.0-5.3-vc6.dll
xdebug.collect_params=4
xdebug.show_local_vars=on
xdebug.scream=1
xdebug.collect_vars=on
xdebug.dump_globals=on
xdebug.profiler_enable=1
xdebug.profiler_output_dir=C:/WEB/_profiler/
xdebug.profiler_output_name=cachegrind.%s.out
xdebug.collect_return=1
xdebug.collect_assignments=1
xdebug.show_mem_delta=1

我发现了博客文章关于缓存性能比较(但这是从2006年开始的!):

And I found a blog post about cache performance comparison (but it's from 2006!):

Cache Type                Cache Gets/sec

Array Cache                       365000
APC Cache                          98000
File Cache                         27000
Memcached Cache (TCP/IP)           12200
MySQL Query Cache (TCP/IP)          9900
MySQL Query Cache (Unix Socket)    13500
Selecting from table (TCP/IP)       5100
Selecting from table (Unix Socket)  7400

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

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