cache.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. class Cache {
  2. constructor(handler) {
  3. this.cacheSetHandler = uni.setStorageSync;
  4. this.cacheGetHandler = uni.getStorageSync;
  5. this.cacheClearHandler = uni.removeStorageSync;
  6. this.cacheExpire = '_expire_2019_12_17_18_44';
  7. }
  8. /**
  9. * 获取当前时间戳
  10. */
  11. time() {
  12. return Math.round(new Date() / 1000);
  13. }
  14. /**
  15. * 设置过期时间缓存
  16. * @param {Object} key
  17. * @param {Object} expire
  18. */
  19. setExpireCahe(key, expire) {
  20. expire = expire !== undefined ? expire : 0;
  21. if (expire) {
  22. this.cacheSetHandler(key + this.cacheExpire, this.time() + expire)
  23. }
  24. }
  25. /**
  26. * 缓存是否过期,过期自动删除
  27. * @param {Object} key
  28. * @param {Object} $bool true = 删除,false = 不删除
  29. */
  30. getExpireCahe(key, $bool) {
  31. try {
  32. let time = this.cacheGetHandler(key + this.cacheExpire);
  33. if (time) {
  34. let newTime = parseInt(time);
  35. if (time && time < this.time() && !Number.isNaN(newTime)) {
  36. if ($bool === undefined || $bool === true) {
  37. this.cacheClearHandler(key);
  38. this.cacheClearHandler(key + this.cacheExpire);
  39. }
  40. return false;
  41. } else
  42. return true;
  43. } else {
  44. return !!this.cacheGetHandler(key);
  45. }
  46. } catch (e) {
  47. return false;
  48. }
  49. }
  50. /**
  51. * 设置缓存
  52. * @param {Object} key
  53. * @param {Object} data
  54. */
  55. set(key, data, expire) {
  56. if (typeof data === 'object')
  57. data = JSON.stringify(data);
  58. try {
  59. this.setExpireCahe(key, expire);
  60. return this.cacheSetHandler(key, data);
  61. } catch (e) {
  62. return false;
  63. }
  64. }
  65. /**
  66. * 检测缓存是否存在
  67. * @param {Object} key
  68. */
  69. has(key) {
  70. return this.getExpireCahe(key);
  71. }
  72. /**
  73. * 获取缓存
  74. * @param {Object} key
  75. * @param {Object} $default
  76. * @param {Object} expire
  77. */
  78. get(key, $default, expire) {
  79. try {
  80. let isBe = this.getExpireCahe(key);
  81. let data = this.cacheGetHandler(key);
  82. if (data && isBe) {
  83. if (typeof $default === 'boolean')
  84. return JSON.parse(data);
  85. else
  86. return data;
  87. } else {
  88. if (typeof $default === 'function') {
  89. let value = $default();
  90. this.set(key, value, expire);
  91. return value;
  92. } else {
  93. this.set(key, $default, expire);
  94. return $default;
  95. }
  96. }
  97. } catch (e) {
  98. return null;
  99. }
  100. }
  101. /**
  102. * 删除缓存
  103. * @param {Object} key
  104. */
  105. clear(key) {
  106. try {
  107. let cahceValue = this.cacheGetHandler(key + this.cacheExpire);
  108. if (cahceValue)
  109. this.cacheClearHandler(key + this.cacheExpire);
  110. return this.cacheClearHandler(key);
  111. } catch (e) {
  112. return false;
  113. }
  114. }
  115. /**
  116. * 清除过期缓存
  117. */
  118. clearOverdue() {
  119. // let cacheList = uni.getStorageInfoSync(),that = this;
  120. // if (typeof cacheList.keys === 'object'){
  121. // cacheList.keys.forEach(item=>{
  122. // that.getExpireCahe(item);
  123. // })
  124. // }
  125. }
  126. }
  127. export default new Cache;