storage.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * window.localStorage 浏览器永久缓存
  3. * @method set 设置永久缓存
  4. * @method get 获取永久缓存
  5. * @method remove 移除永久缓存
  6. * @method clear 移除全部永久缓存
  7. */
  8. export const Local = {
  9. // 设置永久缓存
  10. set(key, val) {
  11. window.localStorage.setItem(key, JSON.stringify(val))
  12. },
  13. // 获取永久缓存
  14. get(key) {
  15. let json = window.localStorage.getItem(key)
  16. return JSON.parse(json)
  17. },
  18. // 移除永久缓存
  19. remove(key) {
  20. window.localStorage.removeItem(key)
  21. },
  22. // 移除全部永久缓存
  23. clear() {
  24. window.localStorage.clear()
  25. }
  26. }
  27. /**
  28. * window.sessionStorage 浏览器临时缓存
  29. * @method set 设置临时缓存
  30. * @method get 获取临时缓存
  31. * @method remove 移除临时缓存
  32. * @method clear 移除全部临时缓存
  33. */
  34. export const Session = {
  35. // 设置临时缓存
  36. set(key, val) {
  37. window.sessionStorage.setItem(key, JSON.stringify(val))
  38. },
  39. // 获取临时缓存
  40. get(key) {
  41. let json = window.sessionStorage.getItem(key)
  42. return JSON.parse(json)
  43. },
  44. // 移除临时缓存
  45. remove(key) {
  46. window.sessionStorage.removeItem(key)
  47. },
  48. // 移除全部临时缓存
  49. clear() {
  50. window.sessionStorage.clear()
  51. }
  52. }