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 List
getMapValueByKeyFromRedis(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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Http工具类java http调用
下一篇:java ArrayList size 1 没有元素 list.size() = 1 但是显示 All elements are null

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月18日 15时04分22秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章