Redised.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package com.yiidata.intergration.web.modules.sys.cache;
  2. import com.primeton.damp.cache.serde.JdkSerializer;
  3. import com.primeton.damp.cache.serde.Serializer;
  4. import com.primeton.damp.cache.serde.StringSerializer;
  5. import org.apache.commons.lang.StringUtils;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import redis.clients.jedis.Jedis;
  9. import java.io.Closeable;
  10. import java.io.IOException;
  11. import java.io.Serializable;
  12. import java.util.Set;
  13. /**
  14. * <pre>
  15. *
  16. * Created by zhaopx.
  17. * User: zhaopx
  18. * Date: 2019/8/21
  19. * Time: 18:55
  20. *
  21. * </pre>
  22. *
  23. * @author zhaopx
  24. */
  25. public class Redised implements ICache<Serializable>, Closeable {
  26. private static Logger logger = LoggerFactory.getLogger(Redised.class);
  27. /**
  28. * Redis 的 Java Client
  29. */
  30. Jedis jedis;
  31. /**
  32. * redis db index
  33. */
  34. private final int db;
  35. /**
  36. * KEY 序列化
  37. */
  38. private final Serializer<String> KEY_SERDE = new StringSerializer();
  39. /**
  40. * Redis Cache 序列化方式
  41. */
  42. private final Serializer<Serializable> VALUE_SERDE = new JdkSerializer();
  43. public Redised() {
  44. this("localhost", 6379, 0);
  45. }
  46. public Redised(String host, int port){
  47. this(host, port, 0);
  48. }
  49. public Redised(String host, int port, int db){
  50. this(host, port, null, db);
  51. }
  52. public Redised(String host, int port, String password){
  53. this(host, port, password, 0);
  54. }
  55. public Redised(String host, int port, String password, int db){
  56. this.db = db;
  57. jedis = new Jedis(host, port);
  58. if(StringUtils.isNotBlank(password)) {
  59. jedis.auth(password);
  60. }
  61. jedis.select(db);
  62. }
  63. @Override
  64. public void add(String key, Serializable value) {
  65. if(value == null) {
  66. return;
  67. }
  68. jedis.set(KEY_SERDE.serialize(key), VALUE_SERDE.serialize(value));
  69. }
  70. @Override
  71. public void add(String key, int exp, Serializable value) {
  72. byte[] keybytes = KEY_SERDE.serialize(key);
  73. jedis.set(keybytes, VALUE_SERDE.serialize(value));
  74. jedis.expire(keybytes, exp);
  75. }
  76. @Override
  77. public Serializable get(String key) {
  78. if(key == null) {
  79. return null;
  80. }
  81. byte[] bytes = jedis.get(KEY_SERDE.serialize(key));
  82. if(bytes == null) {
  83. return null;
  84. }
  85. return VALUE_SERDE.deserialize(bytes);
  86. }
  87. /**
  88. * 移除某条缓存
  89. * @param key 缓存的 KEY
  90. * @return
  91. */
  92. @Override
  93. public Serializable remove(String key) {
  94. if(key == null) {
  95. return null;
  96. }
  97. logger.info("remove cache key: {}", key);
  98. return jedis.del(KEY_SERDE.serialize(key));
  99. }
  100. @Override
  101. public int removeByPrefix(String prefix) {
  102. Set<String> keys = jedis.keys(prefix+"*");
  103. if(keys != null && keys.size() > 0) {
  104. for (String key : keys) {
  105. remove(key);
  106. }
  107. }
  108. return keys == null ? 0 : keys.size();
  109. }
  110. @Override
  111. public void clear() {
  112. jedis.flushDB();
  113. }
  114. @Override
  115. public int size() {
  116. return jedis.dbSize().intValue();
  117. }
  118. @Override
  119. public void close() throws IOException {
  120. jedis.close();
  121. }
  122. @Override
  123. public void shutdown() {
  124. try {
  125. close();
  126. } catch (IOException e) {
  127. }
  128. }
  129. }