cache.js 5.3 KB

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