1.添加依赖
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-starter-data-redis</artifactId> |
| </dependency> |
2.添加application
| |
| #####################redis |
| spring.redis.database=0 |
| spring.redis.host=localhost |
| spring.redis.port=6379 |
| # 连接池最大连接数(使用负值表示没有限制) |
| spring.redis.jedis.pool.max-active=8 |
| # 连接池最大阻塞等待时间(使用负值表示没有限制) |
| spring.redis.jedis.pool.max-wait=-1 |
| # 连接池中的最小空闲连接 |
| spring.redis.jedis.pool.min-idle=0 |
| # 连接池中的最大空闲连接 |
| spring.redis.jedis.pool.max-idle=8 |
| # 连接超时时间(毫秒) |
| spring.redis.timeout=10000 |
3.redisutils
| package com.lt.congateway.utils; |
| |
| import org.springframework.data.redis.core.RedisTemplate; |
| import org.springframework.stereotype.Component; |
| import org.springframework.util.CollectionUtils; |
| |
| import javax.annotation.Resource; |
| import java.util.Collection; |
| import java.util.List; |
| import java.util.Map; |
| import java.util.concurrent.TimeUnit; |
| |
| |
| |
| |
| |
| |
| @Component |
| public class JedisUtils { |
| @Resource |
| private RedisTemplate redisTemplate; |
| |
| |
| |
| |
| |
| |
| public Boolean expire(String key, long time) { |
| try { |
| if (time > 0) { |
| redisTemplate.expire(key, time, TimeUnit.SECONDS); |
| } |
| return true; |
| } catch (Exception e) { |
| e.printStackTrace(); |
| return false; |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| public Long getExpire(String key) { |
| Long expire = redisTemplate.getExpire(key); |
| return expire; |
| } |
| |
| |
| |
| |
| |
| |
| |
| public boolean hasKey(String key) { |
| try { |
| return redisTemplate.hasKey(key); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| return false; |
| } |
| } |
| |
| |
| |
| |
| |
| |
| @SuppressWarnings("unchecked") |
| public void del(String... key) { |
| if (key != null && key.length > 0) { |
| if (key.length == 1) { |
| redisTemplate.delete(key[0]); |
| } else { |
| redisTemplate.delete(CollectionUtils.arrayToList(key)); |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| public boolean del(final String key) { |
| |
| Boolean ret = redisTemplate.delete(key); |
| return ret != null && ret; |
| } |
| |
| |
| |
| |
| |
| |
| |
| public void set(final String key, final Object value) { |
| redisTemplate.opsForValue().set(key, value, 1, TimeUnit.MINUTES); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public void set(final String key, final Object value, final long timeout) { |
| redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS); |
| } |
| |
| |
| |
| |
| |
| |
| |
| public Object get(final String key) { |
| return redisTemplate.opsForValue().get(key); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public void hPut(final String key, final String hKey, final Object value) { |
| redisTemplate.opsForHash().put(key, hKey, value); |
| } |
| |
| |
| |
| |
| |
| |
| public void mset(Map map) { |
| this.redisTemplate.opsForValue().multiSet(map); |
| } |
| |
| |
| |
| |
| |
| |
| |
| public List mget(Collection collection) { |
| List list = this.redisTemplate.opsForValue().multiGet(collection); |
| return list; |
| } |
| |
| |
| |
| |
| |
| |
| |
| public void hPutAll(final String key, final Map<String, Object> values) { |
| |
| redisTemplate.opsForHash().putAll(key, values); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| public Object hGet(final String key, final String hKey) { |
| |
| return redisTemplate.opsForHash().get(key, hKey); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| public List<Object> hMultiGet(final String key, final Collection<Object> hKeys) { |
| |
| return redisTemplate.opsForHash().multiGet(key, hKeys); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public long sSet(final String key, final Object... values) { |
| Long count = redisTemplate.opsForSet().add(key, values); |
| return count == null ? 0 : count; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| public long sDel(final String key, final Object... values) { |
| Long count = redisTemplate.opsForSet().remove(key, values); |
| return count == null ? 0 : count; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public long lPush(final String key, final Object value) { |
| Long count = redisTemplate.opsForList().rightPush(key, value); |
| return count == null ? 0 : count; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| public long lPushAll(final String key, final Collection<Object> values) { |
| Long count = redisTemplate.opsForList().rightPushAll(key, values); |
| return count == null ? 0 : count; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| public long lPushAll(final String key, final Object... values) { |
| Long count = redisTemplate.opsForList().rightPushAll(key, values); |
| return count == null ? 0 : count; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public List<Object> lGet(final String key, final int start, final int end) { |
| return redisTemplate.opsForList().range(key, start, end); |
| } |
| |
| } |
测试
| package com.lt.congateway; |
| |
| import com.alibaba.fastjson.JSON; |
| import com.lt.congateway.utils.JedisUtils; |
| import org.junit.jupiter.api.Test; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.boot.test.context.SpringBootTest; |
| import org.springframework.data.redis.core.RedisTemplate; |
| |
| import java.util.HashMap; |
| import java.util.List; |
| import java.util.Set; |
| import java.util.UUID; |
| |
| @SpringBootTest |
| class ConGatewayApplicationTests { |
| @Autowired |
| private RedisTemplate redisTemplate; |
| @Autowired |
| private JedisUtils jedisUtils; |
| |
| @Test |
| void test() { |
| |
| |
| |
| |
| HashMap<String, Object> stringObjectHashMap = new HashMap<>(16); |
| for (int i = 0; i < 10; i++) { |
| String uuid = UUID.randomUUID().toString(); |
| stringObjectHashMap.put(uuid, uuid + ":" + i); |
| } |
| Set<String> strings = stringObjectHashMap.keySet(); |
| System.out.println(JSON.toJSONString(stringObjectHashMap)); |
| this.jedisUtils.mset(stringObjectHashMap); |
| List list = this.jedisUtils.mget(strings); |
| System.out.println(JSON.toJSONString(list)); |
| } |
| |
| } |
测试通过说明集成ok
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)