cache.js 2.8 KB

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