RedisClient.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package com.yihu.ehr.redis.client;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.dao.DataAccessException;
  4. import org.springframework.data.redis.connection.RedisConnection;
  5. import org.springframework.data.redis.connection.StringRedisConnection;
  6. import org.springframework.data.redis.core.RedisCallback;
  7. import org.springframework.data.redis.core.RedisTemplate;
  8. import org.springframework.stereotype.Service;
  9. import org.springframework.util.SerializationUtils;
  10. import java.io.Serializable;
  11. import java.util.*;
  12. /**
  13. * Redis 数据访问接口。
  14. *
  15. * @author Sand
  16. * @version 1.0
  17. * @created 2015.08.04 11:12
  18. */
  19. @Service
  20. public class RedisClient {
  21. @Autowired
  22. private RedisTemplate<String, Serializable> redisTemplate;
  23. /**
  24. * 添加数据。
  25. *
  26. * @param key
  27. * @param value
  28. */
  29. public void set(final String key, final Serializable value) {
  30. redisTemplate.execute((RedisCallback<Object>) connection -> {
  31. byte[] key_ = key.getBytes();
  32. byte[] value_ = SerializationUtils.serialize(value);
  33. connection.set(key_, value_);
  34. return true;
  35. });
  36. }
  37. /**
  38. * 添加数据,含失效时间。
  39. *
  40. * @param key
  41. * @param value
  42. */
  43. public void set(final String key, final Serializable value,long seconds) {
  44. redisTemplate.execute((RedisCallback<Object>) connection -> {
  45. byte[] key_ = key.getBytes();
  46. byte[] value_ = SerializationUtils.serialize(value);
  47. connection.setEx(key_,seconds, value_);
  48. return true;
  49. });
  50. }
  51. /**
  52. * 批量设置key-value值。
  53. *
  54. * @param data
  55. */
  56. public void multiSet(Map<Serializable, Serializable> data){
  57. redisTemplate.executePipelined(new RedisCallback<Object>() {
  58. @Override
  59. public Object doInRedis(RedisConnection connection) throws DataAccessException {
  60. StringRedisConnection stringRedisConn = (StringRedisConnection)connection;
  61. for(Serializable key : data.keySet()){
  62. Serializable value = data.get(key);
  63. connection.rPushX(SerializationUtils.serialize(key), SerializationUtils.serialize(value));
  64. }
  65. return null;
  66. }
  67. });
  68. }
  69. /**
  70. * 批量设置key-value值。
  71. *
  72. * @param data
  73. */
  74. public void multiSetData(Map<String, Serializable> data){
  75. redisTemplate.executePipelined(new RedisCallback<Object>() {
  76. @Override
  77. public Object doInRedis(RedisConnection connection) throws DataAccessException {
  78. for (String key : data.keySet()) {
  79. byte[] key_ = key.getBytes();
  80. byte[] value_ = SerializationUtils.serialize(data.get(key));
  81. connection.setNX(key_, value_);
  82. }
  83. return null;
  84. }
  85. });
  86. }
  87. /**
  88. * 获取数据
  89. *
  90. * @param key
  91. * @param <T>
  92. * @return
  93. */
  94. public <T> T get(final String key) {
  95. return (T)redisTemplate.execute((RedisCallback<Serializable>) connection -> {
  96. byte[] keyBytes = key.getBytes();
  97. byte[] bytes = connection.get(keyBytes);
  98. if(bytes == null) return null;
  99. return (Serializable) SerializationUtils.deserialize(bytes);
  100. });
  101. }
  102. /**
  103. * 批量获取key关联的值。
  104. *
  105. * @param keys
  106. * @return
  107. */
  108. public List<Serializable> multiGet(Collection<String> keys){
  109. return redisTemplate.opsForValue().multiGet(keys);
  110. }
  111. /**
  112. * 删除记录,支持Key模糊匹配删除
  113. *
  114. * @param key
  115. */
  116. public void delete(String key) {
  117. redisTemplate.delete(redisTemplate.keys(key));
  118. }
  119. /**
  120. * 删除多条记录,如果Key集合过大,建议使用Key模糊匹配删除
  121. * @param keys
  122. */
  123. public void delete(Collection<String> keys) {
  124. redisTemplate.delete(keys);
  125. }
  126. /**
  127. * 匹配特定模式的Key列表
  128. * @param pattern
  129. * @return
  130. */
  131. public Set<String> keys(String pattern) {
  132. return redisTemplate.execute((RedisCallback<Set<String>>) connection -> {
  133. Set<byte[]> keys = connection.keys(pattern.getBytes());
  134. Set<String> returnKeys = new HashSet<>();
  135. for (byte[] key : keys) {
  136. returnKeys.add(new String(key));
  137. }
  138. return returnKeys;
  139. });
  140. }
  141. /**
  142. * 是否包含指定Key
  143. * @param key
  144. * @return
  145. */
  146. public boolean hasKey(String key) {
  147. return redisTemplate.execute((RedisCallback<Boolean>) connection -> connection.exists(SerializationUtils.serialize(key)));
  148. }
  149. }