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