cache.js 3.4 KB

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