cache.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. return false;
  107. }
  108. }
  109. /**
  110. * 检测缓存是否存在
  111. * @param {Object} key
  112. */
  113. has(checkwhethethecacheexists) {
  114. this.clearOverdue();
  115. return this.getExpireCahe(checkwhethethecacheexists);
  116. }
  117. /**
  118. * 获取缓存
  119. * @param {Object} key
  120. * @param {Object} $default
  121. * @param {Object} expire
  122. */
  123. get(key, $default, expire) {
  124. this.clearOverdue();
  125. try {
  126. let isBe = this.getExpireCahe(key);
  127. let data = this.cacheGetHandler(key);
  128. if (data && isBe) {
  129. if (typeof $default === 'boolean')
  130. return JSON.parse(data);
  131. else
  132. return data;
  133. } else {
  134. if (typeof $default === 'function') {
  135. let value = $default();
  136. this.set(key, value, expire);
  137. return value;
  138. } else {
  139. this.set(key, $default, expire);
  140. return $default;
  141. }
  142. }
  143. } catch (e) {
  144. return null;
  145. }
  146. }
  147. /**
  148. * 删除缓存
  149. * @param {Object} key
  150. */
  151. clear(key) {
  152. try {
  153. let cahceValue = this.cacheGetHandler(this.cacheExpire),
  154. index = false;
  155. if (cahceValue && typeof cahceValue === 'object' && cahceValue.length) {
  156. cahceValue.map((item, i) => {
  157. if (item.key === key) {
  158. index = i;
  159. }
  160. });
  161. if (index !== false) {
  162. cahceValue.splice(index, 1);
  163. }
  164. this.cacheSetHandler(this.cacheExpire, cahceValue);
  165. }
  166. return this.cacheClearHandler(key);
  167. } catch (e) {
  168. return false;
  169. }
  170. }
  171. /**
  172. * 清除过期缓存
  173. */
  174. clearOverdue() {
  175. let cahceValue = this.cacheGetHandler(this.cacheExpire),
  176. time = this.time(),
  177. newBeOverdueValue = [],
  178. newTagValue = [];
  179. if (cahceValue && typeof cahceValue === 'object' && cahceValue.length) {
  180. cahceValue.map(item => {
  181. if (item) {
  182. if ((item.expire !== undefined && item.expire > time) || item.expire === 0) {
  183. newTagValue.push(item);
  184. } else {
  185. newBeOverdueValue.push(item.key);
  186. }
  187. }
  188. });
  189. }
  190. //保存没有过期的缓存标签
  191. if (newTagValue.length !== cahceValue.length) {
  192. this.cacheSetHandler(this.cacheExpire, newTagValue);
  193. }
  194. //删除过期缓存
  195. newBeOverdueValue.forEach(k => {
  196. this.cacheClearHandler(k);
  197. })
  198. }
  199. }
  200. export default new Cache;