引入依赖

  1. <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
  2. <dependency>
  3. <groupId>redis.clients</groupId>
  4. <artifactId>jedis</artifactId>
  5. <version>4.2.1</version>
  6. </dependency>

Jedis连接Redis

需要打开阿里云的防火墙,6379端口。
修改配置文件

  1. protected-mode no,改成no
  2. #bind 127.0.0.1 -::1,注释了

然后用jedis测试链接

  1. public static void main(String[] args) {
  2. //创建Jedis对象
  3. Jedis jedis = new Jedis("xxx.xxx.xxx.xxx",6379);
  4. jedis.auth("xxxxxx");
  5. //测试
  6. String ping = jedis.ping();
  7. System.out.println(ping);
  8. }

简单测试

很多方法,在前面的文章,数据类型的常用命令里面都有,用法都一样的,可以照猫画虎的去用。这里就不一一演示了

  1. public static void main(String[] args) {
  2. //创建Jedis对象
  3. Jedis jedis = new Jedis("xxx.xxx.xxx.xxx",6379);
  4. jedis.auth("xxxxxx");
  5. //获取所有的key
  6. Set<String> keys = jedis.keys("*");
  7. for(String key:keys){
  8. System.out.println(key);
  9. }
  10. //添加
  11. jedis.set("name","张三");
  12. //获取
  13. String name = jedis.get("name");
  14. System.out.println(name);
  15. //设置多个key-value
  16. jedis.mset("k01","v1","k02","v2");
  17. //获取多个值
  18. List<String> keys2 = jedis.mget("k01", "k02");
  19. for (String key:keys2){
  20. System.out.println(key);
  21. }
  22. }

测试实例:模拟手机验证码

  1. package com.lyd.jedis;
  2. import redis.clients.jedis.Jedis;
  3. import java.util.List;
  4. import java.util.Random;
  5. import java.util.Set;
  6. public class JedisDemo1 {
  7. public static void main(String[] args) {
  8. //模拟验证码的发送
  9. String code = verifyCode("18899988888");
  10. System.out.println(code);
  11. getRedisCode("18899988888","233456");
  12. }
  13. //3.验证码校验
  14. public static void getRedisCode(String phone,String code){
  15. //从redis中获取验证码
  16. //创建Jedis对象
  17. Jedis jedis = new Jedis("xxx.xxx.xxx.xxx",6379);
  18. jedis.auth("xxxxxx");
  19. //验证码的key
  20. String codeKey ="VerifyCode"+phone+":code";
  21. String coder = jedis.get(codeKey);
  22. //判断
  23. if(coder.equals(code)){
  24. System.out.println("成功");
  25. }else {
  26. System.out.println("失败");
  27. }
  28. jedis.close();
  29. }
  30. //2.每个手机每天只能发送3次,验证码放到redis中,设置过期时间
  31. public static String verifyCode(String phone){
  32. //创建Jedis对象
  33. Jedis jedis = new Jedis("xxx.xxx.xxx.xxx",6379);
  34. jedis.auth("xxxxxx");
  35. //手机发送次数key
  36. String countKey = "VerifyCode"+phone+":count";
  37. //验证码的key
  38. String codeKey ="VerifyCode"+phone+":code";
  39. //每个手机每天只能发三次
  40. String count = jedis.get(countKey);
  41. if(count==null){
  42. //没有发送次数,第一次发送
  43. //设置发送次数为1
  44. jedis.setex(countKey,24*60*60,"1");
  45. }else if(Integer.parseInt(count)<=2){
  46. //发送次数+1
  47. jedis.incr(countKey);
  48. }
  49. else {
  50. //发送三次,不能发送了
  51. System.out.println("今天已经发送超过3次了");
  52. jedis.close();
  53. return null;
  54. }
  55. //发送的验证码要放到redis里面
  56. String vcode = getCode();
  57. jedis.setex(codeKey,120,vcode);
  58. jedis.close();
  59. return vcode;
  60. }
  61. //1.生成6位数据验证码
  62. public static String getCode(){
  63. Random random = new Random();
  64. String code="";
  65. for(int i =0;i<6;i++){
  66. int rand = random.nextInt(10);
  67. code+=rand;
  68. }
  69. return code;
  70. }
  71. }