Socket
Book a DemoInstallSign in
Socket

@jojy/storage

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jojy/storage

A simple storage

1.0.3
latest
npmnpm
Version published
Maintainers
1
Created
Source

简易设置LocalStorage的工具

安装和使用

npm i @jojy/storage -S

import storage from '@jojy/storage';

storage.use('xxx.com');

storage.set('user', {name: 'abc', age: 25}, { expires: 3600 * 24 * 30 });
storage.get('user');

storage.set('isLogin', false, { namespace: 'xxx.com_web', expires: 3600 * 24 });
storage.get('isLogin', { namespace: 'xxx.com_web' });

// 注意storage.use()要在文件第一次引用时调用,如果不好确定第一次调用的地方,可以包装一下

import myStorage from '@jojy/storage';
myStorage.use('xxx');
export default myStorage;

方法

  • use(namespace),全局改变namespace
  • set(key, val, opts),设置key和val,可设置namespace和expires
  • get(key, opts),获取key的值
  • remove(key, opts),移除key
  • has(key, opts),是否设置key
  • now(),返回10位时间戳

实现原理

  • 以namespace为LocalStorage的key,数据集的JSON为value。格式:{ namespace: JSON.stringify({ key: { val, exp }, ... }) }
  • 可全局设置默认的namespace,也可在方法中指定namespace。

功能

  • namespace 默认值为 __def,可使用use()方法改变 全局 namespace。也可以在使用时,指定参数namespace
  • expires 默认值为 31536000(一年),单位是 秒;expires <= 0,则永不过期。原则上数据都应该有过期时间。

namespace使用场景

  • 不同的项目使用同一个域名,可以设置不同namespace区分存储,防止key冲突
  • 存储大数据时。所有数据都存入同一个namespace,如果有大数据建议使用单独的namespace来存储

Keywords

localStorage

FAQs

Package last updated on 30 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.