cache.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. 'use strict';
  2. /**
  3. * [cacheValue 缓存对象]
  4. * @author szjcomo
  5. * @createTime 2020-09-29
  6. * @param {[type]} key [description]
  7. * @param {[type]} value [description]
  8. * @param {[type]} expire [description]
  9. * @return {[type]} [description]
  10. */
  11. const Cache = function(key,value,expire) {
  12. this.szjcomo_key = key;
  13. this.szjcomo_value = value;
  14. this.expire_time = expire;
  15. }
  16. /**
  17. * [instanceof 判断是否是自身的对象]
  18. * @author szjcomo
  19. * @createTime 2020-09-29
  20. * @param {[type]} obj [description]
  21. * @return {[type]} [description]
  22. */
  23. Cache.instanceof = function(obj) {
  24. if(obj instanceof Object) {
  25. if(obj.hasOwnProperty('szjcomo_key') && obj.hasOwnProperty('szjcomo_value')) return true;
  26. return false;
  27. }
  28. return false;
  29. }
  30. export const localStore = {
  31. /**
  32. * [get 获取本地存储]
  33. * @author szjcomo
  34. * @createTime 2020-09-29
  35. * @param {[type]} key [description]
  36. * @return {[type]} [description]
  37. */
  38. get:function(key) {
  39. let that = this;
  40. let result = window.localStorage.getItem(key);
  41. if(!result) return false;
  42. try {
  43. let obj = JSON.parse(result);
  44. if(Cache.instanceof(obj)) {
  45. let curTime = new Date().getTime();
  46. if(obj.expire_time == 0) return obj.szjcomo_value;
  47. if(obj.expire_time >= curTime) return obj.szjcomo_value;
  48. that.del(obj.szjcomo_key);
  49. return false;
  50. } else {
  51. return obj;
  52. }
  53. } catch(err) {
  54. console.error(err);
  55. return false;
  56. }
  57. },
  58. /**
  59. * [set 设置缓存]
  60. * @author szjcomo
  61. * @createTime 2020-09-29
  62. * @param {[type]} key [description]
  63. * @param {[type]} value [description]
  64. * @param {[type]} seconds [description]
  65. */
  66. set:function(key,value,seconds = 0) {
  67. try {
  68. let tmp_expire = 0;
  69. if(seconds > 0) tmp_expire = (new Date().getTime() + seconds * 1000);
  70. let tmpObj = new Cache(key,value,tmp_expire);
  71. window.localStorage.setItem(key,JSON.stringify(tmpObj));
  72. return true;
  73. } catch(err) {
  74. console.error(err);
  75. return false;
  76. }
  77. },
  78. /**
  79. * [del 删除缓存]
  80. * @author szjcomo
  81. * @createTime 2020-09-29
  82. * @param {[type]} key [description]
  83. * @return {[type]} [description]
  84. */
  85. del:function(key) {
  86. return window.localStorage.removeItem(key);
  87. }
  88. };
  89. /**
  90. * [sessStore session存储]
  91. * @type {Object}
  92. */
  93. export const sessStore = {
  94. /**
  95. * [get 获取本地存储]
  96. * @author szjcomo
  97. * @createTime 2020-09-29
  98. * @param {[type]} key [description]
  99. * @return {[type]} [description]
  100. */
  101. get:function(key) {
  102. let that = this;
  103. let result = window.sessionStorage.getItem(key);
  104. if(!result) return false;
  105. try {
  106. let obj = JSON.parse(result);
  107. if(Cache.instanceof(obj)) {
  108. let curTime = new Date().getTime();
  109. if(obj.expire_time == 0) return obj.szjcomo_value;
  110. if(obj.expire_time >= curTime) return obj.szjcomo_value;
  111. that.del(obj.szjcomo_key);
  112. return false;
  113. } else {
  114. return obj;
  115. }
  116. } catch(err) {
  117. console.error(err);
  118. return false;
  119. }
  120. },
  121. /**
  122. * [set 设置缓存]
  123. * @author szjcomo
  124. * @createTime 2020-09-29
  125. * @param {[type]} key [description]
  126. * @param {[type]} value [description]
  127. * @param {[type]} seconds [description]
  128. */
  129. set:function(key,value,seconds = 0) {
  130. try {
  131. let tmp_expire = 0;
  132. if(seconds > 0) tmp_expire = (new Date().getTime() + seconds * 1000);
  133. let tmpObj = new Cache(key,value,tmp_expire);
  134. window.sessionStorage.setItem(key,JSON.stringify(tmpObj));
  135. return true;
  136. } catch(err) {
  137. console.error(err);
  138. return false;
  139. }
  140. },
  141. /**
  142. * [del 删除缓存]
  143. * @author szjcomo
  144. * @createTime 2020-09-29
  145. * @param {[type]} key [description]
  146. * @return {[type]} [description]
  147. */
  148. del:function(key) {
  149. return window.sessionStorage.removeItem(key);
  150. }
  151. };
  152. /**
  153. * [szjcomoCache 导出缓存类]
  154. * @author szjcomo
  155. * @createTime 2020-09-29
  156. * @param {String} type [description]
  157. * @return {[type]} [description]
  158. */
  159. const szjcomoCache = function(type = 'sess') {
  160. if(type == 'sess') {
  161. if(!window.sessionStorage) throw new Error('您的浏览器不支持本地sessionStorage存储');
  162. return sessStore;
  163. }
  164. if(!window.localStorage) throw new Error('您的浏览器不支持本地localStorage存储');
  165. return localStore;
  166. }
  167. export default szjcomoCache;