storage.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // 默认缓存期限为7天
  2. const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7;
  3. /**
  4. * 创建本地缓存对象
  5. * @param {string=} prefixKey -
  6. * @param {Object} [storage=sessionStorage] - sessionStorage | localStorage
  7. */
  8. export const createStorage = ({
  9. prefixKey = '',
  10. storage = sessionStorage
  11. } = {}) => {
  12. /**
  13. * 本地缓存类
  14. * @class Storage
  15. */
  16. const Storage = class {
  17. private storage = storage;
  18. private prefixKey?: string = prefixKey;
  19. private getKey(key: string) {
  20. return `${this.prefixKey}${key}`.toUpperCase();
  21. }
  22. /**
  23. * @description 设置缓存
  24. * @param {string} key 缓存键
  25. * @param {*} value 缓存值
  26. * @param expire
  27. */
  28. set(key: string, value: any, expire: number | null = DEFAULT_CACHE_TIME) {
  29. const stringData = JSON.stringify({
  30. value,
  31. expire: expire !== null ? new Date().getTime() + expire * 1000 : null
  32. });
  33. this.storage.setItem(this.getKey(key), stringData);
  34. }
  35. /**
  36. * 读取缓存
  37. * @param {string} key 缓存键
  38. * @param {*=} def 默认值
  39. */
  40. get(key: string, def: any = null) {
  41. const item = this.storage.getItem(this.getKey(key));
  42. if (item) {
  43. try {
  44. const data = JSON.parse(item);
  45. const { value, expire } = data;
  46. // 在有效期内直接返回
  47. if (expire === null || expire >= Date.now()) {
  48. return value;
  49. }
  50. this.remove(key);
  51. } catch (e) {
  52. return def;
  53. }
  54. }
  55. return def;
  56. }
  57. /**
  58. * 从缓存删除某项
  59. * @param {string} key
  60. */
  61. remove(key: string) {
  62. this.storage.removeItem(this.getKey(key));
  63. }
  64. /**
  65. * 清空所有缓存
  66. * @memberOf Cache
  67. */
  68. clear(): void {
  69. this.storage.clear();
  70. }
  71. /**
  72. * 设置cookie
  73. * @param {string} name cookie 名称
  74. * @param {*} value cookie 值
  75. * @param {number=} expire 过期时间
  76. * 如果过期时间未设置,默认关闭浏览器自动删除
  77. * @example
  78. */
  79. setCookie(
  80. name: string,
  81. value: any,
  82. expire: number | null = DEFAULT_CACHE_TIME
  83. ) {
  84. document.cookie = `${this.getKey(name)}=${value}; Max-Age=${expire}`;
  85. }
  86. /**
  87. * 根据名字获取cookie值
  88. * @param name
  89. */
  90. getCookie(name: string): string {
  91. const cookieArr = document.cookie.split('; ');
  92. for (let i = 0, length = cookieArr.length; i < length; i++) {
  93. const kv = cookieArr[i].split('=');
  94. if (kv[0] === this.getKey(name)) {
  95. return kv[1];
  96. }
  97. }
  98. return '';
  99. }
  100. /**
  101. * 根据名字删除指定的cookie
  102. * @param {string} key
  103. */
  104. removeCookie(key: string) {
  105. this.setCookie(key, 1, -1);
  106. }
  107. /**
  108. * 清空cookie,使所有cookie失效
  109. */
  110. clearCookie(): void {
  111. const keys = document.cookie.match(/[^ =;]+(?==)/g);
  112. if (keys) {
  113. for (let i = keys.length; i--; ) {
  114. document.cookie = keys[i] + '=0;expire=' + new Date(0).toUTCString();
  115. }
  116. }
  117. }
  118. };
  119. return new Storage();
  120. };
  121. export const storage = createStorage();
  122. export default Storage;