zoukankan      html  css  js  c++  java
  • [05] Jedis

    Quick Start

    • 导入 Jedis 所需要的 jar 包:Commons-pool-1.6.jar、Jedis-2.1.0.jar
    • 编写程序测试连通性
      public class Test {
          public static void main(String[] args) {
              Jedis jedis = new Jedis("192.168.33.128", 6379);
              String result = jedis.ping();
              System.out.println(result);
              jedis.set("name", "刘源");
              System.out.println(jedis.get("name"));
              jedis.close();
          }
      }
      
    • 可能抛出的异常
      Exception in thread "main" redis.clients.jedis.exceptions.
      JedisConnectionException: java.net.ConnectException:
      Connection refused: connect
      
    • 用 Windows 中的 Eclipse 连接虚拟机的 Redis 的注意事项

    手机验证码功能

    要求

    1. 输入手机号,点击发送后随机生成 6 位数字码,2 分钟有效
    2. 输入验证码,点击验证,返回成功或失败
    3. 每个手机号每天只能输入 3 次

    页面

    相关 script 代码:

    var t = 120; // 设定倒计时的时间
    var interval;
    function refer(){
        $("#countdown").text("请于" + t + "秒内填写验证码 "); // 显示倒计时
        t--; // 计数器递减
        if(t <= 0) {
            clearInterval(interval);
            $("#countdown").text("验证码已失效,请重新发送!");
        }
    }
    
    $(function(){
        $("#sendCode").click(function () {
            $.post("/Verify_code/CodeSenderServlet"
                    , $("#codeform").serialize(), function(data) {
                if(data == "true") {
                    t = 120;
                    clearInterval(interval);
                    interval = setInterval("refer()", 1000); // 启动 1 秒定时
                } else if (data == "limit") {
                    clearInterval(interval);
                    $("#countdown").text("单日发送超过次数!")
                }
            });
        });
    
        $("#verifyCode").click(function () {
            $.post("/Verify_code/CodeVerifyServlet"
                    , $("#codeform").serialize(), function(data) {
                if(data == "true") {
                    $("#result").attr("color", "green");
                    $("#result").text("验证成功");
                    clearInterval(interval);
                    $("#countdown").text("");
                } else {
                    $("#result").attr("color", "red");
                    $("#result").text("验证失败");
                }
            });
        });
    });
    

    相关表单代码:

    <form class="navbar-form navbar-left" role="search" id="codeform">
        <input type="text" class="form-control"
                placeholder="填写手机号" name="phone_no">
        <button type="button" class="btn btn-default"
                id="sendCode">发送验证码</button><br>
        <font id="countdown" color="red"></font>
        <br>
        <input type="text" class="form-control"
                placeholder="填写验证码" name="verify_code">
        <button type="button" class="btn btn-default"
                id="verifyCode">确定</button>
        <font id="result" color="green"></font>
        <font id="error" color="red" ></font>
    </form>
    

    Servlet

    CodeSenderServlet

    protected void doPost(HttpServletRequest request
            , HttpServletResponse response) throws ServletException, IOException {
        // 获取手机号
        String phone_no = request.getParameter("phone_no");
        // 获取验证码
        String code = getCode(6);
        // 拼接键
        String codeKey = "verifyCode:" + phone_no + ":code";
        // 每个手机号一天只能验 3 次
        String countKey = "verifyCode:" + phone_no + ":count";
        Jedis jedis = new Jedis("192.168.33.128", 6379);
        // 判断发送验证码的次数
        String count = jedis.get(countKey);
        if(count == null) { // 第 1 次
            jedis.setex(countKey, 24*60*60, "1");
        } else if(Integer.parseInt(count) < 3) {
            jedis.incr(countKey);
        } else if(Integer.parseInt(count) >= 3) {
            response.getWriter().print("limit");
            jedis.close();
            return;
        }
    
        // 向 redis 中进行存储:以手机号为键,验证码为值
        jedis.setex(codeKey, 120, code); // 120s 过期
        response.getWriter().print(true);
        jedis.close();
    }
    
    private String getCode(int length) {
        String code = "";
        Random random = new Random();
        for(int i = 0; i < length; i++) {
            int rand = random.nextInt(10);
            code += rand;
        }
        return code;
    }
    

    CodeVerifyServlet

    protected void doPost(HttpServletRequest request
            , HttpServletResponse response) throws ServletException, IOException {
        // 获取验证码和手机号
        String phone_no = request.getParameter("phone_no");
        String verify_code = request.getParameter("verify_code");
        // 拼接 key
        String codeKey = "verifyCode:" + phone_no + ":code";
        // 从 redis 中获取手机号所对应的验证码
        Jedis jedis = new Jedis("192.168.33.128", 6379);
        String code = jedis.get(codeKey);
        if(code.equals(verify_code))
            response.getWriter().print(true);
        jedis.close();
    }
    

    效果展示

  • 相关阅读:
    大型网站架构系列:负载均衡详解(1)
    转:构建高并发高可用的电商平台架构实践
    转:RBAC权限控制
    小型电商网站的架构
    中小型电子商务网站架构
    装饰器在类中的实现
    使用MySQLdb操作Mysql数据库
    unicode转中文以及str形态的unicode转中文
    了解Python内存管理机制,让你的程序飞起来
    多线程初级入门学习
  • 原文地址:https://www.cnblogs.com/liujiaqi1101/p/13613196.html
Copyright © 2011-2022 走看看