Redised.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package com.yiidata.intergration.common.cache;
  2. import com.yiidata.intergration.common.cache.serde.JdkSerializer;
  3. import com.yiidata.intergration.common.cache.serde.Serializer;
  4. import com.yiidata.intergration.common.cache.serde.StringSerializer;
  5. import org.apache.commons.lang.StringUtils;
  6. import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import redis.clients.jedis.Jedis;
  10. import redis.clients.jedis.JedisPool;
  11. import java.io.Closeable;
  12. import java.io.IOException;
  13. import java.io.Serializable;
  14. import java.util.Set;
  15. /**
  16. * <pre>
  17. *
  18. * Created by zhaopx.
  19. * User: zhaopx
  20. * Date: 2019/8/21
  21. * Time: 18:55
  22. *
  23. * </pre>
  24. *
  25. * @author zhaopx
  26. */
  27. public class Redised implements ICache<Serializable>, Closeable {
  28. private static Logger logger = LoggerFactory.getLogger(Redised.class);
  29. /**
  30. * Redis 的 Java Client
  31. */
  32. JedisPool redisPool;
  33. /**
  34. * redis db index
  35. */
  36. private final String host;
  37. /**
  38. * redis db index
  39. */
  40. private final int port;
  41. /**
  42. * redis db password
  43. */
  44. private final String password;
  45. /**
  46. * redis db index
  47. */
  48. private final int db;
  49. /**
  50. * KEY 序列化
  51. */
  52. private final Serializer<String> KEY_SERDE = new StringSerializer();
  53. /**
  54. * Redis Cache 序列化方式
  55. */
  56. private Serializer<Serializable> VALUE_SERDE = new JdkSerializer();
  57. public Redised() {
  58. this("localhost", 6379, 0);
  59. }
  60. public Redised(String host, int port){
  61. this(host, port, 0);
  62. }
  63. public Redised(String host, int port, int db){
  64. this(host, port, null, db);
  65. }
  66. public Redised(String host, int port, String password){
  67. this(host, port, password, 0);
  68. }
  69. public Redised(String host, int port, String password, int db){
  70. this.db = db;
  71. this.host = host;
  72. this.port = port;
  73. this.password = password;
  74. GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
  75. poolConfig.setMaxIdle(100);
  76. poolConfig.setMinIdle(5);
  77. //jedis = new Jedis(host, port, 30000, 3000);
  78. if(StringUtils.isNotBlank(password)) {
  79. this.redisPool = new JedisPool(poolConfig, host, port, 60000, password);
  80. } else {
  81. this.redisPool = new JedisPool(poolConfig, host, port);
  82. }
  83. }
  84. @Override
  85. public void add(String key, Serializable value) {
  86. if(value == null) {
  87. return;
  88. }
  89. try(Jedis jedis = redisPool.getResource();) {
  90. jedis.select(db);
  91. jedis.set(KEY_SERDE.serialize(key), VALUE_SERDE.serialize(value));
  92. }
  93. }
  94. @Override
  95. public void add(String key, int exp, Serializable value) {
  96. byte[] keybytes = KEY_SERDE.serialize(key);
  97. try(Jedis jedis = redisPool.getResource();) {
  98. jedis.select(db);
  99. jedis.set(keybytes, VALUE_SERDE.serialize(value));
  100. jedis.expire(keybytes, exp);
  101. }
  102. }
  103. @Override
  104. public Serializable get(String key) {
  105. if(key == null) {
  106. return null;
  107. }
  108. try(Jedis jedis = redisPool.getResource();) {
  109. jedis.select(db);
  110. byte[] bytes = jedis.get(KEY_SERDE.serialize(key));
  111. if (bytes == null) {
  112. return null;
  113. }
  114. return VALUE_SERDE.deserialize(bytes);
  115. }
  116. }
  117. /**
  118. * 移除某条缓存
  119. * @param key 缓存的 KEY
  120. * @return
  121. */
  122. @Override
  123. public Serializable remove(String key) {
  124. if(key == null) {
  125. return null;
  126. }
  127. logger.info("remove cache key: {}", key);
  128. try(Jedis jedis = redisPool.getResource();) {
  129. jedis.select(db);
  130. return jedis.del(KEY_SERDE.serialize(key));
  131. }
  132. }
  133. @Override
  134. public int removeByPrefix(String prefix) {
  135. try(Jedis jedis = redisPool.getResource();) {
  136. jedis.select(db);
  137. Set<String> keys = jedis.keys(prefix + "*");
  138. if (keys != null && keys.size() > 0) {
  139. for (String key : keys) {
  140. remove(key);
  141. }
  142. }
  143. return keys == null ? 0 : keys.size();
  144. }
  145. }
  146. @Override
  147. public void clear() {
  148. try(Jedis jedis = redisPool.getResource();) {
  149. jedis.select(db);
  150. jedis.flushDB();
  151. }
  152. }
  153. @Override
  154. public int size() {
  155. try(Jedis jedis = redisPool.getResource();) {
  156. jedis.select(db);
  157. return jedis.dbSize().intValue();
  158. }
  159. }
  160. @Override
  161. public void close() throws IOException {
  162. redisPool.close();
  163. }
  164. @Override
  165. public void shutdown() {
  166. try {
  167. close();
  168. } catch (IOException e) {
  169. }
  170. }
  171. /**
  172. * 序列化方式
  173. * @param valueSerde
  174. */
  175. public void setValueSerde(Serializer<Serializable> valueSerde) {
  176. this.VALUE_SERDE = valueSerde;
  177. }
  178. public String getHost() {
  179. return host;
  180. }
  181. public int getPort() {
  182. return port;
  183. }
  184. public String getPassword() {
  185. return password;
  186. }
  187. public int getDb() {
  188. return db;
  189. }
  190. public JedisPool getRedisPool() {
  191. return redisPool;
  192. }
  193. }