Java封装读取Redis数据库数据的工具类【杭州多测师_王sir】
package cn.duoceshi.springbootdemo.utils; import org.springframework.stereotype.Component; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; public class RedisUtils { private static JedisPool jedisPool; static { Map<String, String> props = PropertyUtils.getAll(RedisUtils.class.getResourceAsStream("/application.properties")); String ip = props.get("redis.host"); int port = Integer.parseInt(props.get("redis.port")); int timeout = 60000; String password = props.get("redis.password"); JedisPoolConfig config = new JedisPoolConfig(); config.setMaxActive(10); config.setMaxIdle(5); config.setMaxWait(5000); config.setTestOnBorrow(true); config.setTestOnReturn(true); config.setTestWhileIdle(true); config.setMinEvictableIdleTimeMillis(60000L); config.setTimeBetweenEvictionRunsMillis(3000L); config.setNumTestsPerEvictionRun(-1); jedisPool = new JedisPool(config, ip, port, timeout, password); } public static void close(Jedis jedis) { try { jedisPool.returnResource(jedis); } catch (Exception e) { if (jedis.isConnected()) { jedis.quit(); jedis.disconnect(); } } } public static String get(String key) { String value = null; Jedis jedis = null; try { jedis = jedisPool.getResource(); value = jedis.get(key); } finally { //返还到连接池 close(jedis); } return value; } public static void set(String key, String value) { Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.set(key, value); } finally { //返还到连接池 close(jedis); } } public static Set<String> keys(String keyFormat) { Jedis jedis = null; Set<String> set = new HashSet<>(); try { jedis = jedisPool.getResource(); set = jedis.keys(keyFormat); } finally { //返还到连接池 close(jedis); } return set; } public static void hset(String key, String field, String value) { Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.hset(key, field, value); } finally { //返还到连接池 close(jedis); } } /** * 获取数据 * * @param key * @return */ public static String hget(String key, String field) { String value = null; Jedis jedis = null; try { jedis = jedisPool.getResource(); value = jedis.hget(key, field); } finally { //返还到连接池 close(jedis); } return value; } /** * 发布消息 * * @param channel * @param message */ public static void publish(String channel, String message) { Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.publish(channel, message); } finally { //返还到连接池 close(jedis); } } /** * 向名为key的list尾部添加一个值为value的元素 * * @param key * @param value */ public static void rpush(String key, String value) { Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.rpush(key, value); } finally { //返还到连接池 close(jedis); } } /** * 向名为key的list尾部添加一个值为value的元素 * @param key * @param valueList */ public static void rpush(String key,List<String> valueList){ Jedis jedis = null; try { jedis = jedisPool.getResource(); String[] valueArray = new String[valueList.size()]; valueList.toArray(valueArray); jedis.rpush(key, valueArray); } catch (Exception e) { //释放redis对象 jedisPool.returnBrokenResource(jedis); e.printStackTrace(); } finally { //返还到连接池 close(jedis); } } public static String lindex(String key, Integer index) { Jedis jedis = null; String value = null; try { jedis = jedisPool.getResource(); value = jedis.lindex(key, index); } finally { //返还到连接池 close(jedis); } return value; } public static void hmset(Object key, Map<String, String> hash) { Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.hmset(key.toString(), hash); } finally { //返还到连接池 close(jedis); } } public static void hmset(Object key, Map<String, String> hash, int time) { Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.hmset(key.toString(), hash); jedis.expire(key.toString(), time); } finally { //返还到连接池 close(jedis); } } public static List<String> hmget(Object key, String... fields) { List<String> result = null; Jedis jedis = null; try { jedis = jedisPool.getResource(); result = jedis.hmget(key.toString(), fields); } finally { //返还到连接池 close(jedis); } return result; } public static Set<String> hkeys(String key) { Set<String> result = null; Jedis jedis = null; try { jedis = jedisPool.getResource(); result = jedis.hkeys(key); } finally { //返还到连接池 close(jedis); } return result; } public static List<String> lrange(String key, int from, int to) { List<String> result = null; Jedis jedis = null; try { jedis = jedisPool.getResource(); result = jedis.lrange(key, from, to); } finally { //返还到连接池 close(jedis); } return result; } public static Map<String, String> hgetAll(String key) { Map<String, String> result = null; Jedis jedis = null; try { jedis = jedisPool.getResource(); result = jedis.hgetAll(key); } finally { //返还到连接池 close(jedis); } return result; } public static void del(String key) { Jedis jedis = null; try { jedis = jedisPool.getResource(); jedis.del(key); } finally { //返还到连接池 close(jedis); } } public static long llen(String key) { long len = 0; Jedis jedis = null; try { jedis = jedisPool.getResource(); len = jedis.llen(key); } finally { //返还到连接池 close(jedis); } return len; } public static void main(String[] args) { System.out.println(RedisUtils.get("name")); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2022-08-30 杭州市民卡面试题【杭州多测师】【杭州多测师_王sir】
2020-08-30 java编程语言中的多态【多测师_王sir】