找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 拇指 mztk
查看: 1734|回复: 1

PHP Redis扩展-windows版本-tp5(thinkphp5)

[复制链接]

134

主题

52

回帖

4913

积分

版主

积分
4913
发表于 2021-4-26 14:08:04 | 显示全部楼层 |阅读模式
本帖最后由 win 于 2021-4-26 14:14 编辑

一、
1.下载扩展地址:https://github.com/phpredis/phpredis/downloads
2.随便解压一个位置,最好解压到PHP存放目录。
3.修改或添加php配置文件
extension=php_igbinary.dllextension=php_redis.dll
4.启用redis找到redis-server.exe文件双击打开运行,不要关闭一直运行二、PHP连接配置
1.配置tp5的config目录下的cache文件
return [

    'type' => 'complex',
    // 默认(文件缓存)
'default' => [
    // 驱动方式
'type'   => 'File',
    // 缓存保存目录
'path'   => '',
    // 缓存前缀
'prefix' => '',
    // 缓存有效期 0表示永久缓存
'expire' => 0,
    'runtime_path'  => '../runtime',
        ],
    // Redis缓存
'redis' => [
        'type' => 'redis',
        'host' => '127.0.0.1',
        'expire' => 0,
        'port' => '6379',
        'password' => '',
        'timeout' => 3600,
    ],

];

2.引用文件
use think\facade\Cache;3.使用redis建立连接Cache::store('redis')->connect('127.0.0.1', 6379);
以下是基础功能,根据个人所需使用
获取
Cache::store('redis')->get('值');
保存Cache::store('redis')->set('值', value:1);
设置过期Cache::store('redis')->expire($ip, 300); //300秒完结



回复

使用道具 举报

134

主题

52

回帖

4913

积分

版主

积分
4913
 楼主| 发表于 2021-4-26 14:16:33 | 显示全部楼层
本帖最后由 win 于 2021-4-26 14:17 编辑

举个例子:当用户点击某个按钮超过7次,则5分钟后再能进行*作
    public function sendsms()
    {
        $phone = Request::param('phone');
        //此方法的公共变量
        $ckip = request()->ip();    //获取当前IP地址
        $nowTime = time();           //当前时间
        $chas = UserModel::where('phone',  $phone)->find();
        $phonecha = $chas['phone'];
        Cache::store('redis')->connect('127.0.0.1', 6379);
        $tip = Cache::store('redis')->get($ckip);
        if ($tip > 7) {
            Cache::store('redis')->expire($ckip, 300);
            $qutime = Cache::get($ckip);
            $shengyuTime = ceil(($qutime - $nowTime)/60);    //剩余分钟
            $mins = '*作次数过多,请休息5分钟,剩余'.$shengyuTime.'分钟';
            return ['status' => 0, 'message' => $mins];
            exit();
        }
             if ($phone != $phonecha) {
                 Cache::store('redis')->incr($ckip);
                 return ['status' => 2, 'message' => '手机号未注册'];
             }
            if($chas){
                $caotime = time() + 300;   //5分钟后再进行*作
                Cache::set($ckip,$caotime,300);
                Cache::store('redis')->incr($ckip);
               /*自己的代码*/

                 //写入发送日志
                 $datas = ['phone' => $phone, 'phpcode' => $code, 'phpip' => $ckip,'yongtu' => '发送验证码', 'sms' =>1];
                 DhsmsModel::create($datas);
                 return [$content, 'status' => 1, 'message' => '发送成功'];
            }else {
                if (Cache::store('redis')->exists($ckip)) {
                    Cache::store('redis')->incr($ckip);
                } else {
                    //第一次错误将错误信息存入redis中
                    Cache::store('redis')->set($ckip, 1);
                }
            }
    }
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|再加计算机 ( 鲁ICP备19060125号-10|鲁公网安备37028102001711号 )

GMT+8, 2026-9-11 18:02 , Processed in 0.134825 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表