1.安装redis
ThinkPHP内置支持的缓存类型包括file、memcache、wincache、sqlite、redis。ThinkPHP默认使用自带的采用think\\Cache类。(PHPstudy自带redis)如果没有跟着下面步骤:
下载地址:https://github.com/tporadowski/redis/releases。a.解压到你自己命名的磁盘(最好不好C盘)b.如何检验是否有安装,按住win+r,输入cmd,在输入进入DOC操作系统窗口。在操作窗口切换到安装redis的目录下c.输入redis-server.exe redis.windows.conf看到这个界面就知道redis已经安装成功。这时候另启一个 cmd 窗口,原来的不要关闭,不然就无法访问服务端了。
redis介绍redis-benchmark.exe #基准测试redis-check-aof.exe # aofredischeck-dump.exe # dumpredis-cli.exe # 客户端redis-server.exe # 服务器redis.windows.conf # 配置文件
//切换到redis目录下
redis-cli.exe -h 127.0.0.1 -p 6379
//设置key
set key ABC
//取出Key
get key
2.配置redis
thinkphp 6 配值路径config/cache.php
<?php
// +———————————————————————-
// | 缓存设置
// +———————————————————————-
return [
// 默认缓存驱动
\’default\’ => env(\’cache.driver\’, \’file\’),
// 缓存连接方式配置
\’stores\’ => [
\’file\’ => [
// 驱动方式
\’type\’ => \’File\’,
// 缓存保存目录
\’path\’ => \’\’,
// 缓存前缀
\’prefix\’ => \’\’,
// 缓存有效期 0表示永久缓存
\’expire\’ => 0,
// 缓存标签前缀
\’tag_prefix\’ => \’tag:\’,
// 序列化机制 例如 [\’serialize\’, \’unserialize\’]
\’serialize\’ => [],
],
// 配置Reids
\’redis\’ => [
\’type\’ => \’redis\’,
\’host\’ => \’127.0.0.1\’,
\’port\’ => \’6379\’,
\’password\’ => \’123456\’,
\’select\’ => \’0\’,
// 全局缓存有效期(0为永久有效)
\’expire\’ => 0,
// 缓存前缀
\’prefix\’ => \’\’,
\’timeout\’ => 0,
],
],
];
没有指定缓存类型的话,默认读取的是default缓存配置,可以动态切换,控制器调用:
use think\\facade\\Cache;
public function test(){
// 使用文件缓存
Cache::set(\’name\’,\’value\’,3600);
Cache::get(\’name\’);
// 使用Redis缓存
Cache::store(\’redis\’)->set(\’name\’,\’value\’,3600);
Cache::store(\’redis\’)->get(\’name\’);
// 切换到文件缓存
Cache::store(\’default\’)->set(\’name\’,\’value\’,3600);
Cache::store(\’default\’)->get(\’name\’);
}
3.thinkphp6 中redis类(调用下面的方法)
<?php
// +———————————————————————-
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +———————————————————————-
// | Copyright (c) 2006~2021 http://thinkphp.cn All rights reserved.
// +———————————————————————-
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +———————————————————————-
// | Author: liu21st <liu21st@gmail.com>
// +———————————————————————-
declare (strict_types = 1);
namespace think\\cache\\driver;
use think\\cache\\Driver;
/**
* Redis缓存驱动,适合单机部署、有前端代理实现高可用的场景,性能最好
* 有需要在业务层实现读写分离、或者使用RedisCluster的需求,请使用Redisd驱动
*
* 要求安装phpredis扩展:https://github.com/nicolasff/phpredis
* @author 尘缘 <130775@qq.com>
*/
class Redis extends Driver
{
/** @var \\Predis\\Client|\\Redis */
protected $handler;
/**
* 配置参数
* @var array
*/
protected $options = [
\’host\’ => \’127.0.0.1\’,
\’port\’ => 6379,
\’password\’ => \’\’,
\’select\’ => 0,
\’timeout\’ => 0,
\’expire\’ => 0,
\’persistent\’ => false,
\’prefix\’ => \’\’,
\’tag_prefix\’ => \’tag:\’,
\’serialize\’ => [],
];
/**
* 架构函数
* @access public
* @param array $options 缓存参数
*/
public function __construct(array $options = [])
{
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
if (extension_loaded(\’redis\’)) {
$this->handler = new \\Redis;
if ($this->options[\’persistent\’]) {
$this->handler->pconnect($this->options[\’host\’], (int) $this->options[\’port\’], (int) $this->options[\’timeout\’], \’persistent_id_\’ . $this->options[\’select\’]);
} else {
$this->handler->connect($this->options[\’host\’], (int) $this->options[\’port\’], (int) $this->options[\’timeout\’]);
}
if (\’\’ != $this->options[\’password\’]) {
$this->handler->auth($this->options[\’password\’]);
}
} elseif (class_exists(\’\\Predis\\Client\’)) {
$params = [];
foreach ($this->options as $key => $val) {
if (in_array($key, [\’aggregate\’, \’cluster\’, \’connections\’, \’exceptions\’, \’prefix\’, \’profile\’, \’replication\’, \’parameters\’])) {
$params[$key] = $val;
unset($this->options[$key]);
}
}
if (\’\’ == $this->options[\’password\’]) {
unset($this->options[\’password\’]);
}
$this->handler = new \\Predis\\Client($this->options, $params);
$this->options[\’prefix\’] = \’\’;
} else {
throw new \\BadFunctionCallException(\’not support: redis\’);
}
if (0 != $this->options[\’select\’]) {
$this->handler->select((int) $this->options[\’select\’]);
}
}
/**
* 判断缓存
* @access public
* @param string $name 缓存变量名
* @return bool
*/
public function has($name): bool
{
return $this->handler->exists($this->getCacheKey($name)) ? true : false;
}
/**
* 读取缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $default 默认值
* @return mixed
*/
public function get($name, $default = null)
{
$this->readTimes++;
$key = $this->getCacheKey($name);
$value = $this->handler->get($key);
if (false === $value || is_null($value)) {
return $default;
}
return $this->unserialize($value);
}
/**
* 写入缓存
* @access public
* @param string $name 缓存变量名
* @param mixed $value 存储数据
* @param integer|\\DateTime $expire 有效时间(秒)
* @return bool
*/
public function set($name, $value, $expire = null): bool
{
$this->writeTimes++;
if (is_null($expire)) {
$expire = $this->options[\’expire\’];
}
$key = $this->getCacheKey($name);
$expire = $this->getExpireTime($expire);
$value = $this->serialize($value);
if ($expire) {
$this->handler->setex($key, $expire, $value);
} else {
$this->handler->set($key, $value);
}
return true;
}
/**
* 自增缓存(针对数值缓存)
* @access public
* @param string $name 缓存变量名
* @param int $step 步长
* @return false|int
*/
public function inc(string $name, int $step = 1)
{
$this->writeTimes++;
$key = $this->getCacheKey($name);
return $this->handler->incrby($key, $step);
}
/**
* 自减缓存(针对数值缓存)
* @access public
* @param string $name 缓存变量名
* @param int $step 步长
* @return false|int
*/
public function dec(string $name, int $step = 1)
{
$this->writeTimes++;
$key = $this->getCacheKey($name);
return $this->handler->decrby($key, $step);
}
/**
* 删除缓存
* @access public
* @param string $name 缓存变量名
* @return bool
*/
public function delete($name): bool
{
$this->writeTimes++;
$key = $this->getCacheKey($name);
$result = $this->handler->del($key);
return $result > 0;
}
/**
* 清除缓存
* @access public
* @return bool
*/
public function clear(): bool
{
$this->writeTimes++;
$this->handler->flushDB();
return true;
}
/**
* 删除缓存标签
* @access public
* @param array $keys 缓存标识列表
* @return void
*/
public function clearTag(array $keys): void
{
// 指定标签清除
$this->handler->del($keys);
}
/**
* 追加TagSet数据
* @access public
* @param string $name 缓存标识
* @param mixed $value 数据
* @return void
*/
public function append(string $name, $value): void
{
$key = $this->getCacheKey($name);
$this->handler->sAdd($key, $value);
}
/**
* 获取标签包含的缓存标识
* @access public
* @param string $tag 缓存标签
* @return array
*/
public function getTagItems(string $tag): array
{
$name = $this->getTagKey($tag);
$key = $this->getCacheKey($name);
return $this->handler->sMembers($key);
}
}
4.Redis 常用命令操作
Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合)。
4.redis数据备份与恢复
redis 127.0.0.1:6379> SAVE
//该命令将在 redis 安装目录中创建dump.rdb文件。
//如果需要恢复数据,只需将备份文件 (dump.rdb) 移动到 redis 安装目录并启动服务即可。获取 redis 目录可以使用 CONFIG 命令
redis 127.0.0.1:6379> CONFIG GET dir
备注:如果是php 记得要打开redis的扩展才能使用
您可能感兴趣的文章:
- ThinkPHP6使用JWT+中间件实现Token验证实例详解
- Thinkphp6配置并使用redis图文详解
- ThinkPHP6.0前置、后置中间件区别
- ThinkPHP6.0 重写URL去掉Index.php的解决方法
- 基于thinkphp6.0的success、error实现方法
- thinkphp3.2框架集成QRcode生成二维码的方法分析
- Thinkphp使用Zxing扩展库解析二维码内容图文讲解
- Thinkphp3.2.3整合phpqrcode生成带logo的二维码
- ThinkPHP6使用最新版本Endroid/QrCode生成二维码的方法实例