cache.js 5.9 KB

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