jedis封装

不念不忘少年蓝@ 2023-01-15 15:24 191阅读 0赞

redisTemplate封装参考:
https://blog.csdn.net/leinminna/article/details/115999431

1.jedis 基础属性

  1. public class RedisUtils {
  2. private static JedisPool jedisPool;
  3. public static final String PREFIX = "0621B4751BC607DAB5EF2F391E4D2B69_";
  4. static Logger logger = Logger.getLogger(RedisUtils.class);
  5. private static final String REDIS_HOST_DEFAULT = "271.0.0.1";
  6. private static final int REDIS_PORT_DEFAULT = 6380;
  7. private static final int REDIS_MAXTOTAL_DEFAULT = 200;
  8. private static final int REDIS_MAXIDLE_DEFAULT = 25;
  9. private static final int REDIS_MAXWAIT_DEFAULT = 1000;
  10. private static final String REDIS_HOST = "redis.host";
  11. private static final String REDIS_PORT = "redis.port";
  12. private static final String REDIS_MAXTOTAL = "redis.maxTotal";
  13. private static final String REDIS_MAXIDLE = "redis.maxIdle";
  14. private static final String REDIS_MAXWAIT = "redis.maxWait";
  15. private static final String REDIS_PASSWORD = "redis.password";
  16. static {
  17. String host = SiteConfig.get(REDIS_HOST, REDIS_HOST_DEFAULT);
  18. int port = SiteConfig.getInt(REDIS_PORT, REDIS_PORT_DEFAULT);
  19. int maxTotal = SiteConfig.getInt(REDIS_MAXTOTAL, REDIS_MAXTOTAL_DEFAULT);
  20. int maxIdle = SiteConfig.getInt(REDIS_MAXIDLE, REDIS_MAXIDLE_DEFAULT);
  21. long maxWait = SiteConfig.getInt(REDIS_MAXWAIT, REDIS_MAXWAIT_DEFAULT);
  22. String password = SiteConfig.get(REDIS_PASSWORD);
  23. JedisPoolConfig config = new JedisPoolConfig();
  24. config.setMaxTotal(maxTotal);
  25. config.setMaxIdle(maxIdle);
  26. config.setMaxWaitMillis(maxWait);
  27. if (StringUtils.isEmpty(password)) {
  28. jedisPool = new JedisPool(config, host, port);
  29. } else {
  30. jedisPool = new JedisPool(config, host, port, 180000, password);
  31. }
  32. }
  33. ...
  34. }

