redis工具类java
发布日期:2021-06-29 11:47:04
浏览次数:2
分类:技术文章
本文共 5372 字,大约阅读时间需要 17 分钟。
package com.XXX.util;import java.util.Calendar;import java.util.Date;import java.util.List;import java.util.Map;import java.util.Set;import javax.annotation.PostConstruct;import org.springframework.beans.factory.annotation.Value;import lombok.extern.slf4j.Slf4j;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;/** * 操作Redis的类 * * @author liuxiao * */@Slf4jpublic class RedisUtil { @Value("${redis.host}") private String host; @Value("${redis.password}") private String password; @Value("${redis.port}") private Integer port; @Value("${redis.maxInst}") private Integer maxInst; @Value("${redis.maxIdle}") private Integer maxIdle; @Value("${redis.maxWait}") private Integer maxWait; private static JedisPool jedisPool; private JedisPoolConfig conf = new JedisPoolConfig(); private RedisUtil() { } @PostConstruct private void init() { conf.setMaxTotal(maxInst); conf.setMaxIdle(maxIdle); conf.setMaxWaitMillis(maxWait); conf.setTestOnBorrow(true); createJedisPool(); } private void createJedisPool() { try { if (jedisPool == null || (jedisPool != null && jedisPool.isClosed())) { jedisPool = new JedisPool(conf, host, port, 2000, password); } } catch (Exception e) { log.error("can not create JedisPool.", e); } } /** * 获取Jedis连接 * * @return */ public static Jedis getConnection() { Jedis jedis = null; if (jedisPool != null && !jedisPool.isClosed()) { try { jedis = jedisPool.getResource(); } catch (Exception e) { close(jedis); log.error("can not get jedis from JedisPool.", e); } } else { log.error("JedisPool is closed!"); throw new RuntimeException("JedisPool is closed!"); } return jedis; } /** * 关闭Jedis连接 * * @param jedis */ public static void close(Jedis jedis) { if (jedis != null) { try { jedis.close(); } catch (Exception e) { log.error("can not return jedis to JedisPool.", e); } } } /** * 从Redis上获取数据 * ** @param key * @return * Modifications: * Modifier wangdefeng; 2017年4月21日; Create new Method getStringFromRedis **/ public static String getStringFromRedis(String key) { Jedis jedis = null; String result = null; try { jedis = RedisUtil.getConnection(); result = jedis.get(key); } catch (Exception e) { e.printStackTrace(); log.error(e.getMessage(), e); } finally { if (jedis != null) { RedisUtil.close(jedis); } } return result; } /** * 向redis存数据 * ** @param key * @param value * Modifications: * Modifier wangdefeng; 2017年4月21日; Create new Method putStringToRedis **/ public static void putStringToRedis(String key, String value) { Jedis jedis = null; try { jedis = RedisUtil.getConnection(); jedis.set(key, value); jedis.expire(key, getExpireSecond()); } catch (Exception e) { e.printStackTrace(); log.error(e.getMessage(), e); } finally { if (null != jedis) { RedisUtil.close(jedis); } } } /** * 根据redis的key和map中的key获取value * @param key * @param field * @return */ public ListgetMapValueByKeyFromRedis(String key,String field){ Jedis jedis = null; List result = null; try { jedis = RedisUtil.getConnection(); result = jedis.hmget(key,field); } catch (Exception e) { e.printStackTrace(); log.error(e.getMessage(), e); } finally { if (jedis != null) { RedisUtil.close(jedis); } } return result; } /** * 获取map中所有的key * @param key * @return */ public Set getMapKeyFromRedis(String key){ Jedis jedis = null; Set result = null; try { jedis = RedisUtil.getConnection(); result = jedis.hkeys(key); } catch (Exception e) { e.printStackTrace(); log.error(e.getMessage(), e); } finally { if (jedis != null) { RedisUtil.close(jedis); } } return result; } /** * 将map缓存到redis中 * @param key * @param map */ public void putMapToRedis(String key,Map map){ Jedis jedis = null; try { jedis = RedisUtil.getConnection(); jedis.hmset(key, map); jedis.expire(key, getExpireSecond()); } catch (Exception e) { e.printStackTrace(); log.error(e.getMessage(), e); } finally { if (null != jedis) { RedisUtil.close(jedis); } } } /** * 获取到当天24点剩余时间,单位(s) * * * @return * Modifications: * Modifier wangdefeng; 2017年4月21日; Create new Method getExpireSecond **/ private static int getExpireSecond() { Date date = new Date(); Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.set(Calendar.HOUR, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); cal.add(Calendar.DATE, 1); Date nextDate = cal.getTime(); return (int) (nextDate.getTime() - date.getTime()) / 1000; } /** * 删除redis数据 * @param key * @return */ public static void delFromRedis(String key) { Jedis jedis = null; try { jedis = RedisUtil.getConnection(); jedis.del(key); } catch (Exception e) { e.printStackTrace(); log.error(e.getMessage(), e); } finally { if (jedis != null) { RedisUtil.close(jedis); } } }}
转载地址:https://blog.csdn.net/zzchances/article/details/105507612 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2024年04月18日 15时04分22秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
5款好用的 REST API 工具,推荐给你!
2019-04-29
1.3 万亿条数据查询,如何做到毫秒级响应?
2019-04-29
卧槽,又来一个 Java 神器!!
2019-04-29
VSCode 上竟然也能约会,谈对象了???
2019-04-29
推荐一个基于 Spring Boot + Mybatis + Vue 的代码生成器
2019-04-29
刚刚上任3位“杰青”副校长后,又聘任10名清北博士任教,这所高校突围双一流建设!...
2019-04-29
12306架构到底是不是国内最牛逼的架构
2019-04-29
MyBatis 的执行流程,写得太好了!
2019-04-29
一顿骚操作!我将 SQL 耗时从 30248.271s 优化到 0.001s
2019-04-29
再见,杀毒软件之父,王江民!
2019-04-29
太赞了!这个5w字的《Java面试手册》PDF 一站封神!
2019-04-29
跨平台应用即将消亡!
2019-04-29
P0级重大事故:超卖了100瓶飞天茅台,整个项目组慌得一逼~
2019-04-29
你看得上瘾的斗鱼,首次开源了自家项目
2019-04-29
速来,清华毕业大牛分享出Redis实战视频及相关面试文档
2019-04-29
这些JAVA面试问题(第一问)
2019-04-29
java知识面试,这么题面试官很难不问到你,千万不要错过了
2019-04-29
Spring5新宠:PathPattern,AntPathMatcher:那我走?
2019-04-29
这15道关于MySQL的题目,不来看看就可惜了
2019-04-29
这十大JVM面试题你可不能再错过了,都是精华中的精华
2019-04-29