zoukankan      html  css  js  c++  java
  • redis终端简单命令

    keys * 获取所有键
    lRange hongbao:44 0 -1获取该键的所有值
    del hongbao:44 删除该键的所有值

    hgetAll user:44 获取该键的所有队列
    hget hongbao:44 8 获取该队列用户为8的值
    hset hongbao:44 7 'asdf'设置该队列用户为7的值
    hdel user:44 8 删除该队列用户为8的值


    flushall 清空redis所有缓存数据

    <?php

    /*
    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    */

    namespace HomeController;
    use ThinkController;
    use HomeUtilApiResponse;

    /**
    * Description of RedisController
    *
    * @author Administrator
    */
    class SweepController extends BaseController{
    /**
    * 检测redis是否正常运行
    */
    public function __construct(){
    parent::__construct();
    if (!extension_loaded('redis')) {
    E(L('_NOT_SUPPORT_') . ':redis');
    }
    if (empty($options)) {
    $options = array(
    'host' => C('REDIS_HOST') ? C('REDIS_HOST') : '127.0.0.1',
    'port' => C('REDIS_PORT') ? C('REDIS_PORT') : 6379,
    'timeout' => C('DATA_CACHE_TIMEOUT') ? C('DATA_CACHE_TIMEOUT') : false,
    'persistent' => false,
    );
    }
    $this->options = $options;
    $this->options['expire'] = isset($options['expire']) ? $options['expire'] : C('DATA_CACHE_TIME');
    $this->options['prefix'] = isset($options['prefix']) ? $options['prefix'] : C('DATA_CACHE_PREFIX');
    $this->options['length'] = isset($options['length']) ? $options['length'] : 0;
    $this->options['dbindex'] = isset($options['dbindex']) ? $options['dbindex'] : 0;
    $func = $options['persistent'] ? 'pconnect' : 'connect';
    $this->handler = new Redis;
    $options['timeout'] === false ?
    $this->handler->$func($options['host'], $options['port']) :
    $this->handler->$func($options['host'], $options['port'], $options['timeout']);
    $this->handler->select($this->options['dbindex']);
    }

    /**
    * redis连接
    * @access private
    * @return resource
    * @author bieanju
    */
    public function connectRedis() {
    header('Content-Type:application/json; charset=utf-8');
    $config = [
    'host' => '127.0.0.1',
    'port' => 6379,
    ];
    $redis = new Redis();
    $redis->connect($config['host'], $config['port']);
    $redis->select(0);
    return $redis;
    }
    /**
    * 红包列表
    */
    public function mine_sweeps(){
    $p = I('get.p',1);
    $user_id = $_SESSION['user']['uid'];
    $count = M('hongbao')->alias('H')
    ->join('join hb_user as U on H.uid=U.id')
    ->order('H.addtime desc')
    ->field('H.*, U.headimg')
    ->count();
    $Page = new ThinkHPage($count,4);// 实例化分页类 传入总记录数和每页显示的记录数
    $show = $Page->show();// 分页显示输出
    $list['hongbao'] = M('hongbao')->alias('H')
    ->join('join hb_user as U on H.uid=U.id')
    ->order('H.addtime desc')
    ->field('H.*, U.headimg, U.name')
    ->limit($Page->firstRow.','.$Page->listRows)
    ->select();
    foreach($list['hongbao'] as &$v){
    $is_grab = M('record')->where(['uid' => ['eq', $user_id], 'h_id' => ['eq', $v['id']]])->getField('id');
    $v['headimg'] = $v['headimg']?$v['headimg']:C('HEAD_IMG');
    $v['sy_num'] = $v['num']-$v['lq_num'];
    $v['is_red'] = !empty($is_grab)?2:1;
    $v['is_user'] = $v['uid']==$user_id?1:2;
    }
    $list['user'] = M('user')->where(['id' => ['eq', $user_id], 'status' => ['eq', 1]])->field('headimg, name, total_money')->find();
    $list['user']['headimg'] = $list['user']['headimg']?$list['user']['headimg']:C('HEAD_IMG');
    $this->assign('list', $list);
    $this->assign('page', $show);
    $this->assign('p', $p);
    $this->display();
    }

    /**
    * 红包列表页显示
    */
    public function mine_sweep(){
    $p = I('get.p',1);
    $this->assign('p', $p);
    $this->display();
    }

    /**
    * 红包列表api
    */
    public function mine_sweep_api(){
    try{
    $p = I('get.p',1);
    $user_id = $_SESSION['user']['uid'];
    $sign_time = M('user')->where(['id' =>$user_id, 'status' => 1])->getField('sign_time');

    $count = M('hongbao')->alias('H')
    ->join('join hb_user as U on H.uid=U.id')
    ->order('H.addtime desc')
    ->where(['H.addtime' => ['between' ,array($sign_time,time())]])
    ->field('H.*, U.headimg')
    ->count();
    $Page = new ThinkHPage($count,4);// 实例化分页类 传入总记录数和每页显示的记录数
    $show = $Page->show();// 分页显示输出
    $list['hongbao'] = M('hongbao')->alias('H')
    ->join('join hb_user as U on H.uid=U.id')
    ->order('H.addtime desc')
    ->field('H.*, U.headimg, U.name')
    ->where(['H.addtime' => ['between' ,array($sign_time,time())]])
    ->limit($Page->firstRow.','.$Page->listRows)
    ->select();
    foreach($list['hongbao'] as &$v){
    $is_grab = M('record')->where(['uid' => ['eq', $user_id], 'h_id' => ['eq', $v['id']]])->getField('id');
    $v['headimg'] = $v['headimg']?$v['headimg']:C('HEAD_IMG');
    $v['sy_num'] = $v['num']-$v['lq_num'];
    $v['is_red'] = !empty($is_grab)?2:1;
    $v['is_user'] = $v['uid']==$user_id?1:2;
    }
    $list['user'] = M('user')->where(['id' => ['eq', $user_id], 'status' => ['eq', 1]])->field('id,headimg, name, total_money')->find();
    $list['user']['headimg'] = $list['user']['headimg']?$list['user']['headimg']:C('HEAD_IMG');
    $show=str_replace("mine_sweep_api","mine_sweep",$show);
    $list['page'] = $show;
    $list['p'] = $p;
    $this->doResponse(ApiResponse::SUCCESS, 'SUCCESS', $list);
    } catch (Exception $e) {
    $this->doResponse($e->getCode() ?: ApiResponse::SERVICE_ERROR, $e->getMessage());
    }
    }
    /**
    * 抢包详情列表
    */
    public function detail(){
    header('Content-Type:application/json; charset=utf-8');
    $hongbao_id = I('id');
    $user_id = $_SESSION['user']['uid'];
    $list['info'] = M('record')->alias('R')
    ->join('join hb_user as U on U.id=R.uid')
    ->where(['R.h_id' => ['eq', $hongbao_id]])
    ->field('U.headimg ,U.name ,U.id as user_id,R.*')
    ->select();
    $list['user'] = M('hongbao')->alias('H')
    ->join('join hb_user as U on H.uid=U.id')
    ->where(['H.id' => ['eq', $hongbao_id]])
    ->field('U.name, U.headimg, H.money,H.num')
    ->find();
    $list['user'] = M('user')->where(['id' => ['eq', $user_id], 'status' => ['eq', 1]])->field('name,headimg')->find();
    $hongbao = M('hongbao')->where(['id' => ['eq', $hongbao_id]])->field('money,num')->find();
    $list['user']['headimg'] = $list['user']['headimg']?$list['user']['headimg']:C('HEAD_IMG');
    $list['user']['total_moneys'] = $hongbao['money'];
    $list['user']['num'] = $hongbao['num'];
    foreach($list['info'] as &$vo){
    $vo['headimg'] = $vo['headimg']?$vo['headimg']:C('HEAD_IMG');
    $vo['name'] = $vo['name']?$vo['name']:'无';
    if($vo['user_id'] == $user_id){
    $list['user']['money'] = $vo['money'];
    }
    }
    // print_r($list);exit;
    $this->assign('list', $list);
    $this->display();
    }
    /**
    * 发包显示
    */
    public function send(){
    $user_id = $_SESSION['user']['uid'];
    $pack = M('pack')->order('num asc')->select();
    $choice_money = M('choice_money')->where(['type' => ['eq', 3]])->order('money asc')->select();
    $user = M('user')->where(['id' => ['eq', $user_id], 'status' => ['eq', 1]])->field('name,total_money,headimg')->find();
    $user['headimg'] = $user['headimg']?$user['headimg']:C('HEAD_IMG');
    $this->assign('pack', $pack);
    $this->assign('user', $user);
    $this->assign('choice_money', $choice_money);
    $this->display();
    }
    /*
    * 用户发红包
    */
    public function before_detail(){
    // $_SESSION['user']['id'] = 1;//发包人id
    $min = 0.01;
    $pack_id = I('post.bao_id');
    $zid = I('post.zid');//自定义金额
    $money = I('post.money');//选择金额
    $lucky = I('post.lucky');//雷号 10-0
    $pack = M('pack')->where(['id' => $pack_id])->find();
    $num = $pack['num'];//包数
    $multiple = $pack['multiple'];//倍数
    $money = !empty($zid)?$zid:$money;
    if (empty($pack_id)) {
    $return['status'] = 0;
    $return['msg'] = "请选择包数!";
    $this->ajaxReturn($return);
    }
    if (empty($lucky)) {
    $return['status'] = 0;
    $return['msg'] = "请选择雷号!";
    $this->ajaxReturn($return);
    }
    if(!preg_match('/^[0-9]+.{0,1}[0-9]{0,2}$/',$money) || empty($money)){
    $return['status'] = 0;
    $return['msg'] = "请输入正确的充值金额!";
    $this->ajaxReturn($return);
    }
    if (!preg_match("/^[0-9]{1,}$/", $num)) {
    $return['status'] = 0;
    $return['msg'] = "红包数量请输入整数!";
    $this->ajaxReturn($return);
    }
    if ($num * $min > $money) {
    $return['status'] = 0;
    $return['msg'] = "真抠";
    $this->ajaxReturn($return);
    }
    $user_money = M('user')->where(['id' => ['eq', $_SESSION['user']['uid']], 'status' => ['eq', 1]])->getField('total_money');
    if ($user_money < $money) {
    $return['status'] = 0;
    $return['msg'] = "您的余额不足,请充值!";
    $this->ajaxReturn($return);
    }
    $lucky = $lucky==10?0:$lucky;

    /*添加发包记录*/
    $data = [
    'uid' => $_SESSION['user']['uid'],
    'money' => $money,
    'num' => $num,
    'lucky' => $lucky,//雷号
    'multiple' => $multiple,//倍数
    'addtime' => time(),
    'over_time' => time()+180,
    ];
    $hongbao_redpacket = M('hongbao');
    M('hongbao')->startTrans();
    $hongbao_id = $hongbao_redpacket->add($data);
    /*添加发包余额变动记录*/
    $data_profit = [
    'uid' => $_SESSION['user']['uid'],
    'money' => $user_money-$money,
    'change' => $money,
    'type' => 4,
    'addtime' => time(),
    ];
    $add_profit = M('profit')->add($data_profit);
    $save_money = M('user')->where(['id' => ['eq', $_SESSION['user']['uid']], 'status' => ['eq', 1]])->setDec('total_money', $money);
    if (empty($hongbao_id) || !$save_money || empty($add_profit)) {
    $hongbao_redpacket->roolback();
    $return['status']=0;
    $return['msg']="发包失败";
    $this->ajaxReturn($return);
    }
    $this->hongbao_key = 'hongbao:' . $hongbao_id;
    $redis = $this->connectRedis();
    $gnRedis = $redis->llen("{$this->hongbao_key}");
    /* 判断第一个红包不为雷 */
    for ($x = 0; $x <= 100; $x++) {
    $redpacket_money = $this->rand_section($min, $money, $num, $money);
    if (mb_substr($redpacket_money[0], -1, 1) != $lucky) {
    break;
    }
    }
    /*redis添加红包金额*/
    if (!$gnRedis) {
    for ($i = 0; $i < $num; $i ++) {
    $redis->lpush("{$this->hongbao_key}", $redpacket_money[$i]);
    }
    }

    $resetRedis = $redis->llen("{$this->hongbao_key}");
    if (!$resetRedis) {
    $hongbao_redpacket->roolback();
    $return['status'] = 0;
    $return['msg'] = "发包失败";
    $this->ajaxReturn($return);
    }
    $hongbao_redpacket->commit();
    $return['status']=200;
    $return['msg']="发包成功";
    $this->ajaxReturn($return);
    }

    /*
    * 红包id、用户id
    */
    public function grab_number_queue(){
    $hongbao_id = I('id');//红包id
    $user_id = $_SESSION['user']['uid'];
    $this->user_key = 'user:'.$hongbao_id;
    $this->hongbao_key = 'hongbao:'.$hongbao_id;
    /* redis 队列 */
    $redis = $this->connectRedis();
    /* 进入队列 */
    $hongbao_num = $redis->llen("{$this->hongbao_key}");
    $user_num = $redis->hGet("{$this->user_key}",$user_id);
    // 判断用户是否已在队列
    if ($user_num) {
    $return['status'] = 200;
    $return['msg'] = "您已抢过红包";
    $this->ajaxReturn($return);
    }
    $find_redpacket = M('hongbao')->where(['id' => $hongbao_id])->field('uid, money, status, lucky, multiple,over_time,lq_money,lq_num')->find();
    $user_money = M('user')->where(['id' => ['eq', $user_id], 'status' => ['eq', 1]])->getField('total_money');
    $peifu = $find_redpacket['money']*$find_redpacket['multiple'];
    if (!$hongbao_num || $find_redpacket['status'] == 2) {
    $return['status'] = 0;
    $return['msg'] = "手慢了,红包已派完";
    $this->ajaxReturn($return);
    }
    /*判断过期红包*/
    if($find_redpacket['over_time']<time()){
    $return['status'] = 0;
    $return['msg'] = "红包已过期!";
    $this->ajaxReturn($return);
    }
    /* 判断用户余额是否大于赔付金额 */
    if ($user_money < $peifu) {
    $return['status'] = 0;
    $return['msg'] = "您的余额低于赔付金额,请充值!";
    $this->ajaxReturn($return);
    }
    $goods_number_key = $redis->lpop("{$this->hongbao_key}");
    if ($goods_number_key) {
    // 插入抢购用户信息
    $userinfo = array(
    "user_id" => $user_id,
    "create_time" => time()
    );
    $Modelr = M('record');
    $Modelr->startTrans();
    $redis->hSet("{$this->user_key}", $user_id, serialize($userinfo));
    /*判断用户是否中雷*/
    if (mb_substr($goods_number_key, -1, 1) == $find_redpacket['lucky']) {
    $is_type = 2;
    $save_user_money = M('user')->where(['id' => ['eq', $user_id], 'status' => ['eq', 1]])->setDec('total_money', $peifu);
    $hongbao_money = M('user')->where(['id' => ['eq', $find_redpacket['uid']], 'status' => ['eq', 1]])->getField('total_money');//查询放在下面防止抢包与发包人同一个
    $save_hongbao_money = M('user')->where(['id' => ['eq', $find_redpacket['uid']], 'status' => ['eq', 1]])->setInc('total_money', $peifu);
    $ray = [[
    'uid' => $user_id,
    'money' => $user_money-$peifu,
    'change' => $peifu,
    'type' => 6,//用户踩雷减余额
    'addtime' => time(),
    ],[
    'uid' => $find_redpacket['uid'],
    'money' => $hongbao_money+$peifu,
    'change' => $peifu,
    'type' => 8,//用户踩雷发包者加余额
    'addtime' => time(),
    ]];
    $addAll_money = M('profit')->addAll($ray);
    if (empty($save_user_money) || empty($save_hongbao_money) || empty($addAll_money)) {
    $Modelr->rollback();
    $return['status'] = 0;
    $return['msg'] = "抢包失败!";
    $this->ajaxReturn($return);
    }
    $user_money = $user_money-$peifu;
    } else {
    $is_type = 1;
    }
    /*用户抢包添加金额*/
    $add_money = M('user')->where(['id' => ['eq', $user_id], 'status' => ['eq', 1]])->setInc('total_money', $goods_number_key);
    $lq_hongbao = M('hongbao')->where(['id' => ['eq', $hongbao_id]])->save(['lq_money' => $find_redpacket['lq_money']+$goods_number_key,'lq_num' => $find_redpacket['lq_num']+1]);
    // 插入用户抢包记录
    $redpack_log = [
    'uid' => $user_id,
    'money' => $goods_number_key,
    'h_id' => $hongbao_id,
    'lucky'=> $find_redpacket['lucky'],
    'type' => $is_type,
    'addtime' => time(),
    ];
    $redpacket_user = M('record')->add($redpack_log);
    /*添加用户抢包余额变动记录*/
    $data_profit = [
    'uid' => $user_id,
    'money' => $user_money+$goods_number_key,
    'change' => $goods_number_key,
    'type' => 5,
    'addtime' => time(),
    ];
    $add_profit = M('profit')->add($data_profit);
    if (empty($add_money) || empty($redpacket_user) || empty($add_profit) || empty($lq_hongbao)) {
    $Modelr->rollback();
    $return['status'] = 0;
    $return['msg'] = "抢包失败!";
    $this->ajaxReturn($return);
    }
    $hongbao_num = $redis->llen("{$this->hongbao_key}");
    if (!$hongbao_num) {
    $save_redpacket = M('hongbao')->where(['id' => $hongbao_id])->save(['status' => 2]);
    if (empty($save_redpacket)) {
    $Modelr->rollback();
    $return['status'] = 0;
    $return['msg'] = "抢包失败!";
    $this->ajaxReturn($return);
    }
    $redisDel=$redis->del("{$this->user_key}");
    }
    $Modelr->commit();
    $return['status'] = 200;
    $return['msg'] = '红包金额' . $goods_number_key;
    $this->ajaxReturn($return);
    } else {
    $return['status'] = 0;
    $return['msg'] = "系统繁忙";
    $this->ajaxReturn($return);
    }
    }

    /** 获取区间内随机红包(符合正态分布)
    * @param $min 红包最小值
    * @param $max 红包最大值
    * @param $num 红包个数
    * @param $total 红包金额
    * @return array
    */
    public function rand_section ($min,$max,$num,$total){

    $data = array();
    if ($min * $num > $total) {
    return array();
    }
    if($max*$num < $total){
    return array();
    }
    while ($num >= 1) {
    $num--;
    $kmix = max($min, $total - $num * $max);
    $kmax = min($max, $total - $num * $min);
    $kAvg = $total / ($num + 1);
    //获取最大值和最小值的距离之间的最小值
    $kDis = min($kAvg - $kmix, $kmax - $kAvg);
    //获取0到1之间的随机数与距离最小值相乘得出浮动区间,这使得浮动区间不会超出范围
    $r = ((float)(rand(1, 10000) / 10000) - 0.5) * $kDis * 2;
    $k = sprintf("%.2f", $kAvg + $r);
    $total -= $k;
    $data[] = $k;
    }
    return $data;
    }



    /*
    * redis调试
    */
    public function order_num(){
    $redis=$this->connectRedis();
    // $redis->flushall();//清空redis所有缓存数据
    $a = $redis->keys('*');
    print_r($a);
    }

    }

  • 相关阅读:
    Vue路由机制
    谷歌浏览器打不开应用商店的解决方法
    Vue报错——Component template should contain exactly one root element. If you are using vif on multiple elements, use velseif to chain them instead.
    Vue.js学习之——安装
    Vue使用axios无法读取data的解决办法
    关于localstorage存储JSON对象的问题
    2013年整体计划
    个人喜欢的警语收集
    Linux防火墙的关闭和开启
    Flex修改title 转载
  • 原文地址:https://www.cnblogs.com/zhang-bin/p/10137506.html
Copyright © 2011-2022 走看看