cache.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. this.name = "storage";
  9. }
  10. /**
  11. * 获取当前时间戳
  12. */
  13. time() {
  14. return Math.round(new Date() / 1000);
  15. }
  16. /**
  17. * 日期字符串转时间戳
  18. * @param {Object} expiresTime
  19. */
  20. strTotime(expiresTime) {
  21. let expires_time = expiresTime.substring(0, 19);
  22. expires_time = expires_time.replace(/-/g, "/");
  23. return Math.round(new Date(expires_time).getTime() / 1000);
  24. }
  25. setExpireCaheTag(key, expire) {
  26. expire = expire !== undefined ? expire : EXPIRE;
  27. if (typeof expire === "number") {
  28. let tag = this.cacheGetHandler(this.cacheExpire),
  29. newTag = [],
  30. newKeys = [];
  31. if (typeof tag === "object" && tag.length) {
  32. newTag = tag.map((item) => {
  33. newKeys.push(item.key);
  34. if (item.key === key) {
  35. item.expire = expire === 0 ? 0 : this.time() + expire;
  36. }
  37. return item;
  38. });
  39. }
  40. if (!newKeys.length || newKeys.indexOf(key) === -1) {
  41. newTag.push({
  42. key: key,
  43. expire: expire === 0 ? 0 : this.time() + expire,
  44. });
  45. }
  46. this.cacheSetHandler(this.cacheExpire, newTag);
  47. }
  48. }
  49. /**
  50. * 设置过期时间缓存
  51. * @param {Object} name key
  52. * @param {Object} value value
  53. * @param {Object} expire 过期时间
  54. * @param {Object} startTime 记录何时将值存入缓存,毫秒级
  55. */
  56. setItem(params) {
  57. let obj = {
  58. name: "",
  59. value: "",
  60. expires: "",
  61. startTime: new Date().getTime(),
  62. };
  63. let options = {};
  64. //将obj和传进来的params合并
  65. Object.assign(options, obj, params);
  66. if (options.expires) {
  67. //如果options.expires设置了的话
  68. //以options.name为key,options为值放进去
  69. // localStorage.setItem(options.name,JSON.stringify(options));
  70. uni.setStorageSync(options.name, JSON.stringify(options));
  71. } else {
  72. //如果options.expires没有设置,就判断一下value的类型
  73. let type = Object.prototype.toString.call(options.value);
  74. //如果value是对象或者数组对象的类型,就先用JSON.stringify转一下,再存进去
  75. if (Object.prototype.toString.call(options.value) == "[object Object]") {
  76. options.value = JSON.stringify(options.value);
  77. }
  78. if (Object.prototype.toString.call(options.value) == "[object Array]") {
  79. options.value = JSON.stringify(options.value);
  80. }
  81. // localStorage.setItem(options.name,options.value);
  82. uni.setStorageSync(options.name, options.value);
  83. }
  84. }
  85. /**
  86. * 缓存是否过期,过期自动删除
  87. * @param {Object} key
  88. * @param {Object} $bool true = 删除,false = 不删除
  89. */
  90. getExpireCahe(key, $bool) {
  91. try {
  92. let time = this.cacheGetHandler(key + this.cacheExpire);
  93. if (time) {
  94. let newTime = parseInt(time);
  95. if (time && time < this.time() && !Number.isNaN(newTime)) {
  96. if ($bool === undefined || $bool === true) {
  97. this.cacheClearHandler(key);
  98. this.cacheClearHandler(key + this.cacheExpire);
  99. }
  100. return false;
  101. } else return true;
  102. } else {
  103. return !!this.cacheGetHandler(key);
  104. }
  105. } catch (e) {
  106. return false;
  107. }
  108. }
  109. /**
  110. * 设置缓存
  111. * @param {Object} key
  112. * @param {Object} data
  113. */
  114. set(key, data, expire) {
  115. if (typeof data === "object") data = JSON.stringify(data);
  116. try {
  117. this.setExpireCaheTag(key, expire);
  118. return this.cacheSetHandler(key, data);
  119. } catch (e) {
  120. return false;
  121. }
  122. }
  123. /**
  124. * 检测缓存是否存在
  125. * @param {Object} key
  126. */
  127. has(key) {
  128. return this.getExpireCahe(key);
  129. }
  130. /**
  131. * 获取缓存
  132. * @param {Object} key
  133. * @param {Object} $default
  134. * @param {Object} expire
  135. */
  136. get(key, $default, expire) {
  137. try {
  138. let isBe = this.getExpireCahe(key);
  139. let data = this.cacheGetHandler(key);
  140. if (data && isBe) {
  141. if (typeof $default === "boolean") return JSON.parse(data);
  142. else return data;
  143. } else {
  144. if (typeof $default === "function") {
  145. let value = $default();
  146. this.set(key, value, expire);
  147. return value;
  148. } else {
  149. this.set(key, $default, expire);
  150. return $default;
  151. }
  152. }
  153. } catch (e) {
  154. return null;
  155. }
  156. }
  157. /**
  158. * 删除缓存
  159. * @param {Object} key
  160. */
  161. clear(key) {
  162. try {
  163. let cahceValue = this.cacheGetHandler(key + this.cacheExpire);
  164. if (cahceValue) this.cacheClearHandler(key + this.cacheExpire);
  165. return this.cacheClearHandler(key);
  166. } catch (e) {
  167. return false;
  168. }
  169. }
  170. /**
  171. * 清除过期缓存
  172. */
  173. clearOverdue() {
  174. // let cacheList = uni.getStorageInfoSync(),that = this;
  175. // if (typeof cacheList.keys === 'object'){
  176. // cacheList.keys.forEach(item=>{
  177. // that.getExpireCahe(item);
  178. // })
  179. // }
  180. }
  181. /**
  182. * 获取缓存,调用后无需转换数据类型
  183. * @param {Object} key
  184. */
  185. getItem(name) {
  186. // let item = localStorage.getItem(name);
  187. let item = uni.getStorageSync(name);
  188. //先将拿到的试着进行json转为对象的形式
  189. try {
  190. item = JSON.parse(item);
  191. } catch (error) {
  192. //如果不行就不是json的字符串,就直接返回
  193. item = item;
  194. }
  195. //如果有startTime的值,说明设置了失效时间
  196. if (item.startTime) {
  197. let date = new Date().getTime();
  198. //何时将值取出减去刚存入的时间,与item.expires比较,如果大于就是过期了,如果小于或等于就还没过期
  199. if (date - item.startTime > item.expires) {
  200. //缓存过期,清除缓存,返回false
  201. // localStorage.removeItem(name);
  202. uni.removeStorageSync(name);
  203. return false;
  204. } else {
  205. //缓存未过期,返回值
  206. return item.value;
  207. }
  208. } else {
  209. //如果没有设置失效时间,直接返回值
  210. return item;
  211. }
  212. }
  213. }
  214. export default new Cache();