RedisUtil.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package com.goafanti.common.utils;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.ObjectInputStream;
  6. import java.io.ObjectOutputStream;
  7. import java.util.List;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.stereotype.Component;
  11. import com.alibaba.fastjson.JSON;
  12. import redis.clients.jedis.Jedis;
  13. import redis.clients.jedis.JedisPool;
  14. /**
  15. * redis辅助类
  16. * @author
  17. *
  18. */
  19. @Component
  20. public class RedisUtil {
  21. @Value(value = "${jedis.host}")
  22. private String host;
  23. @Value(value = "${jedis.password}")
  24. private String pwd;
  25. @Autowired
  26. private JedisPool pool;//创建代理对象
  27. // /**
  28. // * 初始化Redis连接池
  29. // */
  30. // public RedisUtil() {
  31. // if (pool==null) {
  32. // try {
  33. // JedisPoolConfig config = new JedisPoolConfig();
  34. // config.setBlockWhenExhausted(false);
  35. // config.setEvictionPolicyClassName("org.apache.commons.pool2.impl.DefaultEvictionPolicy");
  36. // config.setJmxEnabled(true);
  37. // config.setJmxNamePrefix("pool");
  38. // config.setLifo(true);
  39. // config.setMaxIdle(8);
  40. // config.setMaxTotal(8);
  41. // config.setMaxWaitMillis(-1);
  42. // config.setMinEvictableIdleTimeMillis(1800000L);
  43. // config.setMinIdle(0);
  44. // config.setNumTestsPerEvictionRun(3);
  45. // config.setSoftMinEvictableIdleTimeMillis(1800000L);
  46. // config.setTestOnBorrow(false);
  47. // config.setTestWhileIdle(false);
  48. // config.setTimeBetweenEvictionRunsMillis(-1);
  49. // pool = new JedisPool(config, host, 6379, 10000, pwd);
  50. // } catch (Exception e) {
  51. // e.printStackTrace();
  52. // }
  53. // }
  54. // }
  55. public boolean presence(String key){
  56. Jedis jedis = pool.getResource();
  57. boolean bool=true;
  58. try {
  59. bool =jedis.exists(key);//进行判断
  60. }catch(Exception e){
  61. e.printStackTrace();//输出错误信息
  62. }finally {
  63. jedis.close();//释放资源
  64. }
  65. return bool;
  66. }
  67. /**
  68. * 将缓存中数据进行反序列化
  69. * @param key
  70. * @param clazz
  71. * @return
  72. */
  73. @SuppressWarnings("unchecked")
  74. public <T>List<T> getObject(String key,Class<T> clazz){
  75. Jedis jedis = pool.getResource();
  76. ByteArrayInputStream is=null;
  77. ObjectInputStream ois=null;
  78. try {
  79. //将缓存中数据存入byte数组中
  80. byte[] b = jedis.get(key.getBytes());
  81. is= new ByteArrayInputStream(b);
  82. ois = new ObjectInputStream(is);
  83. return(List<T>)ois.readObject();
  84. } catch (Exception e) {
  85. e.printStackTrace();
  86. }finally{
  87. try {
  88. is.close();
  89. ois.close();
  90. } catch (IOException e) {
  91. e.printStackTrace();
  92. }
  93. jedis.close();
  94. }
  95. return null;
  96. }
  97. /**
  98. * 将对象进行序列化存入redis中
  99. * @param object
  100. * @param key
  101. */
  102. public void saveObject(Object object,String key){
  103. Jedis jedis=pool.getResource();
  104. ByteArrayOutputStream os=null;
  105. ObjectOutputStream oos=null;
  106. try {
  107. os=new ByteArrayOutputStream();
  108. oos =new ObjectOutputStream(os);
  109. byte[] b=os.toByteArray();
  110. JSON.toJSONString(b);
  111. jedis.set(key.getBytes(), b);
  112. } catch (Exception e) {
  113. e.printStackTrace();
  114. }finally{
  115. try {
  116. os.close();
  117. oos.close();
  118. } catch (Exception e2) {
  119. e2.printStackTrace();
  120. }
  121. jedis.close();
  122. }
  123. }
  124. // 把List集合对象转换成json格式保存到指定的键中
  125. public void saveJsonArray(Object object, String key) {
  126. Jedis jedis = pool.getResource();
  127. try {
  128. // 格式化成Json字符串
  129. String array = JSON.toJSONString(object);
  130. jedis.set(key, array.toString()); // 存入缓存
  131. } finally {
  132. jedis.close();
  133. }
  134. }
  135. public void setString(String key, String value) {
  136. Jedis jedis = pool.getResource();
  137. try {
  138. jedis.set(key,value); // 存入缓存
  139. } finally {
  140. jedis.close();
  141. }
  142. }
  143. public void deleteString(String key) {
  144. Jedis jedis = pool.getResource();
  145. try {
  146. jedis.del(key);
  147. } finally {
  148. jedis.close();
  149. }
  150. }
  151. public String getString(String key) {
  152. Jedis jedis = pool.getResource();
  153. String str=new String();
  154. try {
  155. str=jedis.get(key);
  156. } finally {
  157. jedis.close();
  158. }
  159. return str;
  160. }
  161. // 通过键取出Json字符串并转换成 <T>这个T所指定的类型
  162. public <T> T getJsonArray(String key, Class<T> clazz) {
  163. Jedis jedis = pool.getResource();
  164. try {
  165. String str = jedis.get(key);
  166. // 把字符串转换回集合对象 clazz是指定的类型
  167. return JSON.parseObject(str, clazz);
  168. } finally {
  169. jedis.close();
  170. }
  171. }
  172. }