storage.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import store from 'store'
  2. export default {
  3. // 后缀标识
  4. suffix: "_deadtime",
  5. /**
  6. * 获取
  7. * @param {string} key 关键字
  8. */
  9. get(key) {
  10. return store.get(key)
  11. },
  12. /**
  13. * 获取全部
  14. */
  15. info() {
  16. let d = {}
  17. store.each(function (value, key) {
  18. d[key] = value
  19. })
  20. return d
  21. },
  22. /**
  23. * 设置
  24. * @param {string} key 关键字
  25. * @param {*} value 值
  26. * @param {number} expires 过期时间
  27. */
  28. set(key, value, expires) {
  29. store.set(key, value);
  30. if (expires) {
  31. store.set(`${key}${this.suffix}`, Date.parse(new Date()) + expires * 1000);
  32. }
  33. },
  34. /**
  35. * 是否过期
  36. * @param {string} key 关键字
  37. */
  38. isExpired(key) {
  39. return (this.getExpiration(key) || 0) - Date.parse(new Date()) <= 2000;
  40. },
  41. /**
  42. * 获取到期时间
  43. * @param {string} key 关键字
  44. */
  45. getExpiration(key) {
  46. return this.get(key + this.suffix)
  47. },
  48. /**
  49. * 移除
  50. * @param {string} key 关键字
  51. */
  52. remove(key) {
  53. store.remove(key)
  54. this.removeExpiration(key)
  55. },
  56. /**
  57. * 移除到期时间
  58. * @param {string} key 关键字
  59. */
  60. removeExpiration(key) {
  61. store.remove(key + this.suffix)
  62. },
  63. /**
  64. * 清理
  65. */
  66. clearAll() {
  67. store.clearAll()
  68. }
  69. };