1.安装redis
ThinkPHP内置支持的缓存类型包括file、memcache、wincache、sqlite、redis。ThinkPHP默认使用自带的采用thinkCache类。(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 # aof
redischeck-dump.exe # dump
redis-cli.exe # 客户端
redis-server.exe # 服务器
redis.windows.conf # 配置文件
1 2 3 4 5 6 | // 切换到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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 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缓存配置,可以动态切换,控制器调用:
1 2 3 4 5 6 7 8 9 10 11 12 | use thinkfacadeCache; 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类(调用下面的方法)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | <?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 // +---------------------------------------------------------------------- declare (strict_types = 1); namespace thinkcachedriver; use thinkcacheDriver; /** * Redis缓存驱动,适合单机部署、有前端代理实现高可用的场景,性能最好 * 有需要在业务层实现读写分离、或者使用RedisCluster的需求,请使用Redisd驱动 * * 要求安装phpredis扩展:https://github.com/nicolasff/phpredis * @author 尘缘 */ class Redis extends Driver { /** @var PredisClient|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 ( 'PredisClient' )) { $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 PredisClient( $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:有序集合)。
命令 | 语法 | 作用 |
---|---|---|
set | set name 张三 | 设置键值 |
get | get name | 取出key值 |
del | del name1 name2 | 删除一个或者多个键值 |
mset | mset k1 k2 k3 v1 v2 v3 | 设置多个键值 |
rename | rename key newkey | 改键名 |
keys | keys * 慎用(大型服务器,会dwon,消耗进程)keys k? | 查找相应的key |
incr | incr key | 指定的key增加1,并返回加1之后的值 |
decr | decr key | 指定的key减1,并返回减1之后的值 |
append | append key value | 把value 追加到key的原值后面 |
– | – | – |
hset | hset key field value | 将哈希表 key 中的字段 field 的值设为 value 。(场景:添加用户信息) |
hmset | hmset key field1 value1 [field 2 vaue2] | 同时将多个 field-value (域-值)对设置到哈希表 key 中。 |
hget | hget key field | 获取存储在哈希表中key的指定字段的值 |
hmget | hmget key field1 field2 | 获取key的选择字段和值 |
hkeys | hkeys key | 查对应的key中所有的field |
hlen | hlen key | 获取key的长度 |
hdel | hdel key field | 删除key中的field与值 |
hexists | hexists key field | 查看哈希表 key 中,指定的字段是否存在。 |
– | – | – |
lpush | lpush key value [value2] | 将一个或多个值插入到列表头部 |
rpush | rpush key value [valus2] | 在列表中添加一个或多个值 |
lindex | lindex key index | 通过索引获取列表中的元素 |
llen | llen key | 获取列表长度 |
lpop | lpop key | 左删除并返回值 |
rpop | rpop key | 右删除并返回值 |
– | – | – |
sadd | sadd key member1 [member2] | 向集合添加一个或多个成员,集合里面相同的值的个数值算一个(应用场景:标签,社交等) |
smembers | smembers key | 返回集合中的所有成员 |
srem | srem key value1 value2 | 用于移除集合中的一个或多个成员元素,不存在的成员元素会被忽略。 |
sismember | sismember key value | 判断value是否存在集合key中,存在返回1,不存在返回0. |
smove | smove key1 key2 value | 将集合key1里面的value值删除并添加到集合key2中,如果key1里面value的值不存在,命令就不执行,返回0。如果key2已经存在value的值,那key1的集合直接执行删除value值。 |
sinter | sinter key1 key2 | key1 key2的交集,返回key1 key2的相同value。 |
sdiff | sdiff key1 key2 | key1 key2的差集,举例:key1集合{1,2,3} key2集合{2,3,4} .key1 减去 key2={1},key2减去key1={4}(相减去相同的) |
– | – | – |
zadd | zadd key score1 value1 score2 value2 | 向有序集合添加一个或多个成员,或者更新已存在成员的分数(应用场景:排名、社交等) |
zcard | zcard key | 统计key 的值的个数 |
zrange | zrange key start stop withscores | zrange key start stop withscores 把集合排序后,按照分数排序打印出来 |
zrevrange | zrevrange key start stop withscores | 把集合降序排列 |
4.redis数据备份与恢复
1 2 3 4 | 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的扩展才能使用