Cache.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * 本地缓存类,可配置缓存时间,长久保存
  3. */
  4. class localStorageEngine {
  5. constructor() {
  6. try {
  7. if (window.localStorage) {
  8. this.ok = true;
  9. this.key = '';
  10. this.expTime = 0;
  11. }
  12. } catch (e) {
  13. console.log('localStorage not exist');
  14. }
  15. }
  16. getItem() {
  17. if (this.ok) {
  18. let value = window.localStorage.getItem(this.key);
  19. if (value && value !== 'undefined') {
  20. value = JSON.parse(value);
  21. this.expTime = value.expTime;
  22. } else {
  23. return null;
  24. }
  25. return value.data;
  26. }
  27. return null;
  28. }
  29. setItem(time, value) {
  30. if (this.ok) {
  31. value = JSON.stringify({ data: value, expTime: time });
  32. window.localStorage.setItem(this.key, value);
  33. return true;
  34. }
  35. return false;
  36. }
  37. removeItem() {
  38. if (this.ok) {
  39. window.localStorage.removeItem(this.key);
  40. this.expTime = 0;
  41. return true;
  42. }
  43. return false;
  44. }
  45. }
  46. class Cache extends localStorageEngine {
  47. constructor(key) {
  48. super();
  49. this.key = key;
  50. this.value = null;
  51. }
  52. setCache(value, time, local) {
  53. if (time) {
  54. this.expTime = Math.ceil(Date.now() / 1000) + time;
  55. }
  56. this.value = value;
  57. if (local) {
  58. this.setItem(this.expTime, value);
  59. }
  60. }
  61. getCache(local) {
  62. if (this.hasCache(local)) {
  63. return this.value;
  64. }
  65. return null;
  66. }
  67. clearCache(local) {
  68. this.value = null;
  69. if (local) this.removeItem();
  70. }
  71. hasCache(local) {
  72. if (!this.value && local) this.value = this.getItem();
  73. if (this.expTime !== 0 && Math.ceil(Date.now() / 1000) > this.expTime) {
  74. this.clearCache(local);
  75. return false;
  76. }
  77. return !!this.value;
  78. }
  79. }
  80. export default class CacheManage {
  81. constructor() {
  82. this.cacheMap = {};
  83. this.prefix = __BASE_NAME__.substr(1, __BASE_NAME__.length - 1) || 'base';
  84. }
  85. formatKey(key) {
  86. return `gj:${key}`.toUpperCase();
  87. }
  88. getCache(key, local = true) {
  89. /**
  90. * 获取缓存
  91. * key 密钥
  92. * local 是否本地缓存
  93. */
  94. key = this.formatKey(key);
  95. if (!this.cacheMap[key]) {
  96. this.cacheMap[key] = CacheManage.makeCache(key);
  97. }
  98. return this.cacheMap[key].getCache(local);
  99. }
  100. setCache(key, value, time, local = true) {
  101. /**
  102. * 设置缓存
  103. * key 密钥
  104. * value 值
  105. * time 缓存时间
  106. * local 是否本地缓存
  107. */
  108. key = this.formatKey(key);
  109. if (!this.cacheMap[key]) {
  110. this.cacheMap[key] = CacheManage.makeCache(key);
  111. }
  112. this.cacheMap[key].setCache(value, time, local);
  113. }
  114. removeCache(key, local = true) {
  115. key = this.formatKey(key);
  116. if (this.cacheMap[key]) {
  117. this.cacheMap[key].clearCache(local);
  118. }
  119. }
  120. static getApiCacheKey() {
  121. return '';
  122. }
  123. static makeCache(key) {
  124. return new Cache(key);
  125. }
  126. }