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

@easyevery/ez-store

v0.0.23

Published

a storage library

Downloads

8

Readme

ez-store 以简单,统一的方式使用localStoragesessionStorage、内存对象。

开始使用

1.创建同步缓存项

import EzStore from '@easyevery/ez-store';

export const activeCache = EzStore.createLocal<boolean>('active-a', {
  prefix: '平台A',
});

// 使用同步缓存项
activeCache.set(true);
activeCache.get();

2. 创建异步缓存项

import EzStore from '@easyevery/ez-store';

export const asyncCache = EzStore.createSession('test', {
  getter: async () => {
    return Promise.resolve({ id: 1 });
  },
  expire: EzStore.TimeSpan.day * 2, // 2天后过期
});

// 使用异步缓存项
await asyncCache.get();
await asyncCache.set({ id: 2 });

注意:

getter: 异步缓存项优先从 cache 中取值,若没有则调用 getter 方法。

setter: 异步缓存项优使用 setter 的返回值来设置缓存的值

3.支持设置过期时间

export const requestCache = EzStore.createLocal<boolean>('request', {
  prefix: '平台A',
  expire: dayjs().add('day', 1).valueOf(), // 1天后过期
});

Interface

1. 缓存项

type Cache = Readonly<{
  name: string;
  get: () => T | null;
  set: (value: T) => boolean;
  remove: () => boolean;
}>;

2. 创建缓存项的方法

type IStoreOptions = { prefix?: string; version?: number; expire?: number };

function CreateCacheImp<T = any>(
  key: string,
  options?: IStoreOptions & {
    getter?: ((params: any) => Promise<T>) | (() => null);
    setter?: ((value: T) => Promise<T>) | ((val: T) => T);
  },
): Cache;

3. 额外提供一个内置的时间戳枚举

enum TimeSpan {
  second = 1000,
  minute = 60 * 1000,
  hour = 3600 * 1000,
  day = 24 * 3600 * 1000,
  week = 7 * 24 * 3600 * 1000,
  month = 30 * 24 * 3600 * 1000,
  year = 365 * 24 * 3600 * 1000,
}

API

1.内置方法:创建特定类型的缓存项

| 方法 | 说明 | 类型 | 默认值 | 版本 | | -------- | ------------ | ------------ | ------ | ------ | | createLocal| 基于localStorate的同步缓存 | CreateCacheImp | - | - | | createSession| 基于sessionStorate的同步缓存 | CreateCacheImp | - | - | | createMemory| 基于内存的的同步缓存 | CreateCacheImp | - | - | | createAsyncLocal| 基于localStorate的异步缓存 | CreateCacheImp | - | - | | createAsyncSession | 基于sessionStorate的异步缓存 | CreateCacheImp | - | - | | createAsyncMemory| 基于内存的的异步缓存 | CreateCacheImp | - | - |

入参

| 参数 | 说明 | 类型 | 默认值 | 版本 | | -------- | ------------ | ------------ | ------ | ------ | | key | 缓存的key | string | - | - | | options | 缓存的options | IStoreOptions | - | - |

2.内置方法:基于缓存类型创建缓存项

type StoreType = 'localStorage' | 'sessionStorage' | 'memory';

function SyncCacheImp<T = any>(
  type: StoreType,
  key: string,
  options?: IStoreOptions & {
    getter?: ((params: any) => Promise<T>) | (() => null);
    setter?: ((value: T) => Promise<T>) | ((val: T) => T);
  },
): Cache;

| 方法 | 说明 | 类型 | 默认值 | 版本 | | -------- | ------------ | ------------ | ------ | ------ | | createSyncCache| 根据type创建同步的缓存 | SyncCacheImp | - | - | | createAsyncCache| 根据type创建异步的缓存 | SyncCacheImp | - | - |

相比 createLocal、createAsyncLocal 等方法,只是多了第一个参数 type ,但返回值都是缓存项Cache

入参

| 参数 | 说明 | 类型 | 默认值 | 版本 | | -------- | -------------| ------------ | ------ | ------ | | type | 缓存的类型 | StoreType | - | - | | key | 缓存的key | string | - | - | | options | 缓存的options | IStoreOptions | - | - |

3.内置方法:设置全局统一前缀 setConfig

| 参数 | 说明 | 类型 | 默认值 | 版本 | | -------- | -------------| ------------ | ------ | ------ | | options | options | {prefix:string} | - | - |

4. 缓存项的属性和方法

| 参数 | 说明 | 类型 | 默认值 | 版本 | | -------- | -------------| ------------ | ------ | ------ | | name | 获取缓存的key | string | - | - | | get | 获取缓存的值 | () => T | null; | - | - | | set | 设置缓存的值 | (value: T) => boolean; | - | - | | remove | 移除缓存的值 | () => boolean; | - | - |

安装

npm i @easyevery/ez-store

yarn add @easyevery/ez-store

License

MIT