RedisManager.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package com.nuliji.tools.shiro.cache;
  2. import java.util.Set;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.data.redis.connection.RedisConnection;
  6. import org.springframework.data.redis.connection.RedisConnectionFactory;
  7. /**
  8. * Created by gaojie on 2017/11/7.
  9. */
  10. public class RedisManager {
  11. private RedisConnectionFactory factory;
  12. private static final Logger logger = LoggerFactory.getLogger(RedisManager.class);
  13. /**
  14. * 有效期
  15. */
  16. private int expire;
  17. public RedisManager() {
  18. }
  19. /**
  20. * get value from redis
  21. * @param key
  22. * @return
  23. */
  24. public byte[] get(byte[] key) {
  25. byte[] value = null;
  26. RedisConnection redis = factory.getConnection();
  27. try {
  28. // logger.debug("get redis:{}", new String(key));
  29. value = redis.get(key);
  30. } finally {
  31. redis.close();
  32. }
  33. return value;
  34. }
  35. /**
  36. * set
  37. * @param key
  38. * @param value
  39. * @return
  40. */
  41. public byte[] set(byte[] key, byte[] value) {
  42. RedisConnection redis = factory.getConnection();
  43. try {
  44. // logger.debug("set redis:{}", new String(key));
  45. redis.set(key, value);
  46. if (this.expire != 0) {
  47. redis.expire(key, this.expire);
  48. }
  49. } finally {
  50. redis.close();
  51. }
  52. return value;
  53. }
  54. /**
  55. * set
  56. * @param key
  57. * @param value
  58. * @param expire
  59. * @return
  60. */
  61. public byte[] set(byte[] key, byte[] value, int expire) {
  62. RedisConnection redis = factory.getConnection();
  63. try {
  64. // logger.debug("set redis expire:{}", new String(key));
  65. redis.set(key, value);
  66. if (expire != 0) {
  67. // logger.debug("set redis expire:{}", expire);
  68. redis.expire(key, expire);
  69. }
  70. } finally {
  71. redis.close();
  72. }
  73. return value;
  74. }
  75. /**
  76. * del
  77. * @param key
  78. */
  79. public void del(byte[] key) {
  80. RedisConnection redis = factory.getConnection();
  81. try {
  82. // logger.debug("delete redis:{}", new String(key));
  83. redis.del(key);
  84. } finally {
  85. redis.close();
  86. }
  87. }
  88. /**
  89. * flush
  90. */
  91. public void flushDb() {
  92. RedisConnection redis = factory.getConnection();
  93. try {
  94. if(!redis.isClosed()){
  95. logger.debug("flushDb redis");
  96. redis.flushDb();
  97. }
  98. } finally {
  99. redis.close();
  100. }
  101. }
  102. /**
  103. * size
  104. */
  105. public Long dbSize() {
  106. Long dbSize = 0L;
  107. RedisConnection redis = factory.getConnection();
  108. try {
  109. dbSize = redis.dbSize();
  110. } finally {
  111. redis.close();
  112. }
  113. return dbSize;
  114. }
  115. /**
  116. * keys
  117. * @param pattern
  118. * @return
  119. */
  120. public Set<byte[]> keys(String pattern) {
  121. Set<byte[]> keys = null;
  122. RedisConnection redis = factory.getConnection();
  123. try {
  124. // logger.debug("keys redis:{}", pattern);
  125. keys = redis.keys(pattern.getBytes());
  126. } finally {
  127. redis.close();
  128. }
  129. return keys;
  130. }
  131. public void setFactory(RedisConnectionFactory factory) {
  132. this.factory = factory;
  133. }
  134. public void setExpire(int expire) {
  135. this.expire = expire;
  136. }
  137. }