环境
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 11211Trying 127.0.0.1...Connected to 127.0.0.1.Escape character is ‘^]‘.// 以上为正常状态// ?这是一条完整的创建命令set foot 0 0 3 bar // 记得按回车键 Endset 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, ???],
php
public function m2(){ ???????cache(‘name‘,‘7777‘); ???}
ssh
get nameVALUE name 768 3777END
2.复合缓存
‘cache‘ => ?[ ???????// 使用复合缓存类型 ???????‘type‘ ?=> ?‘complex‘, ???????// 默认使用的缓存 ???????‘default‘ ??=> ?[ ???????????// 驱动方式 ???????????‘type‘ ??=> ‘file‘, ???????????// 缓存保存目录 ???????????‘path‘ ??=> CACHE_PATH, ???????], ???????// 文件缓存 ???????‘file‘ ??=> ?[ ???????????// 驱动方式 ???????????‘type‘ ??=> ‘file‘, ???????????// 设置不同的缓存保存目录 ???????????‘path‘ ??=> RUNTIME_PATH . ‘file/‘, ???????], ???????// redis缓存 ???????/*‘redis‘ ??=> ?[ ???????????// 驱动方式 ???????????‘type‘ ??=> ‘memcached‘, ???????????// 服务器地址 ???????????‘host‘ ??????=> ‘192.168.70.164‘, ???????????‘password‘ => ‘admin999‘, ???????],*/ ???????// memcache缓存 ???????‘memcache‘ ??=> ?[ ???????????// 驱动方式 ???????????‘type‘ ??=> ‘memcache‘, ???????????// 服务器地址 ???????????‘host‘ ??????=> ‘192.168.70.164‘, ???????????‘port‘ => ‘12000‘, ???????],
php
public function m(){ ???????//$mem = Cache::store(‘memcache‘)->get(‘name‘); ???????$mem = Cache::store(‘memcache‘)->set(‘name‘,666); ???????//print_r($mem); ???}
ssh
get nameVALUE name 768 3777END
3.内部链接
thinkphp5 memcached
原文地址:https://www.cnblogs.com/wesky/p/9400366.html