2. String 封装

  1. /** * string_key string_value * * @param key string_key * @return string_value */
  2. public static String get(String key) {
  3. Jedis jedis = jedisPool.getResource();
  4. try {
  5. return jedis.get(key);
  6. } catch (Exception e) {
  7. logger.error(e, e);
  8. jedisPool.returnBrokenResource(jedis);
  9. jedis = null;
  10. throw e;
  11. } finally {
  12. if (jedis != null) {
  13. jedisPool.returnResource(jedis);
  14. }
  15. }
  16. }
  17. /** * string_key string_value * * @param key string_key * @param value string_value * @param seconds 有效时间段 */
  18. public static void set(String key, Serializable value, int seconds) {
  19. Jedis jedis = jedisPool.getResource();
  20. try {
  21. byte[] keyBytes = key.getBytes();
  22. jedis.set(keyBytes, SerializationUtil.serialize(value));
  23. jedis.expire(keyBytes, seconds);
  24. } catch (Exception e) {
  25. logger.error(e, e);
  26. jedisPool.returnBrokenResource(jedis);
  27. jedis = null;
  28. throw e;
  29. } finally {
  30. if (jedis != null) {
  31. jedisPool.returnResource(jedis);
  32. }
  33. }
  34. }
  35. /** * string_key string_value * * @param key string_key * @param value string_value */
  36. public static void set(String key, Serializable value) {
  37. Jedis jedis = jedisPool.getResource();
  38. try {
  39. jedis.set(key.getBytes(), SerializationUtil.serialize(value));
  40. } catch (Exception e) {
  41. logger.error(e, e);
  42. jedisPool.returnBrokenResource(jedis);
  43. jedis = null;
  44. throw e;
  45. } finally {
  46. if (jedis != null) {
  47. jedisPool.returnResource(jedis);
  48. }
  49. }
  50. }
  51. /** * string_key string_value * * @param key string_key * @param clazz T类 * @param <T> T 对象 * @return T 对象 */
  52. public static <T> T get(String key, Class<T> clazz) {
  53. Jedis jedis = jedisPool.getResource();
  54. try {
  55. byte[] data = jedis.get(key.getBytes());
  56. return data == null ? null : SerializationUtil.deserialize(data, clazz);
  57. } catch (Exception e) {
  58. logger.error(e, e);
  59. jedisPool.returnBrokenResource(jedis);
  60. jedis = null;
  61. throw e;
  62. } finally {
  63. if (jedis != null) {
  64. jedisPool.returnResource(jedis);
  65. }
  66. }
  67. }
  68. /** * string-key,string-value * * @param key string-key * @param clazz string-value 转 T 类 * @param seconds 重新设置过期时间(秒) * @param <T> T 的类型 * @return 返回 T 对象 */
  69. public static <T> T gas(String key, Class<T> clazz, int seconds) {
  70. Jedis jedis = jedisPool.getResource();
  71. try {
  72. byte[] keyBytes = key.getBytes();
  73. byte[] data = jedis.get(keyBytes);
  74. if (data != null) {
  75. jedis.expire(keyBytes, seconds);
  76. return SerializationUtil.deserialize(data, clazz);
  77. } else {
  78. return null;
  79. }
  80. } catch (Exception e) {
  81. logger.error(e, e);
  82. jedisPool.returnBrokenResource(jedis);
  83. jedis = null;
  84. throw e;
  85. } finally {
  86. if (jedis != null) {
  87. jedisPool.returnResource(jedis);
  88. }
  89. }
  90. }
  91. /** * string-key,string-value * * @param key string-key * @param seconds 重新设置过期时间(秒) * @return 返回 jsonString */
  92. public static String gas(String key, int seconds) {
  93. Jedis jedis = jedisPool.getResource();
  94. try {
  95. String str = jedis.get(key);
  96. if (str != null) {
  97. jedis.expire(str, seconds);
  98. }
  99. return str;
  100. } catch (Exception e) {
  101. logger.error(e, e);
  102. jedisPool.returnBrokenResource(jedis);
  103. jedis = null;
  104. throw e;
  105. } finally {
  106. if (jedis != null) {
  107. jedisPool.returnResource(jedis);
  108. }
  109. }
  110. }
  111. /** * 根据键删除值,批量操作,键不存不操作,返回键值对删除的个数,没有返回0 * * @param keys key 数组 */
  112. public static void del(String... keys) {
  113. Jedis jedis = jedisPool.getResource();
  114. try {
  115. jedis.del(keys);
  116. } catch (Exception e) {
  117. logger.error(e, e);
  118. jedisPool.returnBrokenResource(jedis);
  119. jedis = null;
  120. throw e;
  121. } finally {
  122. if (jedis != null) {
  123. jedisPool.returnResource(jedis);
  124. }
  125. }
  126. }
  127. /** * 删除 匹配的 key 对应的数据 * * @param pattern */
  128. public static void delPattern(String pattern) {
  129. Jedis jedis = jedisPool.getResource();
  130. try {
  131. Set<String> keys = jedis.keys(pattern);
  132. if (keys != null && keys.size() > 0) {
  133. Iterator<String> it = keys.iterator();
  134. while (it.hasNext()) {
  135. String key = it.next();
  136. jedis.del(key);
  137. }
  138. }
  139. } catch (Exception e) {
  140. logger.error(e, e);
  141. jedisPool.returnBrokenResource(jedis);
  142. jedis = null;
  143. throw e;
  144. } finally {
  145. if (jedis != null) {
  146. jedisPool.returnResource(jedis);
  147. }
  148. }
  149. }
  150. public static boolean exists(String key) {
  151. Jedis jedis = jedisPool.getResource();
  152. try {
  153. return jedis.exists(key);
  154. } catch (Exception e) {
  155. logger.error(e, e);
  156. jedisPool.returnBrokenResource(jedis);
  157. jedis = null;
  158. throw e;
  159. } finally {
  160. if (jedis != null) {
  161. jedisPool.returnResource(jedis);
  162. }
  163. }
  164. }
  165. public static void expire(String key, int seconds) {
  166. Jedis jedis = jedisPool.getResource();
  167. try {
  168. jedis.expire(key, seconds);
  169. } catch (Exception e) {
  170. logger.error(e, e);
  171. jedisPool.returnBrokenResource(jedis);
  172. jedis = null;
  173. throw e;
  174. } finally {
  175. if (jedis != null) {
  176. jedisPool.returnResource(jedis);
  177. }
  178. }
  179. }
  180. public static byte[] getBytes(String key) {
  181. Jedis jedis = jedisPool.getResource();
  182. try {
  183. return jedis.get(key.getBytes());
  184. } catch (Exception e) {
  185. logger.error(e, e);
  186. jedisPool.returnBrokenResource(jedis);
  187. jedis = null;
  188. throw e;
  189. } finally {
  190. if (jedis != null) {
  191. jedisPool.returnResource(jedis);
  192. }
  193. }
  194. }
  195. public static void setBytes(String key, byte[] value, int expire) {
  196. Jedis jedis = jedisPool.getResource();
  197. try {
  198. jedis.setex(key.getBytes(), expire, value);
  199. } catch (Exception e) {
  200. logger.error(e, e);
  201. jedisPool.returnBrokenResource(jedis);
  202. jedis = null;
  203. throw e;
  204. } finally {
  205. if (jedis != null) {
  206. jedisPool.returnResource(jedis);
  207. }
  208. }
  209. }

