cache.js 5.2 KB

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