springboot使用redis
1、pom文件中引入 spring-boot-starter-redis
1 2 3 4 | < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-redis</ artifactId > </ dependency > |
2、添加application.properties配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | # REDIS (RedisProperties) # Redis数据库索引(默认为 0 ) spring.redis.database= 0 # Redis服务器地址 spring.redis.host= 192.168 . 0.58 # Redis服务器连接端口 spring.redis.port= 6379 # Redis服务器连接密码(默认为空) spring.redis.password= # 连接池最大连接数(使用负值表示没有限制) spring.redis.pool.max-active= 8 # 连接池最大阻塞等待时间(使用负值表示没有限制) spring.redis.pool.max-wait=- 1 # 连接池中的最大空闲连接 spring.redis.pool.max-idle= 8 # 连接池中的最小空闲连接 spring.redis.pool.min-idle= 0 # 连接超时时间(毫秒) spring.redis.timeout= 1000 |
这里的连接超时时间为0的话,运行程序会报连接超时异常
3.使用redisTemplate来操作redis中的数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | @Test public void testObj() throws Exception { User user= new User( 111 , "user01" , "user01" ); ValueOperations<String, User> operations=redisTemplate.opsForValue(); operations.set( "user" , user); operations.set( "user2" , user); boolean exists=redisTemplate.hasKey( "user" ); if (exists){ System.out.println( "exists is true" ); } else { System.out.println( "exists is false" ); } operations.set( "2" , new User( 111 , "aa" , "zz" )); System.out.println(operations.get( "2" )); User user1 = operations.get( "user" ); User user2 = operations.get( "user2" ); System.out.println(user1 + "," + user2.toString()); // Assert.assertEquals("aa", operations.get("com.neo.f").getUserName()); } |
设置过期时间
1 2 3 4 5 6 7 8 9 10 | redistempalate的超时设置时,一定要每次用set写入时,更新超时,不然默认会失效的。 例如: //获取过期时间 int tempTime = this .redisTemplate.getExpire( "max" ).intValue(); //获取value tempCount = this .redisTemplate.opsForValue().get( "max" ); //设置key-value this .redisTemplate.opsForValue().set( "max" , tempCount); //设置过期时间 this .redisTemplate.expire( "max" ,tempTime,TimeUnit.SECONDS); |
通过Jedis操作redis
这里在pom文件中引入依赖
1 2 3 4 5 6 | <!--Jedis的jar--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version> 2.9 . 0 </version> </dependency> |
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | //连接本地的 Redis 服务 Jedis jedis = new Jedis( "ip地址" ,端口); jedis.auth( "mypassowrd" ); System.out.println( "连接成功" ); //清空redis String s = jedis.flushAll(); System.out.println(s); // 获取所有key并输出 Set<String> keys = jedis.keys( "*" ); Iterator<String> it=keys.iterator() ; while (it.hasNext()){ String key = it.next(); System.out.println(key); } |
本博客文章均已测试验证,欢迎评论、交流、点赞。
部分文章来源于网络,如有侵权请联系删除。
转载请注明原文链接:https://www.cnblogs.com/sueyyyy/p/10006264.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix