RedisService.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package com.miyzh.service;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.data.redis.core.RedisTemplate;
  4. import org.springframework.stereotype.Component;
  5. import java.util.Collection;
  6. import java.util.Date;
  7. import java.util.Set;
  8. import java.util.concurrent.TimeUnit;
  9. import java.util.stream.Collectors;
  10. import java.util.stream.Stream;
  11. @Component
  12. public class RedisService {
  13. @Autowired
  14. private RedisTemplate<String, String> redisTemplate;
  15. /**
  16. * 默认过期时长,单位:秒
  17. */
  18. public static final long DEFAULT_EXPIRE = 60 * 60 * 24;
  19. /**
  20. * 不设置过期时长
  21. */
  22. public static final long NOT_EXPIRE = -1;
  23. public boolean existsKey(String key) {
  24. return redisTemplate.hasKey(key);
  25. }
  26. /**
  27. * 重名名key,如果newKey已经存在,则newKey的原值被覆盖
  28. *
  29. * @param oldKey
  30. * @param newKey
  31. */
  32. public void renameKey(String oldKey, String newKey) {
  33. redisTemplate.rename(oldKey, newKey);
  34. }
  35. /**
  36. * newKey不存在时才重命名
  37. *
  38. * @param oldKey
  39. * @param newKey
  40. * @return 修改成功返回true
  41. */
  42. public boolean renameKeyNotExist(String oldKey, String newKey) {
  43. return redisTemplate.renameIfAbsent(oldKey, newKey);
  44. }
  45. /**
  46. * 删除key
  47. *
  48. * @param key
  49. */
  50. public void deleteKey(String key) {
  51. redisTemplate.delete(key);
  52. }
  53. /**
  54. * 删除多个key
  55. *
  56. * @param keys
  57. */
  58. public void deleteKey(String... keys) {
  59. Set<String> kSet = Stream.of(keys).map(k -> k).collect(Collectors.toSet());
  60. redisTemplate.delete(kSet);
  61. }
  62. /**
  63. * 删除Key的集合
  64. *
  65. * @param keys
  66. */
  67. public void deleteKey(Collection<String> keys) {
  68. Set<String> kSet = keys.stream().map(k -> k).collect(Collectors.toSet());
  69. redisTemplate.delete(kSet);
  70. }
  71. /**
  72. * 设置key的生命周期
  73. *
  74. * @param key
  75. * @param time
  76. * @param timeUnit
  77. */
  78. public void expireKey(String key, long time, TimeUnit timeUnit) {
  79. redisTemplate.expire(key, time, timeUnit);
  80. }
  81. /**
  82. * 指定key在指定的日期过期
  83. *
  84. * @param key
  85. * @param date
  86. */
  87. public void expireKeyAt(String key, Date date) {
  88. redisTemplate.expireAt(key, date);
  89. }
  90. /**
  91. * 查询key的生命周期
  92. *
  93. * @param key
  94. * @param timeUnit
  95. * @return
  96. */
  97. public long getKeyExpire(String key, TimeUnit timeUnit) {
  98. return redisTemplate.getExpire(key, timeUnit);
  99. }
  100. /**
  101. * 将key设置为永久有效
  102. *
  103. * @param key
  104. */
  105. public void persistKey(String key) {
  106. redisTemplate.persist(key);
  107. }
  108. }