SimpleRedisClient.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package cn.com.qmth.examcloud.web.redis;
  2. import java.util.concurrent.TimeUnit;
  3. import org.springframework.data.redis.core.RedisTemplate;
  4. import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
  5. import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
  6. import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
  7. /**
  8. * redis client
  9. *
  10. * @author WANGWEI
  11. * @date 2019年2月22日
  12. * @Copyright (c) 2018-2020 WANGWEI [QQ:522080330] All Rights Reserved.
  13. */
  14. public final class SimpleRedisClient implements RedisClient {
  15. private static final ExamCloudLog REDIS_LOG = ExamCloudLogFactory.getLog("REDIS_LOGGER");
  16. private RedisTemplate<String, Object> redisTemplate;
  17. private boolean enable = true;
  18. public SimpleRedisClient(RedisTemplate<String, Object> redisTemplate) {
  19. super();
  20. this.redisTemplate = redisTemplate;
  21. }
  22. private void beforeMethod() {
  23. if (!enable) {
  24. throw new ExamCloudRuntimeException("RedisClient is not enabled");
  25. }
  26. }
  27. private void afterMethod(String method, long startTimeMillis) {
  28. if (REDIS_LOG.isDebugEnabled()) {
  29. String s = String.format("[SimpleRedisClient.%s] cost %d ms.", method,
  30. System.currentTimeMillis() - startTimeMillis);
  31. REDIS_LOG.debug(s);
  32. }
  33. }
  34. @Override
  35. public void set(String key, Object value) {
  36. long s = System.currentTimeMillis();
  37. beforeMethod();
  38. redisTemplate.opsForValue().set(key, value);
  39. afterMethod("set(String key, Object value)", s);
  40. }
  41. @Override
  42. public void set(String key, Object value, int timeout) {
  43. long s = System.currentTimeMillis();
  44. beforeMethod();
  45. set(key, value);
  46. expire(key, timeout);
  47. afterMethod("set(String key, Object value, int timeout)", s);
  48. }
  49. @Override
  50. public void expire(String key, int timeout) {
  51. long s = System.currentTimeMillis();
  52. beforeMethod();
  53. redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
  54. afterMethod("expire(String key, int timeout)", s);
  55. }
  56. /**
  57. * 方法注释
  58. *
  59. * @author WANGWEI
  60. * @param key
  61. * @param timeout
  62. * @param unit
  63. */
  64. public void expire(String key, final long timeout, final TimeUnit unit) {
  65. long s = System.currentTimeMillis();
  66. beforeMethod();
  67. redisTemplate.expire(key, timeout, unit);
  68. afterMethod("(String key, final long timeout, final TimeUnit unit)", s);
  69. }
  70. @Override
  71. public <T> T get(String key, Class<T> c, int timeout) {
  72. long s = System.currentTimeMillis();
  73. beforeMethod();
  74. Object object = redisTemplate.opsForValue().get(key);
  75. @SuppressWarnings("unchecked")
  76. T t = (T) object;
  77. expire(key, timeout);
  78. afterMethod("get(String key, Class<T> c, int timeout)", s);
  79. return t;
  80. }
  81. @Override
  82. public <T> T get(String key, Class<T> c) {
  83. long s = System.currentTimeMillis();
  84. beforeMethod();
  85. Object object = redisTemplate.opsForValue().get(key);
  86. @SuppressWarnings("unchecked")
  87. T t = (T) object;
  88. afterMethod("get(String key, Class<T> c)", s);
  89. return t;
  90. }
  91. @Override
  92. public void delete(String key) {
  93. long s = System.currentTimeMillis();
  94. beforeMethod();
  95. redisTemplate.opsForValue().set(key, null);
  96. expire(key, 0);
  97. afterMethod("delete(String key)", s);
  98. }
  99. @Override
  100. public void convertAndSend(String channel, Object message) {
  101. long s = System.currentTimeMillis();
  102. beforeMethod();
  103. redisTemplate.convertAndSend(channel, message);
  104. afterMethod("convertAndSend(String channel, Object message)", s);
  105. }
  106. @Override
  107. public Boolean setIfAbsent(String key, String value, int timeout) {
  108. long s = System.currentTimeMillis();
  109. beforeMethod();
  110. Boolean b = redisTemplate.opsForValue().setIfAbsent(key, value, timeout, TimeUnit.SECONDS);
  111. afterMethod("setIfAbsent(String key, String value, int timeout)", s);
  112. return b;
  113. }
  114. @Override
  115. public boolean isEnable() {
  116. return enable;
  117. }
  118. @Override
  119. public void setEnable(boolean enable) {
  120. this.enable = enable;
  121. }
  122. }