Storage.ts 3.1 KB

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