环境
linux memcached1.5.9 (memcached安装在虚拟机192.168.70.164)
wampserver集成环境 thinkphp5 php7
步骤一:linux安装memcached
1.Linux系统安装memcached,首先要先安装libevent库。
2.源码安装
wget http://memcached.org/latest 下载最新版本 tar -zxvf memcached-1.x.x.tar.gz 解压源码 cd memcached-1.x.x 进入目录 ./configure --prefix=/usr/local/memcached 配置 make && make test 编译 sudo make install 安装
3.运行 memcached
// 作为前台程序运行
/usr/local/memcached/bin/memcached -p 11211 -m 64m -vv
// 作为后台程序运行
#/usr/local/memcached/bin/memcached -p 11211 -m 64m -d 或者
#/usr/local/memcached/bin/memcached -d -m 64M -u root -l 127.0.0.1 -p 11211 -c 256 -P /tmp/memcached.pid
4.ssh链接memcached
telnet 127.0.0.1 11211 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. // 以上为正常状态 // 这是一条完整的创建命令 set foot 0 0 3 bar // 记得按回车键 End set foo 0 0 3 保存命令 bar 数据 STORED 结果 get foo 取得命令 VALUE foo 0 3 数据 bar 数据 END 结束行 quit 退出
注意:默认情况下memecached是有本机访问,要外部机器访问需要设置:
#netstat -tnlp // 查看监听状态 #/usr/local/memcached/bin/memcached -d -m 10 -u root -l 0.0.0.0 -p 12000 -c 256 -P /tmp/mem // 设置对外访问(0.0.0.0) 【127.0.0.1只有本机访问】
步骤二:php7添加memcache扩展
1.下载php_memcache.dll
下载地址:https://gitee.com/zhongjie19/php7_memcached.dll
2.php.ini配置
extension=php_memcache.dll // php.ini末尾加入
步骤三:thinkphp5链接memcached
1.普通cache,只需要修改application/config.php,参数如下(注意加入缓存ip和端口)
// +---------------------------------------------------------------------- // | 缓存设置 // +---------------------------------------------------------------------- 'cache' => [ // 驱动方式 'type' => 'memcache', // 缓存保存目录 'path' => CACHE_PATH, // 缓存前缀 'prefix' => '', 'host'=>'192.168.70.164', 'port' => '12000', // 缓存有效期 0表示永久缓存 'expire' => 0, ],
2.符合缓存