cache.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import {
  2. EXPIRE
  3. } from '../config/app';
  4. class Cache {
  5. constructor(handler) {
  6. this.cacheSetHandler = uni.setStorageSync;
  7. this.cacheGetHandler = uni.getStorageSync;
  8. this.cacheClearHandler = uni.removeStorageSync;
  9. this.cacheExpire = 'UNI-APP-CRMEB:TAG';
  10. this.clearOverdue();
  11. }
  12. /**
  13. * 获取当前时间戳
  14. */
  15. time() {
  16. return Math.round(new Date() / 1000);
  17. }
  18. /**
  19. * 字符串转时间戳
  20. * @param {Object} expiresTime
  21. */
  22. strTotime(expiresTime){
  23. let expires_time = expiresTime.substring(0, 19);
  24. expires_time = expires_time.replace(/-/g, '/');
  25. return Math.round(new Date(expires_time).getTime() / 1000);
  26. }
  27. /**
  28. * 设置过期时间缓存
  29. * @param {Object} key
  30. * @param {Object} expire
  31. */
  32. setExpireCaheTag(key, expire) {
  33. expire = expire !== undefined ? expire : EXPIRE;
  34. if (typeof expire === 'number') {
  35. let tag = this.cacheGetHandler(this.cacheExpire), newTag = [],newKeys = [];
  36. if (typeof tag === 'object' && tag.length) {
  37. newTag = tag.map(item => {
  38. newKeys.push(item.key);
  39. if (item.key === key) {
  40. item.expire = expire === 0 ? 0 : this.time() + expire;
  41. }
  42. return item;
  43. });
  44. }
  45. if (!newKeys.length || newKeys.indexOf(key) === -1) {
  46. newTag.push({
  47. key: key,
  48. expire: expire === 0 ? 0 : this.time() + expire
  49. });
  50. }
  51. this.cacheSetHandler(this.cacheExpire, newTag);
  52. }
  53. }
  54. /**
  55. * 缓存是否过期,过期自动删除
  56. * @param {Object} key
  57. * @param {Object} $bool true = 删除,false = 不删除
  58. */
  59. getExpireCahe(key, $bool) {
  60. try {
  61. let tag = this.cacheGetHandler(this.cacheExpire),time = 0,index = false;
  62. if (typeof tag === 'object' && tag.length) {
  63. tag.map((item,i) => {
  64. if(item.key === key){
  65. time = item.expire
  66. index = i
  67. }
  68. });
  69. if (time) {
  70. let newTime = parseInt(time);
  71. if (time && time < this.time() && !Number.isNaN(newTime)) {
  72. if ($bool === undefined || $bool === true) {
  73. this.cacheClearHandler(key);
  74. if(index !== false){
  75. tag.splice(index,1)
  76. this.cacheClearHandler(this.cacheExpire,tag);
  77. }
  78. }
  79. return false;
  80. } else
  81. return true;
  82. } else {
  83. return !!this.cacheGetHandler(key);
  84. }
  85. }
  86. return false;
  87. } catch (e) {
  88. return false;
  89. }
  90. }
  91. /**
  92. * 设置缓存
  93. * @param {Object} key
  94. * @param {Object} data
  95. */
  96. set(key, data, expire) {
  97. if (data === undefined) {
  98. return true;
  99. }
  100. if (typeof data === 'object')
  101. data = JSON.stringify(data);
  102. try {
  103. this.setExpireCaheTag(key, expire);
  104. return this.cacheSetHandler(key, data);
  105. } catch (e) {
  106. console.log(e);
  107. return false;
  108. }
  109. }
  110. /**
  111. * 检测缓存是否存在
  112. * @param {Object} key
  113. */
  114. has(key) {
  115. this.clearOverdue();
  116. return this.getExpireCahe(key);
  117. }
  118. /**
  119. * 获取缓存
  120. * @param {Object} key
  121. * @param {Object} $default
  122. * @param {Object} expire
  123. */
  124. get(key, $default, expire) {
  125. this.clearOverdue();
  126. try {
  127. let isBe = this.getExpireCahe(key);
  128. let data = this.cacheGetHandler(key);
  129. if (data && isBe) {
  130. if (typeof $default === 'boolean')
  131. return JSON.parse(data);
  132. else
  133. return data;
  134. } else {
  135. if (typeof $default === 'function') {
  136. let value = $default();
  137. this.set(key, value, expire);
  138. return value;
  139. } else {
  140. this.set(key, $default, expire);
  141. return $default;
  142. }
  143. }
  144. } catch (e) {
  145. return null;
  146. }
  147. }
  148. /**
  149. * 删除缓存
  150. * @param {Object} key
  151. */
  152. clear(key) {
  153. try {
  154. let cahceValue = this.cacheGetHandler(this.cacheExpire),
  155. index = false;
  156. if (cahceValue && typeof cahceValue === 'object' && cahceValue.length) {
  157. cahceValue.map((item, i) => {
  158. if (item.key === key) {
  159. index = i;
  160. }
  161. });
  162. if (index !== false) {
  163. cahceValue.splice(index, 1);
  164. }
  165. this.cacheSetHandler(this.cacheExpire, cahceValue);
  166. }
  167. return this.cacheClearHandler(key);
  168. } catch (e) {
  169. return false;
  170. }
  171. }
  172. /**
  173. * 清除过期缓存
  174. */
  175. clearOverdue() {
  176. let cahceValue = this.cacheGetHandler(this.cacheExpire),
  177. time = this.time(),
  178. newBeOverdueValue = [],
  179. newTagValue = [];
  180. if (cahceValue && typeof cahceValue === 'object' && cahceValue.length) {
  181. cahceValue.map(item => {
  182. if (item) {
  183. if ((item.expire !== undefined && item.expire > time) || item.expire === 0) {
  184. newTagValue.push(item);
  185. } else {
  186. newBeOverdueValue.push(item.key);
  187. }
  188. }
  189. });
  190. }
  191. //保存没有过期的缓存标签
  192. if (newTagValue.length !== cahceValue.length) {
  193. this.cacheSetHandler(this.cacheExpire, newTagValue);
  194. }
  195. //删除过期缓存
  196. newBeOverdueValue.forEach(k => {
  197. this.cacheClearHandler(k);
  198. })
  199. }
  200. }
  201. export default new Cache;