Redis学习之Jedis的操作和使用
1、Jedis的介绍
Jedis就是Redis在Java中的实现,所以Jedis就是集成了Redis的一些命令操作,封装了Redis的Java客户端,提供了连接池管理。虽然Jedis是Redis官方推荐的面向Java的操作Redis的客户端,但是我们一般不直接使用Jedis,而是在其上在封装一层,作为业务的使用。所以一般会使用RedisTemplate,RedisTemplate是SpringDataRedis中对JedisApi的高度封装。SpringDataRedis相对于Jedis来说可以方便地更换Redis的Java客户端,比Jedis多了自动管理连接池的特性,方便与其他Spring框架进行搭配使用如:SpringCache。所以我们这里简单了解Jedis的使用即可。
2、Jedis的应用准备工作
首先要修改redis.conf配置文件的这两项:
bind 127.0.0.1 注释掉
protected-mode 设置为no
额外还有一点需要注意的是开启对应的端口:
- CentOS7下查看防火墙运行访问的端口号:firewall-cmd --list-ports
- 开启6379端口号:firewall-cmd --zone=public --add-port=6379/tcp --permanent
- 重启防火墙:firewall-cmd --reload
然后需要导入相关依赖:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 | < dependencies > <!-- Jedis的支持包 --> < dependency > < groupId >redis.clients</ groupId > < artifactId >jedis</ artifactId > < version >3.3.0</ version > </ dependency > <!-- Junit测试包 --> < dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >4.12</ version > </ dependency > <!-- 导入slf4j包 --> < dependency > < groupId >org.slf4j</ groupId > < artifactId >slf4j-nop</ artifactId > < version >1.7.2</ version > </ dependency > |
创建生成Jedis连接的JedisPoolUtil.java工具类,其中:
- redis.clients.jedis.JedisPool:加载配置信息创建连接池
- redis.clients.jedis.JedisPoolConfig:存放配置信息
01 02 03 04 05 06 07 08 09 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 | package com.thr.redis; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class JedisPoolUtil { private static JedisPool jedisPool; static { //连接池配置信息,不写使用默认值 JedisPoolConfig config = new JedisPoolConfig(); //可用最大连接数 config.setMaxTotal( 50 ); //最大空闲连接数 config.setMaxIdle( 5 ); //最长等待时间 config.setMaxWaitMillis( 10000 ); //在获取redis连接时,自动检测连接是否有效 config.setTestOnBorrow( true ); //我的redis数据库设置了密码,所以需要密码参数 ////jedisPool=new JedisPool(config,ADDR,PORT,TIMEOUT,AUTH); jedisPool= new JedisPool(config, "192.168.30.102" , 6379 , 60000 , "123456" ); } //获取Redis资源 public synchronized static Jedis getJedis(){ try { if (jedisPool!= null ) { Jedis jedis=jedisPool.getResource(); return jedis; } else { return null ; } } catch (Exception e) { e.printStackTrace(); } return null ; } //释放redis资源 @SuppressWarnings ( "deprecation" ) public synchronized static void releaseConn(Jedis jedis){ if (jedisPool!= null ) { jedisPool.close(); } } } |
然后通过Jedis的ping()方法测试是否成功连接到了redis。
01 02 03 04 05 06 | @Test public void testPing(){ Jedis jedis = JedisPoolUtil.getJedis(); //测试是否成功连接redis System.out.println(jedis.ping()); } |
输出结果如下:
看到输出PONG,则表示连接成功了!下面我们分别来使用Jedis操作Redis的一些命令。
3、操作基本的key
01 02 03 04 05 06 07 08 09 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 | //对key的操作 @Test public void operateKey() { System.out.println( "******对key的操作******" ); Jedis jedis = JedisPoolUtil.getJedis(); try { // 1.flushDB():清空Redis数据库(谨慎操作) System.out.println(jedis.flushDB()); // 2.exists():判断key是否存在 System.out.println(jedis.exists( "key1" )); jedis.set( "key1" , "value1" ); jedis.set( "key2" , "value2" ); System.out.println(jedis.exists( "key1" )); // 3.keys():查询匹配的key // KEYS * 匹配数据库中所有 key 。 // KEYS h?llo 匹配 hello , hallo 和 hxllo 等。 // KEYS h*llo 匹配 hllo 和 heeeeello 等。 // KEYS h[ae]llo 匹配 hello 和 hallo ,但不匹配 hillo 。 System.out.println(jedis.keys( "*" )); // 4.expire();设置60秒后该key过期 jedis.expire( "key2" , 60 ); // 5.pttl():获取key的剩余生存时间 System.out.println(jedis.pttl( "key2" )); // 6.persist():移除key的过期时间 jedis.persist( "key" ); // 7.type():获取key的类型, "string", "list", "set". "none" none表示key不存在 System.out.println( "type: " + jedis.type( "key2" )); // 8.renamenx():将key重命名 jedis.renamenx( "key2" , "key2_rename" ); System.out.println( "key2是否存在: " + jedis.exists( "key2" )); // 判断是否存在 System.out.println( "key2_rename是否存在: " + jedis.exists( "key2_rename" )); // 判断是否存在 // 9.del():删除key jedis.del( "key2" ); System.out.println(jedis.exists( "key2" )); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.releaseConn(jedis); } } |
输出结果如下:
4、操作String类型数据
01 02 03 04 05 06 07 08 09 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 | //操作String类型数据 @Test public void operateString(){ System.out.println( "******对String的操作******" ); Jedis jedis = JedisPoolUtil.getJedis(); try { // 清空Redis数据库(谨慎操作) //System.out.println(jedis.flushDB()); // 1.set():设置键值(key-value) jedis.set( "hello" , "String" ); System.out.println( "获取的值为:" +jedis.get( "hello" )); // 2.append():在键值后追加内容 jedis.append( "hello" , " This is append String" ); System.out.println( "拼接后的值为:" +jedis.get( "hello" )); // 3.mset():同时设置多个key-value jedis.mset( "a" , "1" , "b" , "2" , "c" , "3" ); System.out.println( "批量获取值:" +jedis.mget( "a" , "b" , "c" )); // 4.setex():设置key过期时间 jedis.setex( "hello2" , 3 , "String" ); try { Thread.sleep( 1000 ); } catch (Exception e) { e.printStackTrace(); } System.out.println( "key的剩余过期时间:" +jedis.pttl( "hello2" )); // 5.setnx():在键key不存在的情况下,将键key的值设置为value,当且仅当可以存在(这个命令非常重要,可以实现分布式锁的功能) // 解释:如果key存在,则SETNX不做任何动作,如果key不存在则相当于进行SET操作 jedis.setnx( "hello" , "world" ); //jedis.setnx("h","w"); System.out.println(jedis.get( "hello" )); System.out.println(jedis.keys( "*" )); // 6.incr():如果键key的值是整数可以进行加减操作,其他它类型会报错 jedis.set( "testInt" , "0" ); jedis.incr( "testInt" ); System.out.println( "整数值进行加操作:" +jedis.get( "testInt" )); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.releaseConn(jedis); } } |
输出结果如下:
5、操作List类型
01 02 03 04 05 06 07 08 09 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 | //操作List类型 @Test public void operateList(){ System.out.println( "******对List的操作******" ); Jedis jedis=JedisPoolUtil.getJedis(); try { // 清空Redis数据库(谨慎操作) System.out.println(jedis.flushDB()); // 1.lpush():从列表左侧增加元素,lpush()方法参数列表是可变参数 jedis.lpush( "redisList" , "aaa" , "bbb" , "ccc" , "ddd" ); // 2.lpushx():将值value插入到列表key的前面,仅当key存在并且是同一个List // 和LPUSH命令相反,当可以不存在是,LPUSHX命令上面也不做 // 注意:LPUSHX和RPUSHX只能插入已存在的List中,如果键key不存在就不进行任何操作 jedis.lpushx( "redisList" , "AAA" , "BBB" ); // 3.lrange():返回List列表中key指定区间内的元素,偏移量start-stop,当start是0,stop是-1时表示遍历整个redisList System.out.println( "List列表的元素:" +jedis.lrange( "redisList" , 0 , - 1 )); List<String> redisList=jedis.lrange( "redisList" , 0 , - 1 ); System.out.print( "遍历出来的List元素:" ); for ( int i = 0 ; i < redisList.size(); i++ ) { System.out.print(redisList.get(i)+ " " ); } System.out.println(); // 4.llen():返回List列表的长度 System.out.println( "List列表的长度为:" +jedis.llen( "redisList" )); // 5.linsert():可以在指定值后插入元素,如果该元素有多个,只在第一个后面插入 jedis.linsert( "redisList" , ListPosition.AFTER, "AAA" , "CCC" ); jedis.linsert( "redisList" , ListPosition.AFTER, "CCC" , "DDD" ); System.out.println(jedis.lrange( "redisList" , 0 , - 1 )); // 6.lindex():根据指定索引获取值,索引为正从左往右获取,索引为负从右向左获取 String index2=jedis.lindex( "redisList" , 2 ); String index_2=jedis.lindex( "redisList" , - 2 ); System.out.println( "从左到右获取索引为2的值为:" +index2); System.out.println( "从右到左获取索引为2的值为:" +index_2); // 7.lset():修改列表指定索引元素,若不存在则报错 jedis.lset( "redisList" , 1 , "updateValue" ); System.out.println( "修改索引为1的值:" +jedis.lindex( "redisList" , 1 )); System.out.println(jedis.lrange( "redisList" , 0 , - 1 )); // 8.lpop():删除列表左侧头部元素 String lrem=jedis.lpop( "redisList" ); System.out.println( "删除列表的头部元素:" +lrem); // 9.rpop():删除列表右侧头部元素 String rrem=jedis.rpop( "redisList" ); System.out.println( "删除列表的尾部元素:" +rrem); // 10.ltrim():去除索引范围外的元素 String ltrim=jedis.ltrim( "redisList" , 1 , 3 ); System.out.println( "去除key为redisList,索引在1-3之外的其它元素:" +ltrim); System.out.println(jedis.lrange( "redisList" , 0 , - 1 )); // 11.lrem():移出指定值的索引位置,如果count>0从左往右删除count个该元素,如果count=0删除列表中与value相同的元素,如果count<0从右往左删除count个该元素 jedis.lrem( "redisList" , 1 , "CCC" ); System.out.println( "移除从左到右key为redisList,值为CCC的一个元素:" +jedis.lrange( "redisList" , 0 , - 1 )); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.releaseConn(jedis); } } |
输出结果如下:
6、操作Hash数据类型
01 02 03 04 05 06 07 08 09 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 | //操作Hash类型数据 @Test public void operateHash(){ System.out.println( "******对Hash的操作******" ); Jedis jedis=JedisPoolUtil.getJedis(); try { //清空数据库(谨慎操作) System.out.println(jedis.flushDB()); Map<String, String> map= new HashMap<String, String>(); map.put( "username" , "张三" ); map.put( "password" , "123456" ); map.put( "age" , "24" ); map.put( "sex" , "男" ); // 1.hmset():批量添加hash类型数据 jedis.hmset( "user" , map); // 2.hget():获取该键包含的指定键值 System.out.println( "获取指定key的值:" +jedis.hget( "user" , "username" )); // 3.hgetAll():获取Hash表中该键包含的所有键值对 System.out.println( "该key中所有的键值对:" +jedis.hgetAll( "user" )); // 4.hexists():判断key是否存在 boolean isExists=jedis.hexists( "user" , "address" ); System.out.println( "Key address是否存在?" +(isExists? "是" : "否" )); // 5.hlen():获取该散列表包含的键的个数 long hlen=jedis.hlen( "user" ); System.out.println( "散列表的key为user的长度为:" +hlen); // 6.hset():向散列中添加键值 jedis.hset( "user" , "address" , "中国深圳" ); // 7.hkeys()/hvals:返回哈希表中key所有的域和域的值。 System.out.println( "哈希表key为user的所有域为:" +jedis.hkeys( "user" )); System.out.println( "哈希表key为user的所有域值为:" +jedis.hvals( "user" )); // 8.hincrBy():如果键值是整型,可以加减该键值 jedis.hincrBy( "user" , "age" , 2 ); System.out.println( "增加后的age为:" +jedis.hget( "user" , "age" )); // 9.hdel():删除散列中的键 jedis.hdel( "user" , "address" ); isExists=jedis.hexists( "user" , "address" ); System.out.println( "user对象中的address是否还存在?" +(isExists? "是" : "否" )); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.releaseConn(jedis); } } |
输出结果如下:
7、操作Set类型数据
01 02 03 04 05 06 07 08 09 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 | //操作Set类型数据 @Test public void operateSet(){ System.out.println( "******对Set的操作******" ); Jedis jedis=JedisPoolUtil.getJedis(); try { //清空数据库(谨慎操作) System.out.println(jedis.flushDB()); // 1.sadd():添加元素,注意与List类型的区别,Set不会存储重复元素 jedis.sadd( "redisSet" , "AAA" ); jedis.sadd( "redisSet" , "AAA" , "BBB" ); jedis.sadd( "redisSet" , "AAA" , "BBB" , "CCC" , "DDD" ); // 2.scard():获取集合里面的元素数量 System.out.println(jedis.scard( "Set集合中的数量为:" + "redisSet" )); // 3.smembers():查询Set集合中的所有元素 Set<String> redisSet=jedis.smembers( "redisSet" ); System.out.println( "Set集合中的所有元素:" +redisSet); System.out.print( "Set集合中遍历出来的所有元素:" ); Iterator<String> iterator=redisSet.iterator(); while (iterator.hasNext()) { System.out.print(iterator.next()+ " " ); } System.out.println(); // 4.sismember():判断元素是否存在于集合内 boolean isExists=jedis.sismember( "redisSet" , "AAA" ); System.out.println( "AAA是否在redisSet?" +(isExists? "是" : "否" )); // 5、集合运算(并集、交集、差集) // (1)、并集 jedis.sadd( "redisSet2" , "aaa" , "bbb" , "ccc" ); Set<String> unionSet=jedis.sunion( "redisSet" , "redisSet2" ); System.out.println( "交集结果:" +unionSet); // 并集结果存入到另一个Set集合,并且只返回集合的数量 System.out.println( "并集结果存入unionSet集合:" +jedis.sunionstore( "unionSet" , "redisSet" , "redisSet2" )); // (2)、交集 Set<String> interSet=jedis.sinter( "redisSet" , "unionSet" ); System.out.println( "交集结果:" +interSet); // 交集结果存入到另一个Set集合,并且只返回集合的数量 System.out.println( "交集结果存入interSet集合:" +jedis.sinterstore( "interSet" , "redisSet" , "unionSet" )); // (3)、差集 Set<String> diffSet=jedis.sdiff( "redisSet" , "redisSet2" ); System.out.println( "差集结果:" +diffSet); // 差集结果存入到另一个Set集合,并且只返回集合的数量 System.out.println( "差集结果存入diffSet集合:" +jedis.sdiffstore( "diffSet" , "redisSet" , "redisSet2" )); // 6.spop():移除并且返回集合中的一个随机元素 System.out.println(jedis.spop( "diffSet" )); // 7.srem():删除指定集合元素 System.out.println(jedis.srem( "unionSet" , "aaa" )); // 8.smove():将一个集合中的元素移入另一个集合中 jedis.smove( "redisSet" , "redisSet2" , "AAA" ); System.out.println( "redisSet元素为:" +jedis.smembers( "redisSet" )); System.out.println( "redisSet2元素为:" +jedis.smembers( "redisSet2" )); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.releaseConn(jedis); } } |
输出结果如下:
8、操作有序集合类型
01 02 03 04 05 06 07 08 09 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 | //操作有序集合(SortedSet)类型 @Test public void operateSortedSet(){ System.out.println( "******对有序集合(SortedSet)的操作******" ); Jedis jedis=JedisPoolUtil.getJedis(); try { //清空数据库(谨慎操作) System.out.println(jedis.flushDB()); // 1.zadd():添加元素到有序集合 jedis.zadd( "scores" , 69 , "张三" ); jedis.zadd( "scores" , 83 , "李四" ); jedis.zadd( "scores" , 73 , "王五" ); //zadd()方法也有重载的传入map类型,分数是Double类型 Map<String, Double> scoresMap= new HashMap<String, Double>(); scoresMap.put( "赵六" , new Double( 59 )); scoresMap.put( "孙七" , new Double( 99 )); jedis.zadd( "scores" ,scoresMap); // 2.zcard():返回集合中的元素个数 System.out.println( "有序集合中的个数:" +jedis.zcard( "scores" )); // 3.zrange():查询有序集合的元素 System.out.println( "按照分数从低到高查询zrange:" +jedis.zrange( "scores" , 0 , - 1 )); System.out.println( "按照分数从高到低查询zrange:" +jedis.zrevrange( "scores" , 0 , - 1 )); // 4.zrangeWithScores():使用Set存储元组遍历元组内分数和元素 Set<Tuple> sortSet=jedis.zrangeWithScores( "scores" , 0 , - 1 ); Iterator<Tuple>iterator=sortSet.iterator(); while (iterator.hasNext()){ Tuple tuple=iterator.next(); System.out.println(tuple.getScore()+ ":" +tuple.getElement()); } // 5.zrangeByScore():根据分数范围查询元素(60<=score<=100) Set<String> zrangeByScore=jedis.zrangeByScore( "scores" , new Double( 60 ), new Double( 100 )); System.out.print( "查询分数范围在(60<=score<=100)之间的元素结果为:" ); for (Iterator<String> it=zrangeByScore.iterator();it.hasNext();) { System.out.print(it.next()+ " " ); } System.out.println(); // 6.zcount():查询指定分数范围内(60<=score<=100)zset键的元素个数 long rangeCount=jedis.zcount( "scores" , 60 , 100 ); System.out.println( "分数范围在(60<=score<=100)之间键的元素个数:" +rangeCount); // 7.zrank():查询指定元素的下标,不存在则返回null long zrank=jedis.zrank( "scores" , "张三" ); System.out.println( "查询指定元素的下标为:" +zrank); // 8.zscore():查询指定元素的分数,不存在则返回null Double zscore=jedis.zscore( "scores" , "张三" ); System.out.println( "查询指定元素的分数为:" +zscore); // 9.zincrby():为有序集合的score分数添加一个增量 Double zincrby=jedis.zincrby( "scores" , 99 , "张三" ); System.out.println( "增加后张三的score分数为:" +zincrby); // 10.zrem():删除指定元素 jedis.zrem( "scores" , "张三" ); // 11.zremrangeByScore():根据分数范围删除元素 jedis.zremrangeByScore( "scores" , 60 , 80 ); System.out.println( "集合的元素为:" +jedis.zrange( "scores" , 0 , - 1 )); } catch (Exception e) { e.printStackTrace(); } finally { JedisPoolUtil.releaseConn(jedis); } } |
输出结果如下:
参考资料:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· 因为Apifox不支持离线,我果断选择了Apipost!