# front-end-cache

> 支持typescript的前端浏览器缓存管理工具，browser front end cache (支持localstorage, sessionstorage, cookie)

Latest version **1.0.2** (published 2023-03-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install front-end-cache
pnpm add front-end-cache
yarn add front-end-cache
bun add front-end-cache
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2023-03-29 |
| First published | 2021-06-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 233.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | sonamu.liao |
| Maintainers | sonamu.liao |
| Keywords | typescript, ts, front end, cache, memory, storage, localstorage, sessionstorage, cookie, 缓存, 前端缓存, 浏览器缓存 |

## Links

- npm: https://www.npmjs.com/package/front-end-cache
- Repository: https://github.com/sssonamuuu/front-end-cache
- Homepage: https://github.com/sssonamuuu/front-end-cache#readme
- Issues: https://github.com/sssonamuuu/front-end-cache/issues
- npm.io page: https://npm.io/package/front-end-cache

## Dependencies (1)

- [core-js](https://npm.io/package/core-js.md) ^3.14.0

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2023-03-29
- 1.0.1 — 2021-06-15
- 1.0.0 — 2021-06-15

## README

# FrontEndCache

统一前端浏览器缓存管理，支持 `cookie`、`localStorage`、`sessionStorage`。

## 文档


语法：`new FrontEndCache<ValueType>(key[, options]);`

### `key`

当前缓存对象的 `key`，可以通过 `options.encryKey` 对其加密。

### `options` 属性

参数|类型|必填|默认值|说明
:---:|:---:|:---:|:---|:---
type|`localStorage` \| `sessionStorage` \| `cookie`|❌|localStorage|类型
domain|string|❌|location.hostname|**仅仅** `type=cookie`<br/>**注意和 原生 `cookie` 的区分，默认不是当前 `path`**
domain|string|❌|/|同 `cookie` 的 `path` 配置
expires|number|❌|-|过期时间，默认单位 `seconds` <br/> 针对 `sessionStorage` 和 `cookie` 默认值为当前会话 <br/> 针对 `localStorage` 默认值为 永久
expiresUnit|`years` \| `months` \| `days` \| `hours` \| `minutes` \| `seconds`|❌|seconds|过期时间单位
secure|boolean|❌|false|**仅仅** `type=cookie`
serialize|boolean|❌|true|是否序通过 `JSON.stringify` 列化 `value` 数<br/>如果为 `cookie` 还会通过 `encodeURIComponent` 一次 `JSON.stringify` 后的数据<br/>**仅在 `value` 类型为 `string` 的时候可以禁用序列化**，但同样会调用 `encodeURIComponent`
encryKey|`(key: string) => string`|❌|-|加密 `key` 的函数<br/>**需要注意**如果 `type = 'cookie'`, 最终的 `key` 也会 `encodeURIComponent`

## 使用

``` typescript
/** cache.ts */

import FrontEndCache from 'front-end-cache';

export const accessToken = new FrontEndCache<string>('ACCESS_TOKEN', { type: 'cookie', serialize: false });

export const userinfoCache = new FrontEndCache<UserInfoModel>('PROJECTNAME_USERINFO_CACHE');

/** more cache */
```

``` typescript
import { userinfoCache } from 'utils/cache.ts';

userinfoCache.setItem({ id: 1, name: 'xxx' });

userinfoCache.getItem();
```

---
_Source: https://npm.io/package/front-end-cache · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
