Home  >  Article  >  php教程  >  php memcache模块优化配置详解

php memcache模块优化配置详解

WBOY
WBOYOriginal
2016-06-08 17:23:281264browse

在php中memcache是一个缓存功能,可以提高数据访问性能同时减少机器负载,下面我来介绍php中memcache优化方法吧。

<script>ec(2);</script>

memcache support enabled
Active persistent connections 0
Revision $Revision: 1.92 $

Directive Local Value Master Value
memcache.allow_failover 1 1
memcache.chunk_size 8192 8192
memcache.default_port 11211 11211
memcache.hash_function crc32 crc32
memcache.hash_strategy standard standard
memcache.max_failover_attempts 20 20

 在网上找到php的memcache模块的优化方面的部分资料,贴出来备忘.
 代码如下 复制代码

vi /etc/php.d/memcache.ini
[Memcache]
; Enable memcache extension module
extension=memcache.so
memcache.allow_failover = "1"
memcache.max_failover_attempts = "20"
memcache.chunk_size = "8192"
memcache.default_port = "11211"
memcache.hash_strategy = "standard"
memcache.hash_function = "crc32"

ps:

 代码如下 复制代码
memcache.allow_failover = "1"

一个布尔值,用于控制当连接出错时 Memcache 扩展是否故障转移到其他服务器上.默认值为 1 (true).

 代码如下 复制代码

memcache.max_failover_attempts = "20"

一个整型值,用于限制连接到持久性数据或检索数据的服务器数目.如果 memcache.allow_failover 为 false,则将忽略此参数.默认值为 20.

 代码如下 复制代码
memcache.chunk_size = "8192"

一个整型值,用于控制数据传输的大小.默认值为 8192 字节 (8 KB),但是如果设置为 32768 (32 KB),则可以获得更好的性能.

 代码如下 复制代码

memcache.default_port = "11211"

另一个整型值,用于设置连接到 Memcache 所使用的 TCP 端口.除非您修改它,否则默认值为无特权的高端口 11211.

 代码如下 复制代码

memcache.hash_strategy = "standard"

哈希策略,目前有standard模式和consistent模式.standard模式其实就是%,即取模.而consistent,就比较复杂, memcache.hash_function = "crc32"
控制哪种 hsah 函数被应用于 key映射 到服务器过程中,默认值“crc32”使用 CRC32 算法,而“fnv”则表示使用 FNV-1a 算法.

测试memcache

 代码如下 复制代码

$mc = new Memcache;
$mc->connect("127.0.0.1",11211);
$item = $mc->get('item');
if(!is_array($item)){
echo "Add item to memcache";
$mc->add('item',array('item'));
}
$item = $mc->get('item');
var_dump($item);
?>

最后,祝您成功提升服务器性能,网站运营如日中天

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn