3012e83e5bfc44200de9135e7bdf09fc8b109c26.svn-base 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.xxl.job.admin.core.util;
  2. import java.util.concurrent.ConcurrentHashMap;
  3. import java.util.concurrent.ConcurrentMap;
  4. /**
  5. * local cache tool
  6. *
  7. * @author xuxueli 2018-01-22 21:37:34
  8. */
  9. public class LocalCacheUtil {
  10. private static ConcurrentMap<String, LocalCacheData> cacheRepository = new ConcurrentHashMap<String, LocalCacheData>(); // 类型建议用抽象父类,兼容性更好;
  11. private static class LocalCacheData{
  12. private String key;
  13. private Object val;
  14. private long timeoutTime;
  15. public LocalCacheData() {
  16. }
  17. public LocalCacheData(String key, Object val, long timeoutTime) {
  18. this.key = key;
  19. this.val = val;
  20. this.timeoutTime = timeoutTime;
  21. }
  22. public String getKey() {
  23. return key;
  24. }
  25. public void setKey(String key) {
  26. this.key = key;
  27. }
  28. public Object getVal() {
  29. return val;
  30. }
  31. public void setVal(Object val) {
  32. this.val = val;
  33. }
  34. public long getTimeoutTime() {
  35. return timeoutTime;
  36. }
  37. public void setTimeoutTime(long timeoutTime) {
  38. this.timeoutTime = timeoutTime;
  39. }
  40. }
  41. /**
  42. * set cache
  43. *
  44. * @param key
  45. * @param val
  46. * @param cacheTime
  47. * @return
  48. */
  49. public static boolean set(String key, Object val, long cacheTime){
  50. // clean timeout cache, before set new cache (avoid cache too much)
  51. cleanTimeoutCache();
  52. // set new cache
  53. if (key==null || key.trim().length()==0) {
  54. return false;
  55. }
  56. if (val == null) {
  57. remove(key);
  58. }
  59. if (cacheTime <= 0) {
  60. remove(key);
  61. }
  62. long timeoutTime = System.currentTimeMillis() + cacheTime;
  63. LocalCacheData localCacheData = new LocalCacheData(key, val, timeoutTime);
  64. cacheRepository.put(localCacheData.getKey(), localCacheData);
  65. return true;
  66. }
  67. /**
  68. * remove cache
  69. *
  70. * @param key
  71. * @return
  72. */
  73. public static boolean remove(String key){
  74. if (key==null || key.trim().length()==0) {
  75. return false;
  76. }
  77. cacheRepository.remove(key);
  78. return true;
  79. }
  80. /**
  81. * get cache
  82. *
  83. * @param key
  84. * @return
  85. */
  86. public static Object get(String key){
  87. if (key==null || key.trim().length()==0) {
  88. return null;
  89. }
  90. LocalCacheData localCacheData = cacheRepository.get(key);
  91. if (localCacheData!=null && System.currentTimeMillis()<localCacheData.getTimeoutTime()) {
  92. return localCacheData.getVal();
  93. } else {
  94. remove(key);
  95. return null;
  96. }
  97. }
  98. /**
  99. * clean timeout cache
  100. *
  101. * @return
  102. */
  103. public static boolean cleanTimeoutCache(){
  104. if (!cacheRepository.keySet().isEmpty()) {
  105. for (String key: cacheRepository.keySet()) {
  106. LocalCacheData localCacheData = cacheRepository.get(key);
  107. if (localCacheData!=null && System.currentTimeMillis()>=localCacheData.getTimeoutTime()) {
  108. cacheRepository.remove(key);
  109. }
  110. }
  111. }
  112. return true;
  113. }
  114. }