您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
memcached安装配置及保存php session于memcached中的方法
发布时间:2019-07-14 15:13:55编辑:雪饮阅读()
环境:
redhat6.4-i386
memcached编译安装
(1)重要依赖
yum install cyrus-sasl-devel
(2)安装libevent
[root@localhost libevent-2.0.21-stable]# ./configure --prefix=/usr/local/libeven
[root@localhost libevent-2.0.21-stable]#make
[root@localhost libevent-2.0.21-stable]#make install
(3)编译memcached
[root@localhost memcached-1.4.15]# ./configure --prefix=/usr/local/memcached --enable-sasl --with-libevent=/usr/local/libevent
[root@localhost memcached-1.4.15]#make
[root@localhost memcached-1.4.15]#make install
memcached服务启动
/usr/local/memcached/bin/memcached -m 128 -n 20 -f 1.25 -vv -u nobody&
-m:分配内存
-n:chunk大小
-f:增长因子
-u: 运行Memcached的用户。
&:以后台启动
chunk、page、slab及最大可申请内存直接的关系:
当你第一次往memcached存储数据时, memcached会去申请1MB的内存, 把该块内存称为一个slab, 也称为一个page, 如果可以存储这个数据的最佳的chunk大小为128B,那么memcached会把刚申请的slab以128B为单位进行分割成8192块. 当这页slab的所有chunk都被用完时,并且继续有数据需要存储在128B的chunk里面时,如果已经申请的内存小于最大可申请内存10MB时,memcached继续去申请1M内存,继续以128B为单位进行分割再进行存储;如果已经无法继续申请内存,那么mamcached会先根据LRU算法把队列里面最久没有被使用到的chunk进行释放后,再将该chunk用于存储.
增长因子
增长因子:每个相邻slab中的chunk的比值。mamcached默认的增长因子是1.25
编译php-memcached
php5.4.13编译:./configure --prefix=/usr/local/php --enable-fpm
(1)编译libmemcached
[root@localhost libmemcached-1.0.18]# ./configure --prefix=/usr/local/libmemcached
[root@localhost libmemcached-1.0.18]#make
[root@localhost libmemcached-1.0.18]#make install
(2)编译php-memcached
[root@localhost memcached-2.2.0]# /usr/local/php/bin/phpize
[root@localhost memcached-2.2.0]# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache --with-libmemcached-dir=/usr/local/libmemcached/
[root@localhost memcached-2.2.0]# make
[root@localhost memcached-2.2.0]# make install
(3)编译php-memcache
[root@localhost memcache-2.2.6]# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
[root@localhost memcache-2.2.6]#make && make install
(4)添加扩展
然后将如下生成的so加入到php.ini中的扩展中
[root@localhost memcached-2.2.0]# ls /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/
memcached.so
接着同样方法再将上面编译的memcache.so也加入即可
注意:这里将memcached和memcache扩展都安装了,实际使用的时候根据喜好可仅装一个即可。
配置php的session保存到memcached中
只需要在ini配置文件中做如下配置
session.save_handler = memcache
session.save_path = "tcp://127.0.0.1:11211?persistent=1&weight=1&timeout=1&retry_interval=15"
persistent:长连接
weight:权重,在多个服务器设置中占的比重
timeout:连接服务器失效的秒数,默认值 1
retry_interval:服务器连接失败时的重试频率,默认是 15 秒一次
然后在你项目中安装mem-admin源码并访问登录(默认账号是admin/admin)
然后再写一个测试session写入的脚本,如:
[root@localhost html]# cat test.php
<?php
session_start();
$_SESSION['xy']='dmj';
?>
那么当在浏览器中访问该脚本后,我们再看看mem-admin后台中memcache的slab中遍历出来的数据
所以我们的session入memcache已经完成。
关键字词:memcache,php,session