# ag-cache

> 基于TypeScript实现，可应用于React，Angular，Vue，JavaScript等web环境的缓存模块。

Latest version **1.0.1** (published 2021-04-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install ag-cache
pnpm add ag-cache
yarn add ag-cache
bun add ag-cache
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2021-04-11 |
| First published | 2021-04-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 1.2 MB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Agility_Jin |
| Maintainers | agility_jin |
| Keywords | Cache, Frontend Cache, TypeScript, JavaScript Cache, React Cache, Vue Cache, Angular Cache |

## Links

- npm: https://www.npmjs.com/package/ag-cache
- Repository: https://github.com/AgilityJin/ag-cache
- npm.io page: https://npm.io/package/ag-cache

## Dependencies (3)

- [flatted](https://npm.io/package/flatted.md) ^3.1.1
- [crypto-js](https://npm.io/package/crypto-js.md) ^4.0.0
- [js-cookie](https://npm.io/package/js-cookie.md) ^2.2.1

## 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.1 (latest) — 2021-04-11
- 1.0.0 — 2021-04-11

## README

# ag-cache

基于TypeScript实现，可应用于React，Angular，Vue，JavaScript等web环境的缓存模块。更好的管理您的缓存信息，灵活的更换缓存所处的位置。

[Documentation in English](./README_en.md)

[更改日志](./CHANGELOG.md)

## 安装

yarn

`yarn add ag-cache`

or npm

`npm install ag-cache`

## 基础文档

[API文档](https://htmlpreview.github.io/?https://raw.githubusercontent.com/AgilityJin/ag-cache/main/docs/index.html)

## 实例化 CacheModule

```TypeScript
new Cache(CacheOptions)
```

### CacheOptions

属性 | 类型 | 默认值 | 描述
---|---|---|---
key | string | | 关联该cache的唯一值，例如可以是用户ID
defaultExpires | number | 200 | 毫秒单位，指定默认的缓存超时时间，0则不超时
defaultLevel | 'RAM'， 'COOKIE'， 'SESSION_STORAGE'， 'LOCAL_STORAGE' | 'RAM' | 默认的缓存级别
defaultCollectorInterval | number | 30000 | 垃圾回收的执行间隔，只回收超时缓存
maximum | CacheMaximum | 可选 | 设置各级缓存最大内存占用，0则为不限制
debug | boolean | false | 启用debug调试模式

### 实例方法

#### set 设置缓存

使用：`set(key, value, option): Promise<void>`

参数名 | 类型 | 默认值 | 描述
---|---|---|---
key | string | | 缓存对应的唯一key
value | CacheValue | | 缓存值
option | {level, expires} | 默认为CacheOptions所对应的defaultExpires及defaultLevel | 设置缓存的级别与超时时间

#### get 读取缓存

使用：`get<T = any>(key, level): T`

参数名 | 类型 | 默认值 | 描述
---|---|---|---
key | string | | 缓存对应的key
level | 'RAM'， 'COOKIE'， 'SESSION_STORAGE'， 'LOCAL_STORAGE' | 可选 | 如果没有指定则会从所有级别缓存内找到首个匹配项返回，如果指定则从指定级别缓存内查询

#### delete 移除指定key的缓存信息

使用：`delete(key, level): void`

参数名 | 类型 | 默认值 | 描述
---|---|---|---
key | string | | 缓存对应的key
level | 'RAM'， 'COOKIE'， 'SESSION_STORAGE'， 'LOCAL_STORAGE' | 可选 | 如果没有指定则会从所有级别缓存内移除首个匹配项，如果指定则从指定级别缓存内移除匹配项

#### clear 清除指定级别的缓存信息

使用: `clear(level: CacheLevel): void`

参数名 | 类型 | 默认值 | 描述
---|---|---|---
level | 'RAM'， 'COOKIE'， 'SESSION_STORAGE'， 'LOCAL_STORAGE' | | 清空指定级别缓存内的所有缓存信息

#### clearAll 清除所有缓存信息

使用: `clearAll(): void`

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