简介 Jedis是Java操作Redis的。提供了非常全面的Redis命令支持。基于阻塞IO,且调用方法是同步的,程序流需要等到Sockets处理完IO才能执行,不支持异步。Jedis的客户端实例时不安全的,所以需要连接池来使用Jedis
基本使用 1.首先需要创建一个maven项目,导入如下的依赖文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 <dependency > <groupId > junit</groupId > <artifactId > junit</artifactId > <version > 4.12</version > <scope > test</scope > </dependency > <dependency > <groupId > redis.clients</groupId > <artifactId > jedis</artifactId > <version > 3.2.0</version > </dependency >
2.测试连接是否正常,显示PONG表示连接正常
1 2 3 4 5 6 7 8 public class JedisDemo { public static void main (String[] args) { Jedis jedis = new Jedis("127.0.0.1" , 6379 ); String value = jedis.ping(); System.out.println(value); } }
操作string 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 @Test public void testString () throws InterruptedException { Jedis jedis = new Jedis("127.0.0.1" , 6379 ); jedis.set("张三" , "10010" ); jedis.set("李四" , "10011" ); jedis.set("王五" , "10012" ); Set<String> keys = jedis.keys("*" ); keys.forEach(System.out::println); System.out.println(jedis.exists("赵六" ) + "," + jedis.exists("张三" )); jedis.setex("Jacket" , 20 , "10030" ); Thread.sleep(2000 ); System.out.println(jedis.ttl("张三" )); System.out.println(jedis.ttl("Jacket" )); System.out.println(jedis.get("李四" )); jedis.mset("k1" , "v1" , "k2" , "v2" ); System.out.println(jedis.mget("k1" , "k2" )); }
操作list 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 @Test public void testList () { Jedis jedis = new Jedis("127.0.0.1" , 6379 ); jedis.lpush("key1" , "luck" , "mary" , "jack" ); List<String> values = jedis.lrange("key1" , 0 , -1 ); System.out.println(values); jedis.rpush("key2" , "luck" , "mary" , "jack" ); List<String> lrange = jedis.lrange("key2" , 0 , jedis.llen("key2" )); System.out.println(lrange); }
操作set 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 @Test public void testSet () { Jedis jedis = new Jedis("127.0.0.1" , 6379 ); jedis.sadd("users" , "user1" ); jedis.sadd("users" , "user2" ); jedis.sadd("users" , "user3" ); jedis.sadd("users" , "user4" ); jedis.srem("users" , "user4" ); System.out.println(jedis.scard("users" )); System.out.println(jedis.srandmember("users" )); System.out.println(jedis.smembers("users" )); System.out.println(jedis.sismember("users" , "user4" )); }
操作zset 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @Test public void testZSet () { Jedis jedis = new Jedis("127.0.0.1" , 6379 ); jedis.zadd("class" ,100d , "zhangsan" ); jedis.zadd("class" ,99d ,"lisi" ); jedis.zadd("class" ,85d , "wangwu" ); jedis.zadd("class" ,75d , "sss" ); jedis.zadd("class" ,65d , "aaa" ); jedis.zadd("class" ,55d , "bbb" ); System.out.println(jedis.zrange("class" ,0 ,-1 )); System.out.println(jedis.zrangeByScore("class" , 60D , 80D )); }
操作map 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 @Test public void testMap () { Jedis jedis = new Jedis("127.0.0.1" , 6379 ); Map<String, String> students = new HashMap<>(); students.put("A" , "a1" ); students.put("B" , "b2" ); students.put("C" , "c3" ); students.put("D" , "4d" ); jedis.hmset("stu" , students); jedis.hdel("stu" , "D" ); System.out.println(jedis.hlen("stu" )); System.out.println(jedis.exists("D" )); System.out.println(jedis.hkeys("stu" )); System.out.println(jedis.hvals("stu" )); }
综合案例 本案例模拟了手机发送验证码登录校验的过程
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 public class PhoneCode { @Test public void testSend () { verifyCode("123456" ); } @Test public void testCheck () { getRedisCode("123456" ,"813002" ); } private static void getRedisCode (String phone, String code) { Jedis jedis = new Jedis("127.0.0.1" , 6379 ); String codeKey = "VerifyCode" + phone + ":code" ; String redisCode = jedis.get(codeKey); if (redisCode.equals(code)) { System.out.println("验证成功" ); } else { System.out.println("验证失败" ); } jedis.close(); } private static void verifyCode (String phone) { Jedis jedis = new Jedis("127.0.0.1" , 6379 ); String countKey = "VerifyCode" + phone + ":count" ; String codeKey = "VerifyCode" + phone + ":code" ; String count = jedis.get(codeKey); if (count == null ) { jedis.setex(countKey, 24 * 60 * 60 , "1" ); } else if (Integer.parseInt(count) <= 2 ) { jedis.incr(codeKey); } else if (Integer.parseInt(count) > 2 ) { System.out.println("今天已经超过三次了,明天再试" ); jedis.close(); return ; } String vCode = getCode(); jedis.setex(codeKey, 120 , vCode); System.out.println(phone + "," + vCode); jedis.close(); } private static String getCode () { Random random = new Random(); String code = "" ; for (int i = 0 ; i < 6 ; i++) { int rand = random.nextInt(10 ); code += rand; } return code; } }