ef0306c2a03bdbf15ee7e4b96d168ffcc1908779.svn-base 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package cn.com.goldenwater.dcproj.utils;
  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.StringRedisTemplate;
  8. import org.springframework.stereotype.Component;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.Set;
  12. import java.util.concurrent.TimeUnit;
  13. /**
  14. * @author 慕课网
  15. * @Title: Redis 工具类
  16. */
  17. @Component
  18. public class RedisOperator {
  19. // @Autowired
  20. // private RedisTemplate<String, Object> redisTemplate;
  21. @Autowired
  22. private StringRedisTemplate redisTemplate;
  23. // Key(键),简单的key-value操作
  24. /**
  25. * 实现命令:TTL key,以秒为单位,返回给定 key的剩余生存时间(TTL, time to live)。
  26. *
  27. * @param key
  28. * @return
  29. */
  30. public long ttl(String key) {
  31. return redisTemplate.getExpire(key);
  32. }
  33. /**
  34. * 实现命令:expire 设置过期时间,单位秒
  35. *
  36. * @param key
  37. * @return
  38. */
  39. public void expire(String key, long timeout) {
  40. redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
  41. }
  42. /**
  43. * 实现命令:INCR key,增加key一次
  44. *
  45. * @param key
  46. * @return
  47. */
  48. public long incr(String key, long delta) {
  49. return redisTemplate.opsForValue().increment(key, delta);
  50. }
  51. /**
  52. * 实现命令:KEYS pattern,查找所有符合给定模式 pattern的 key
  53. */
  54. public Set<String> keys(String pattern) {
  55. return redisTemplate.keys(pattern);
  56. }
  57. /**
  58. * 实现命令:DEL key,删除一个key
  59. *
  60. * @param key
  61. */
  62. public void del(String key) {
  63. redisTemplate.delete(key);
  64. }
  65. // String(字符串)
  66. /**
  67. * 实现命令:SET key value,设置一个key-value(将字符串值 value关联到 key)
  68. *
  69. * @param key
  70. * @param value
  71. */
  72. public void set(String key, String value) {
  73. redisTemplate.opsForValue().set(key, value);
  74. }
  75. /**
  76. * 实现命令:SET key value EX seconds,设置key-value和超时时间(秒)
  77. *
  78. * @param key
  79. * @param value
  80. * @param timeout (以秒为单位)
  81. */
  82. public void set(String key, String value, long timeout) {
  83. redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
  84. }
  85. /**
  86. * 实现命令:GET key,返回 key所关联的字符串值。
  87. *
  88. * @param key
  89. * @return value
  90. */
  91. public String get(String key) {
  92. return (String) redisTemplate.opsForValue().get(key);
  93. }
  94. /**
  95. * 批量查询,对应mget
  96. *
  97. * @param keys
  98. * @return
  99. */
  100. public List<String> mget(List<String> keys) {
  101. return redisTemplate.opsForValue().multiGet(keys);
  102. }
  103. /**
  104. * 批量查询,管道pipeline
  105. *
  106. * @param keys
  107. * @return
  108. */
  109. public List<Object> batchGet(List<String> keys) {
  110. // nginx -> keepalive
  111. // redis -> pipeline
  112. List<Object> result = redisTemplate.executePipelined(new RedisCallback<String>() {
  113. @Override
  114. public String doInRedis(RedisConnection connection) throws DataAccessException {
  115. StringRedisConnection src = (StringRedisConnection) connection;
  116. for (String k : keys) {
  117. src.get(k);
  118. }
  119. return null;
  120. }
  121. });
  122. return result;
  123. }
  124. // Hash(哈希表)
  125. /**
  126. * 实现命令:HSET key field value,将哈希表 key中的域 field的值设为 value
  127. *
  128. * @param key
  129. * @param field
  130. * @param value
  131. */
  132. public void hset(String key, String field, Object value) {
  133. redisTemplate.opsForHash().put(key, field, value);
  134. }
  135. /**
  136. * 实现命令:HGET key field,返回哈希表 key中给定域 field的值
  137. *
  138. * @param key
  139. * @param field
  140. * @return
  141. */
  142. public String hget(String key, String field) {
  143. return (String) redisTemplate.opsForHash().get(key, field);
  144. }
  145. /**
  146. * 实现命令:HDEL key field [field ...],删除哈希表 key 中的一个或多个指定域,不存在的域将被忽略。
  147. *
  148. * @param key
  149. * @param fields
  150. */
  151. public void hdel(String key, Object... fields) {
  152. redisTemplate.opsForHash().delete(key, fields);
  153. }
  154. /**
  155. * 实现命令:HGETALL key,返回哈希表 key中,所有的域和值。
  156. *
  157. * @param key
  158. * @return
  159. */
  160. public Map<Object, Object> hgetall(String key) {
  161. return redisTemplate.opsForHash().entries(key);
  162. }
  163. // List(列表)
  164. /**
  165. * 实现命令:LPUSH key value,将一个值 value插入到列表 key的表头
  166. *
  167. * @param key
  168. * @param value
  169. * @return 执行 LPUSH命令后,列表的长度。
  170. */
  171. public long lpush(String key, String value) {
  172. return redisTemplate.opsForList().leftPush(key, value);
  173. }
  174. /**
  175. * 实现命令:LPOP key,移除并返回列表 key的头元素。
  176. *
  177. * @param key
  178. * @return 列表key的头元素。
  179. */
  180. public String lpop(String key) {
  181. return (String) redisTemplate.opsForList().leftPop(key);
  182. }
  183. /**
  184. * 实现命令:RPUSH key value,将一个值 value插入到列表 key的表尾(最右边)。
  185. *
  186. * @param key
  187. * @param value
  188. * @return 执行 LPUSH命令后,列表的长度。
  189. */
  190. public long rpush(String key, String value) {
  191. return redisTemplate.opsForList().rightPush(key, value);
  192. }
  193. }