src/main/java/org/starrier/common/utils/JedisUtil.java
package org.starrier.common.utils; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import parquet.org.slf4j.Logger;import parquet.org.slf4j.LoggerFactory;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool; /** * @author imperator * @date 2019-09-10 */@Componentpublic class JedisUtil { private static final Logger LOGGER = LoggerFactory.getLogger(JedisUtil.class); @Autowired private JedisPool jedisPool; private Jedis getJedis() { return jedisPool.getResource(); } /** * 设值 * * @param key * @param value * @return */Similar blocks of code found in 2 locations. Consider refactoring. public String set(String key, String value) { Jedis jedis = null; try { jedis = getJedis(); return jedis.set(key, value); } catch (Exception e) { LOGGER.error("set key: {} value: {} error", key, value, e); return null; } finally { close(jedis); } } /** * 设值 * * @param key * @param value * @param expireTime 过期时间, 单位: s * @return */ public String set(String key, String value, int expireTime) { Jedis jedis = null; try { jedis = getJedis(); return jedis.setex(key, expireTime, value); } catch (Exception e) { LOGGER.error("set key:{} value:{} expireTime:{} error", key, value, expireTime, e); return null; } finally { close(jedis); } } /** * 设值 * * @param key * @param value * @return */Similar blocks of code found in 2 locations. Consider refactoring. public Long setnx(String key, String value) { Jedis jedis = null; try { jedis = getJedis(); return jedis.setnx(key, value); } catch (Exception e) { LOGGER.error("set key:{} value:{} error", key, value, e); return null; } finally { close(jedis); } } /** * 取值 * * @param key * @return */Similar blocks of code found in 2 locations. Consider refactoring. public String get(String key) { Jedis jedis = null; try { jedis = getJedis(); return jedis.get(key); } catch (Exception e) { LOGGER.error("get key:{} error", key, e); return null; } finally { close(jedis); } } /** * 删除key * * @param key * @return */Similar blocks of code found in 2 locations. Consider refactoring. public Long del(String key) { Jedis jedis = null; try { jedis = getJedis(); return jedis.del(key.getBytes()); } catch (Exception e) { LOGGER.error("del key:{} error", key, e); return null; } finally { close(jedis); } } /** * 判断key是否存在 * * @param key * @return */Similar blocks of code found in 2 locations. Consider refactoring. public Boolean exists(String key) { Jedis jedis = null; try { jedis = getJedis(); return jedis.exists(key.getBytes()); } catch (Exception e) { LOGGER.error("exists key:{} error", key, e); return null; } finally { close(jedis); } } /** * 设值key过期时间 * * @param key * @param expireTime 过期时间, 单位: s * @return */ public Long expire(String key, int expireTime) { Jedis jedis = null; try { jedis = getJedis(); return jedis.expire(key.getBytes(), expireTime); } catch (Exception e) { LOGGER.error("expire key:{} error", key, e); return null; } finally { close(jedis); } } /** * 获取剩余时间 * * @param key * @return */Similar blocks of code found in 2 locations. Consider refactoring. public Long ttl(String key) { Jedis jedis = null; try { jedis = getJedis(); return jedis.ttl(key); } catch (Exception e) { LOGGER.error("ttl key:{} error", key, e); return null; } finally { close(jedis); } } private void close(Jedis jedis) { if (null != jedis) { jedis.close(); } }}