cache.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // +----------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2016~2021 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), newTag = [],newKeys = [];
  45. if (typeof tag === 'object' && tag.length) {
  46. newTag = tag.map(item => {
  47. newKeys.push(item.key);
  48. if (item.key === key) {
  49. item.expire = expire === 0 ? 0 : this.time() + expire;
  50. }
  51. return item;
  52. });
  53. }
  54. if (!newKeys.length || newKeys.indexOf(key) === -1) {
  55. newTag.push({
  56. key: key,
  57. expire: expire === 0 ? 0 : this.time() + expire
  58. });
  59. }
  60. this.cacheSetHandler(this.cacheExpire, newTag);
  61. }
  62. }
  63. /**
  64. * 缓存是否过期,过期自动删除
  65. * @param {Object} key
  66. * @param {Object} $bool true = 删除,false = 不删除
  67. */
  68. getExpireCahe(key, $bool) {
  69. try {
  70. let tag = this.cacheGetHandler(this.cacheExpire),time = 0,index = false;
  71. if (typeof tag === 'object' && tag.length) {
  72. tag.map((item,i) => {
  73. if(item.key === key){
  74. time = item.expire
  75. index = i
  76. }
  77. });
  78. if (time) {
  79. let newTime = parseInt(time);
  80. if (time && time < this.time() && !Number.isNaN(newTime)) {
  81. if ($bool === undefined || $bool === true) {
  82. this.cacheClearHandler(key);
  83. if(index !== false){
  84. tag.splice(index,1)
  85. this.cacheClearHandler(this.cacheExpire,tag);
  86. }
  87. }
  88. return false;
  89. } else
  90. return true;
  91. } else {
  92. return !!this.cacheGetHandler(key);
  93. }
  94. }
  95. return false;
  96. } catch (e) {
  97. return false;
  98. }
  99. }
  100. /**
  101. * 设置缓存
  102. * @param {Object} key
  103. * @param {Object} data
  104. */
  105. set(key, data, expire) {
  106. if (data === undefined) {
  107. return true;
  108. }
  109. if (typeof data === 'object')
  110. data = JSON.stringify(data);
  111. try {
  112. this.setExpireCaheTag(key, expire);
  113. return this.cacheSetHandler(key, data);
  114. } catch (e) {
  115. return false;
  116. }
  117. }
  118. /**
  119. * 检测缓存是否存在
  120. * @param {Object} key
  121. */
  122. has(checkwhethethecacheexists) {
  123. this.clearOverdue();
  124. return this.getExpireCahe(checkwhethethecacheexists);
  125. }
  126. /**
  127. * 获取缓存
  128. * @param {Object} key
  129. * @param {Object} $default
  130. * @param {Object} expire
  131. */
  132. get(key, $default, expire) {
  133. this.clearOverdue();
  134. try {
  135. let isBe = this.getExpireCahe(key);
  136. let data = this.cacheGetHandler(key);
  137. if (data && isBe) {
  138. if (typeof $default === 'boolean')
  139. return JSON.parse(data);
  140. else
  141. return data;
  142. } else {
  143. if (typeof $default === 'function') {
  144. let value = $default();
  145. this.set(key, value, expire);
  146. return value;
  147. } else {
  148. this.set(key, $default, expire);
  149. return $default;
  150. }
  151. }
  152. } catch (e) {
  153. return null;
  154. }
  155. }
  156. /**
  157. * 删除缓存
  158. * @param {Object} key
  159. */
  160. clear(key) {
  161. try {
  162. let cahceValue = this.cacheGetHandler(this.cacheExpire),
  163. index = false;
  164. if (cahceValue && typeof cahceValue === 'object' && cahceValue.length) {
  165. cahceValue.map((item, i) => {
  166. if (item.key === key) {
  167. index = i;
  168. }
  169. });
  170. if (index !== false) {
  171. cahceValue.splice(index, 1);
  172. }
  173. this.cacheSetHandler(this.cacheExpire, cahceValue);
  174. }
  175. return this.cacheClearHandler(key);
  176. } catch (e) {
  177. return false;
  178. }
  179. }
  180. /**
  181. * 清除过期缓存
  182. */
  183. clearOverdue() {
  184. let cahceValue = this.cacheGetHandler(this.cacheExpire),
  185. time = this.time(),
  186. newBeOverdueValue = [],
  187. newTagValue = [];
  188. if (cahceValue && typeof cahceValue === 'object' && cahceValue.length) {
  189. cahceValue.map(item => {
  190. if (item) {
  191. if ((item.expire !== undefined && item.expire > time) || item.expire === 0) {
  192. newTagValue.push(item);
  193. } else {
  194. newBeOverdueValue.push(item.key);
  195. }
  196. }
  197. });
  198. }
  199. //保存没有过期的缓存标签
  200. if (newTagValue.length !== cahceValue.length) {
  201. this.cacheSetHandler(this.cacheExpire, newTagValue);
  202. }
  203. //删除过期缓存
  204. newBeOverdueValue.forEach(k => {
  205. this.cacheClearHandler(k);
  206. })
  207. }
  208. }
  209. export default new Cache;