npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tool-developer/wx-storage

v0.0.6-canary.6

Published

wx miniprogram storage

Downloads

1

Readme

@tool-developer/wx-storage

基于数据缓存相关接口进行封装。

对外提供一致的api,并添加“存储过期”,原生的存储诸如localStorage之类的,是不支持存储过期的。

默认存储过期时间为3600000ms,即1小时,可通过设置第三个参数自定义存储过期时间(单位ms),设置-1表示永久存储(直到用户或者系统清除缓存)。

除了同步返回之外,还提供Promise和callback两种返回。

用法

npm install --save @tool-developer/wx-storage

or

yarn add @tool-developer/wx-storage
import storage from '@tool-developer/wx-storage'

方法介绍

不管是async,还是session,对外都提供一致的api方法,具体如下: | 方法 | 说明 | | :-- | :-- | | get(key[,cb]) | 获取指定key存储数据 | | set(key,value,expire[,cb]) | 根据key,存储value数据,并指定过期时间expire | | remove(key[,cb]) | 移除指定key | | clear([cb]) | 清除所有 |

用法举例

使用的是小程序提供的Sync结尾的同步版本。

  1. 默认导入的是localStorage方式存储
import storage from '@tool-developer/wx-storage';

// ...
  1. 使用sessionStorage的方式导入
import storage from '@tool-developer/wx-storage/session';

// ...
  1. 使用async的方式导入
import _storage from '@tool-developer/wx-storage';
//import _storage from '@tool-developer/wx-storage/session';
//
import _async from '@tool-developer/wx-storage/async';
//
// 这里得到的对象方法,为promise
// 如果需要调整为session,只需要将_storage替换为sessionStorage方式导入即可
const storage = _async(_storage);

// ...
  1. 存储数据操作 存储的key为null或者undefined,不会进行存储,也不会脚本报错,且返回undefined。
// 
storage.set('key1','value1')

// 指定过期时间为12小时
storage.set('key2','value2',3600000 * 12)

// async方式
storage.set('key3','value3').then(()=>{
  // set返回值为undefined
  // 其他处理逻辑
})

// callback方式,指定过期时间12小时
storage.set('key4','value4',3600000 * 12,()=>{
  // set返回值为undefined
  // 其他处理逻辑
})
// callback方式,未指定过期时间
storage.set('key4','value4',()=>{
  // set返回值为undefined
  // 其他处理逻辑
})
  1. 获取数据操作 获取不存在的key的数据,返回的是undefined。
//
const key1 = storage.get('key1')

// async方式
storage.get('key1').then((data)=>{
  //返回获取到的数据
})
// callback方式
storage.get('key1',(data)=>{
  // 回调返回获取到的数据
})
  1. 清除数据操作 清除不能存在的key,返回结果为undefined。
//
storage.remove('key1')

// async方式
storage.remove('key1').then(()=>{
  //返回undefined
})
// callback方式
storage.remove('key1',()=>{
  // 回调返回undefined
})
  1. 清空数据操作
//
storage.clear()

// async方式
storage.clear().then(()=>{
  //返回undefined
})
// callback方式
storage.clear(()=>{
  // 回调返回undefined
})