storage.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. const common_vendor = require("../common/vendor.js");
  3. const postfix = "_expiry";
  4. const storage = {
  5. /**
  6. * 设置缓存
  7. * @param {[type]} k [键名]
  8. * @param {[type]} v [键值]
  9. * @param {[type]} t [时间、单位秒]
  10. */
  11. set(k, v, t) {
  12. common_vendor.index.setStorageSync(k, v);
  13. const seconds = parseInt(t);
  14. if (seconds > 0) {
  15. let timestamp = Date.parse(/* @__PURE__ */ new Date());
  16. timestamp = timestamp / 1e3 + seconds;
  17. common_vendor.index.setStorageSync(k + postfix, timestamp + "");
  18. } else {
  19. common_vendor.index.removeStorageSync(k + postfix);
  20. }
  21. },
  22. /**
  23. * 获取缓存
  24. * @param {[type]} k [键名]
  25. * @param {[type]} def [获取为空时默认]
  26. */
  27. get(k, def) {
  28. const deadtime = parseInt(common_vendor.index.getStorageSync(k + postfix));
  29. if (deadtime) {
  30. if (parseInt(deadtime) < Date.parse(/* @__PURE__ */ new Date()) / 1e3) {
  31. if (def) {
  32. return def;
  33. } else {
  34. return false;
  35. }
  36. }
  37. }
  38. const res = common_vendor.index.getStorageSync(k);
  39. if (res) {
  40. return res;
  41. }
  42. if (def == void 0 || def == "") {
  43. def = false;
  44. }
  45. return def;
  46. },
  47. /**
  48. * 删除指定缓存
  49. * @param {Object} k
  50. */
  51. remove(k) {
  52. common_vendor.index.removeStorageSync(k);
  53. common_vendor.index.removeStorageSync(k + postfix);
  54. },
  55. /**
  56. * 清理所有缓存
  57. * @return {[type]} [description]
  58. */
  59. clear() {
  60. common_vendor.index.clearStorageSync();
  61. }
  62. };
  63. exports.storage = storage;