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

@bwrong/storage

v1.1.0

Published

为了解决本地缓存(localStorage或sessionStorage)的一些痛点,进行二次封装的本地缓存插件

Downloads

14

Readme

@bwrong/storage

npm npm GitHub stars

为了解决本地缓存(localStorage或sessionStorage)的一些痛点,进行二次封装的本地缓存插件。

  • 可设置驱动,支持localStorage和sessionStorage。
  • 支持设置前缀,避免冲突
  • value值支持多种格式,不必要求为string
  • 支持加密存储,可自定义加密算法
  • 支持class集成扩展功能

安装

npm install @bwrong/storage
// 或
yarn add @bwrong/storage
// 或
pnpm install @bwrong/storage

基本使用

为了方便使用,默认提供了一个全局实例,如果不满足,可以自行初始化Storage实例。

  1. 使用默认全局实例。
import { setStorageConfig,setStorage, getStorage, removeStorage, clearStorage } from '@bwrong/storage';
setStorageConfig({
  driver: localStorage;
  // ...
});
setStorage(key, value, { expire: 10 }); //设置缓存
getStorage(key); //获取缓存
removeStorage(key1,key2); //移除特定标识缓存
clearStorage(); //清除所有缓存
  1. 自己实例化
import UStorage from '@bwrong/storage';
// import Utf8 from 'crypto-js/enc-utf8';
// import Base64 from 'crypto-js/enc-base64';

const storage = new UStorage({
  driver: localStorage,
  prefix: ''
  // 需要加密的时候可以使用
  // encryptFn(val) {
  //   return Base64.stringify(Utf8.parse(val));
  // },
  // decryptFn(val) {
  //   return Base64.parse(val).toString(Utf8);
  // }
});
export const getStorage = storage.get.bind(storage);
export const setStorage = storage.set.bind(storage);
export const removeStorage = storage.remove.bind(storage);
export const clearStorage = storage.clear.bind(storage);

配置

  • driver?: Storage

驱动:localStorage、sessionStorage,默认使用 localStorage

  • prefix?: string

名称前缀,方便区分

  • encryptFn?: StorageCryptFn

加密,配合解密decryptFn使用,需要保证使用一致算法加解密

  • decryptFn?: StorageCryptFn

解密,配合加密encryptFn使用,需要保证使用一致算法加解密

方法

  • new Storage(option: StorageOption) 实例化新的实例
  • config(option: StorageOption) 配置,也可以在实例化时传入
export interface StorageOption {
  /** 驱动:localStorage、sessionStorage,默认使用 localStorage*/
  driver?: Storage;
  /** 名称前缀,方便区分 */
  prefix?: string;
  /** 加密,配合解密decryptFn使用,需要保证使用统一算法加解密 */
  encryptFn?: StorageCryptFn;
  /** 解密,配合加密encryptFn使用,需要保证使用统一算法加解密 */
  decryptFn?: StorageCryptFn;
}
  • set(key: string, data: any, config?: StorageConfig) 设置缓存
export interface StorageConfig {
  //过期时间 单位秒
  expire?: number;
}
  • get<T = any>(key: string) 获取缓存

  • remove(...keys: string[]) 移除缓存

  • clear 清除缓存