3. set 封装

  1. /** * 将指定的成员添加到key对应的set中。如果成员已是集合的成员,则不执行任何操作。 * 如果key不存在,则创建一个新的集,其中指定的成员为唯一成员。 * 如果key存在但不包含设置值,则返回错误。 * * @param key 键 * @param members 放入set集合的成员数组(值) */
  2. public static void sadd(final String key, final String... members) {
  3. Jedis jedis = jedisPool.getResource();
  4. try {
  5. jedis.sadd(key, members);
  6. } catch (Exception e) {
  7. logger.error(e, e);
  8. jedisPool.returnBrokenResource(jedis);
  9. jedis = null;
  10. throw e;
  11. } finally {
  12. if (jedis != null) {
  13. jedisPool.returnResource(jedis);
  14. }
  15. }
  16. }
  17. /** * 如果成员是存储在键上的集合的成员,则返回1,否则返回0。 * * @param key 键 * @param member 元素值 * @return true 键对应的list包含member,false不包含 */
  18. public static Boolean sismember(final String key, final String member) {
  19. Jedis jedis = jedisPool.getResource();
  20. try {
  21. return jedis.sismember(key, member);
  22. } catch (Exception e) {
  23. logger.error(e, e);
  24. jedisPool.returnBrokenResource(jedis);
  25. jedis = null;
  26. throw e;
  27. } finally {
  28. if (jedis != null) {
  29. jedisPool.returnResource(jedis);
  30. }
  31. }
  32. }
  33. /** * 从key 对应的set集合中随意删除一个元素,并返回给对象,如果redis中没有该key,返回null * * @param key redis的一个key * @return string value */
  34. public static String spop(final String key) {
  35. // 获取jedis
  36. Jedis jedis = jedisPool.getResource();
  37. try {
  38. return jedis.spop(key);
  39. } catch (Exception e) {
  40. logger.error(e, e);
  41. jedisPool.returnBrokenResource(jedis);
  42. jedis = null;
  43. throw e;
  44. } finally {
  45. if (jedis != null) {
  46. jedisPool.returnResource(jedis);
  47. }
  48. }
  49. }
  50. /** * 设置key-value;key之前不存在,设置key-value,返回true;key已经存在,不操作,返回false * * @param key 键 * @param value 值 * @return true :key之前不存在,设置key-value;false:key已经存在,不操作 */
  51. public static Boolean setnx(final String key, final String value) {
  52. Jedis jedis = jedisPool.getResource();
  53. try {
  54. // jedis.setnx(key,value):如果key已经存在,不设置value,返回0;key不存在,重新设置value,返回1;
  55. return jedis.setnx(key, value) == 1;
  56. } catch (Exception e) {
  57. logger.error(e, e);
  58. jedisPool.returnBrokenResource(jedis);
  59. jedis = null;
  60. throw e;
  61. } finally {
  62. if (jedis != null) {
  63. jedisPool.returnResource(jedis);
  64. }
  65. }
  66. }

4. hash 封装

  1. /** * string-key,string-value * * @param key hash-key * @param field hashKey 对应的字段名称 * @param seconds 设置过期时间 * @param clazz value数据类型 * @return 返回 数据对象 */
  2. public static <T> T hget(String key, String field, int seconds, Class<T> clazz) {
  3. Jedis jedis = jedisPool.getResource();
  4. try {
  5. String str = jedis.hget(key, field);
  6. if (!StringUtils.isEmpty(str)) {
  7. jedis.expire(key, seconds);
  8. return JSON.parseObject(JSON.toJSONString(str), clazz);
  9. }
  10. return null;
  11. } catch (Exception e) {
  12. logger.error(e, e);
  13. jedisPool.returnBrokenResource(jedis);
  14. jedis = null;
  15. throw e;
  16. } finally {
  17. if (jedis != null) {
  18. jedisPool.returnResource(jedis);
  19. }
  20. }
  21. }

发表评论

表情:
评论列表 (有 0 条评论,191人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Jedis

    redis 应用场景 缓存(数据查询、短连接、新闻内容、商品内容等等) 聊天室的在线好友列表 任务队列。(秒杀、抢购、12306等等) 应用排行榜

    相关 Jedis

    参考资料 Redis 单点模式和集群模式代码测试及问题记录:https://blog.csdn.net/boonya/article/details/49466003 Je