CacheUtils.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package com.ruoyi.common.utils;
  2. import java.util.Iterator;
  3. import java.util.Set;
  4. import org.apache.shiro.cache.Cache;
  5. import org.apache.shiro.cache.CacheManager;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import com.ruoyi.common.utils.spring.SpringUtils;
  9. /**
  10. * Cache工具类
  11. *
  12. * @author ruoyi
  13. */
  14. public class CacheUtils
  15. {
  16. private static Logger logger = LoggerFactory.getLogger(CacheUtils.class);
  17. private static CacheManager cacheManager = SpringUtils.getBean(CacheManager.class);
  18. private static final String SYS_CACHE = "sys-cache";
  19. /**
  20. * 获取SYS_CACHE缓存
  21. *
  22. * @param key
  23. * @return
  24. */
  25. public static Object get(String key)
  26. {
  27. return get(SYS_CACHE, key);
  28. }
  29. /**
  30. * 获取SYS_CACHE缓存
  31. *
  32. * @param key
  33. * @param defaultValue
  34. * @return
  35. */
  36. public static Object get(String key, Object defaultValue)
  37. {
  38. Object value = get(key);
  39. return value != null ? value : defaultValue;
  40. }
  41. /**
  42. * 写入SYS_CACHE缓存
  43. *
  44. * @param key
  45. * @return
  46. */
  47. public static void put(String key, Object value)
  48. {
  49. put(SYS_CACHE, key, value);
  50. }
  51. /**
  52. * 从SYS_CACHE缓存中移除
  53. *
  54. * @param key
  55. * @return
  56. */
  57. public static void remove(String key)
  58. {
  59. remove(SYS_CACHE, key);
  60. }
  61. /**
  62. * 获取缓存
  63. *
  64. * @param cacheName
  65. * @param key
  66. * @return
  67. */
  68. public static Object get(String cacheName, String key)
  69. {
  70. return getCache(cacheName).get(getKey(key));
  71. }
  72. /**
  73. * 获取缓存
  74. *
  75. * @param cacheName
  76. * @param key
  77. * @param defaultValue
  78. * @return
  79. */
  80. public static Object get(String cacheName, String key, Object defaultValue)
  81. {
  82. Object value = get(cacheName, getKey(key));
  83. return value != null ? value : defaultValue;
  84. }
  85. /**
  86. * 写入缓存
  87. *
  88. * @param cacheName
  89. * @param key
  90. * @param value
  91. */
  92. public static void put(String cacheName, String key, Object value)
  93. {
  94. getCache(cacheName).put(getKey(key), value);
  95. }
  96. /**
  97. * 从缓存中移除
  98. *
  99. * @param cacheName
  100. * @param key
  101. */
  102. public static void remove(String cacheName, String key)
  103. {
  104. getCache(cacheName).remove(getKey(key));
  105. }
  106. /**
  107. * 从缓存中移除所有
  108. *
  109. * @param cacheName
  110. */
  111. public static void removeAll(String cacheName)
  112. {
  113. Cache<String, Object> cache = getCache(cacheName);
  114. Set<String> keys = cache.keys();
  115. for (Iterator<String> it = keys.iterator(); it.hasNext();)
  116. {
  117. cache.remove(it.next());
  118. }
  119. logger.info("清理缓存: {} => {}", cacheName, keys);
  120. }
  121. /**
  122. * 从缓存中移除指定key
  123. *
  124. * @param keys
  125. */
  126. public static void removeByKeys(Set<String> keys)
  127. {
  128. removeByKeys(SYS_CACHE, keys);
  129. }
  130. /**
  131. * 从缓存中移除指定key
  132. *
  133. * @param cacheName
  134. * @param keys
  135. */
  136. public static void removeByKeys(String cacheName, Set<String> keys)
  137. {
  138. for (Iterator<String> it = keys.iterator(); it.hasNext();)
  139. {
  140. remove(it.next());
  141. }
  142. logger.info("清理缓存: {} => {}", cacheName, keys);
  143. }
  144. /**
  145. * 获取缓存键名
  146. *
  147. * @param key
  148. * @return
  149. */
  150. private static String getKey(String key)
  151. {
  152. return key;
  153. }
  154. /**
  155. * 获得一个Cache,没有则显示日志。
  156. *
  157. * @param cacheName
  158. * @return
  159. */
  160. private static Cache<String, Object> getCache(String cacheName)
  161. {
  162. Cache<String, Object> cache = cacheManager.getCache(cacheName);
  163. if (cache == null)
  164. {
  165. throw new RuntimeException("当前系统中没有定义“" + cacheName + "”这个缓存。");
  166. }
  167. return cache;
  168. }
  169. }