ThinkPHP从TP3开始就支持Memcache、redis等其他缓存方式,支持memcache和memcached,由于memcached更强大因此都建议使用php memcached。
一、linux安装memcached
如果是宝塔面板直接在软件商店里面安装memcached就行了
1.Linux系统安装memcached,首先要先安装libevent库。wget https://github.com/libevent/libevent/releases/download/release-2.1.11-stable/libevent-2.1.11-stable.tar.gz
./configuremakemake install
2.memcached源码安装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 0.0.0.0 -p 11211 -c 256 -P /tmp/memcached.pid
二、php7添加memcached扩展
如果是宝塔面板直接在软件商店相应的PHP版本里面安装memcached扩展就行了
这步略过
三、TP的调用方法
TP版本从3.2.2开始已经内置了Memcached驱动(位置:ThinkPHP/Library/Think/Cache/Driver/Memcached.class.php)
3.X旧版本有个bug,过期时间设置为0,理论上应该是永久缓存,但是驱动中未做处理,会变成马上过期,我们这里人工修复下set方法修改如下(如果是TP5,TP6无须修改)
public function set($name, $value, $expire = null) {
N('cache_write',1);
if(is_null($expire)) {
$expire = $this->options['expire'];
}
$name = $this->options['prefix'].$name;
if (empty($expire))
$time = 0;
else
$time = time() + $expire;
if($this->handler->set($name, $value, $time)) {
if($this->options['length']>0) {
// 记录缓存队列
$this->queue($name);
}
return true;
}
return false;
}
thinkphp5链接memcached,有三种链接方式
1.普通cache,修改application/config.php,参数如下(注意加入缓存ip和端口)// +----------------------------------------------------------------------
// | 缓存设置
// +----------------------------------------------------------------------
'cache' => [
// 驱动方式
'type' => 'memcache',
// 缓存保存目录
'path' => CACHE_PATH,
// 缓存前缀
'prefix' => '',
'host'=>'192.168.70.164',
'port' => '11211',
// 缓存有效期 0表示永久缓存
'expire' => 0,
],
php
导入:use thinkcacheDriverMemcache;
调用public function m2(){
$Cache = new Memcached();
$Cache->set('test','fsfdsfdsfdsfsfafdfa',3600); //这是存某个值,后面的3600为有效时间
$val=$Cache->get('test'); //获取某个值
$Cache->rm('test'); //删除某个值
$Cache->clear(); //清空缓存
echo $val;
}
}
TP3配置文件config.php是这么添加
//缓存配置
'DATA_CACHE_TYPE' => 'Memcached',
'MEMCACHED_SERVER' => array(
array('127.0.0.1', 11211, 0)
),
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' => '11211',
],
php调用public function m(){
//$mem = Cache::store('memcache')->get('name');
$mem = Cache::store('memcache')->set('name',666);
//print_r($mem);
}
3.内部链接(参照原生PHP调用方法,一模一样)public function mem(){
$mem = new Memcache();
$mem->connect("192.168.70.164", 11211);
$mem->set('name',3333);
$val = $mem->get('name');
echo $val;
}
驱动中调用:Memcached::addServers(array)
可以添加多个缓存服务器
还有一个配置项是 MEMCACHED_LIB,调用的是:
Memcached::setOptions(array)