Skip to content

XYStorage 本地缓存

基于 localStoage sessionStorage js-cookie 的本地缓存

安装

使用 npm:

shell
npm install js-cookie xy-storage -S

使用 jsDelivr CDN:

html
<script src="https://cdn.jsdelivr.net/npm/js-cookie/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/xy-storage/dist/index.min.js"></script>

示例

基础用法

js
import XYStorage from 'xy-storage'

const options = {}

const storage = {
  local: new XYStorage({
    ...options,
    name: 'local'
  }),
  session: new XYStorage({
    ...options,
    name: 'session'
  }),
  cookie: new XYStorage({
    ...options,
    name: 'cookie'
  })
}

storage.local.setItem(key, value)

storage.local.getItem(key)

storage.local.removeItem(key)

storage.local.clear()

API

参数

属性说明类型默认值
namespace命名空间,添加到 keystring-
name储存方式stringlocal
value储存内容string object boolean-
default未获取到值时的返回值string object booleannull
attrs属性,详见 attrsobject-

Attrs

名称说明适用于类型默认值
expires有效期。类型为 number 时,单位为天local cookienumber Date-
path储存路径cookiestring-
domain域名cookiestring-
secure是否需要安全协议cookiebooleanfalse

方法

setItem(key[, attrs])

getItem(key[, def])

removeItem(key)

clear()

依赖

js-cookie

参考文档

js-cookie

sessionStorage

localStorage

本文档内容版权为 XYAdmin 作者所有,保留所有权利。