memcache实践小记
memcache是一个高效快速的,以内存存取的高速缓存,其保存方式通过“键”=>“值”的方式,对key进行hash存储,因而查询速度快,避免硬盘的反复擦写,也正如此,断电或重启就得重新为它“灌”内容了。
准备工作:
1.先安装libevent,因为memcached是依赖它的
2.memcached下载与安装
# ./memcached -d -m 50 -p 11211 -u root
参数说明 -m 指定使用多少兆的缓存空间;-p 指定要监听的端口; -u 指定以哪个用户来运行
3.启动./memcached -p 11211 如果报如下错
error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory
那么,注册一下libevent扩展
# vi /etc/ld.so.conf.d/libevent-i386.conf
内容为你编译生成的libevent库的路径,默认为/usr/local/lib
运行
# ldconfig
好了,这样memcachd就装好了,接下来安装php支持,可以通过phpize或静态编译方式 ,我是通过后者
下载最新版pecl memcache包解压至phpdir/ext
#rm configure
#buildconf –force 当然这个需要
#./configure –with-memcache –enable-memcache
需要注意: 要运行“buildconf”脚本,需要 autoconf ,如果没有或版 本过低就先升级它
OK,写几行试试~
<?php set_time_limit(1000); $host = 'localhost'; $memcache = new Memcache; $memcache->connect($host,11211); for($i=0;$i<100000;$i++){ /*存上10w个看看*/ $memcache->set('k'.$i,$i,false,1000); } print_r($memcache->getServerStatus($host)); print_r($memcache->getStats());//查看memcached服务状态 ?>