RedisUtils.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package com.iamberry.redis;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.iamberry.wechat.tools.NameUtils;
  4. import com.iamberry.wechat.tools.payUtil.DatetimeUtil;
  5. import org.apache.log4j.Logger;
  6. import redis.clients.jedis.Jedis;
  7. import redis.clients.jedis.JedisPool;
  8. import redis.clients.jedis.JedisPoolConfig;
  9. import java.util.Date;
  10. import java.util.List;
  11. /**
  12. * redis工具类
  13. * @author root
  14. */
  15. public class RedisUtils {
  16. /**ip*/
  17. private static final String IP = NameUtils.getConfig("redis_host");
  18. /**端口*/
  19. private static final int PORT = Integer.parseInt(NameUtils.getConfig("redis_port"));
  20. /**密码(原始默认是没有密码)*/
  21. private static final String AUTH = NameUtils.getConfig("redis_auth");
  22. /**最大连接数*/
  23. private static int MAX_ACTIVE = Integer.parseInt(NameUtils.getConfig("redis_max_active"));
  24. /**设置最大空闲数*/
  25. private static int MAX_IDLE = Integer.parseInt(NameUtils.getConfig("redis_max_idle"));
  26. /**最大连接时间*/
  27. private static int MAX_WAIT = Integer.parseInt(NameUtils.getConfig("redis_max_wait"));
  28. /**超时时间*/
  29. private static int TIMEOUT = Integer.parseInt(NameUtils.getConfig("redis_timeout"));
  30. private static JedisPool pool = null;
  31. private static Logger logger = Logger.getLogger(RedisUtils.class);
  32. static {
  33. /*
  34. * 初始化线程池(只在类进行初始化的时候进行一次)
  35. */
  36. JedisPoolConfig config = new JedisPoolConfig();
  37. config.setMaxTotal(MAX_ACTIVE);
  38. config.setMaxIdle(MAX_IDLE);
  39. config.setMaxWaitMillis(MAX_WAIT);
  40. config.setTestOnBorrow(false);
  41. config.setTestOnReturn(true);
  42. config.setTestWhileIdle(true);
  43. pool = new JedisPool(config, IP, PORT, TIMEOUT);
  44. }
  45. /**
  46. * 获取连接
  47. */
  48. private static synchronized Jedis getJedis() {
  49. try {
  50. if(pool != null) {
  51. Jedis jedis = pool.getResource();
  52. jedis.auth(AUTH);
  53. return jedis;
  54. } else {
  55. throw new RuntimeException("获取失败Redis实例失败,请重试");
  56. }
  57. } catch (Exception e) {
  58. logger.error("", e);
  59. throw e;
  60. }
  61. }
  62. /**
  63. * 插入对象
  64. * @param key
  65. * @param obj
  66. * @return
  67. */
  68. public static boolean put(String key, Object obj) {
  69. Jedis jedis = null;
  70. try {
  71. String value = JSONObject.toJSONString(obj);
  72. jedis = getJedis();
  73. return "ok".equalsIgnoreCase(jedis.set(key, value));
  74. } catch (Exception e) {
  75. logger.error("", e);
  76. } finally {
  77. colse(jedis);
  78. }
  79. return false;
  80. }
  81. /**
  82. * 插入对象
  83. * @param key
  84. * @param value
  85. * @return
  86. */
  87. public static boolean put(String key, String value) {
  88. Jedis jedis = null;
  89. try {
  90. jedis = getJedis();
  91. return "ok".equalsIgnoreCase(jedis.set(key, value));
  92. } catch (Exception e) {
  93. logger.error("", e);
  94. } finally {
  95. colse(jedis);
  96. }
  97. return false;
  98. }
  99. /**
  100. * 获取单个对象
  101. * @param key
  102. * @param clazz
  103. * @return
  104. */
  105. public static <T> T get(String key, Class<T> clazz) {
  106. Jedis jedis = null;
  107. try {
  108. jedis = getJedis();
  109. String result = jedis.get(key);
  110. return JSONObject.parseObject(result, clazz);
  111. } catch (Exception e) {
  112. logger.error("", e);
  113. } finally {
  114. colse(jedis);
  115. }
  116. return null;
  117. }
  118. /**
  119. * 获取单个String对象
  120. * @param key
  121. * @return
  122. */
  123. public static String get(String key) {
  124. Jedis jedis = null;
  125. try {
  126. jedis = getJedis();
  127. return jedis.get(key);
  128. } catch (Exception e) {
  129. logger.error("", e);
  130. } finally {
  131. colse(jedis);
  132. }
  133. return null;
  134. }
  135. /**
  136. * 获取list集合
  137. * @param key
  138. * @param clazz
  139. * @return
  140. */
  141. public static <T> List<T> list(String key, Class<T> clazz) {
  142. Jedis jedis = null;
  143. try {
  144. jedis = getJedis();
  145. String result = jedis.get(key);
  146. return JSONObject.parseArray(result, clazz);
  147. } catch (Exception e) {
  148. logger.error("", e);
  149. } finally {
  150. colse(jedis);
  151. }
  152. return null;
  153. }
  154. /**
  155. * 删除key
  156. * @param key
  157. * @return
  158. */
  159. public static boolean del(String key) {
  160. Jedis jedis = null;
  161. try {
  162. jedis = getJedis();
  163. return jedis.del(key) >= 1;
  164. } catch (Exception e) {
  165. logger.error("", e);
  166. } finally {
  167. colse(jedis);
  168. }
  169. return false;
  170. }
  171. /**
  172. * 设置过期时间
  173. * @param key
  174. * @param date
  175. */
  176. public static void expire(String key, Date date) {
  177. Jedis jedis = null;
  178. try {
  179. jedis = getJedis();
  180. if (null != date) {
  181. long nowTime = System.currentTimeMillis();
  182. long dateTime = date.getTime();
  183. if (dateTime > nowTime) {
  184. jedis.expire(key, (int)(dateTime - nowTime) / 1000);
  185. }
  186. }
  187. }catch (Exception e){
  188. logger.error("", e);
  189. }finally {
  190. colse(jedis);
  191. }
  192. }
  193. /**
  194. * 关闭连接
  195. * @param jedis
  196. */
  197. private static void colse(Jedis jedis) {
  198. if(jedis != null) {
  199. jedis.close();
  200. }
  201. }
  202. }