@celljs/cache 4.6.0 → 4.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.zh-cn.md +51 -0
  2. package/package.json +2 -2
@@ -0,0 +1,51 @@
1
+ # Cell - 缓存组件
2
+
3
+ Cell 后端的命名缓存管理器,基于 [cache-manager](https://github.com/jaredwray/cache-manager) 5。
4
+
5
+ ```bash
6
+ npm install @celljs/cache
7
+ ```
8
+
9
+ ```ts
10
+ import { Autowired, Component, Named } from '@celljs/core';
11
+ import { CacheManager } from '@celljs/cache';
12
+
13
+ @Component()
14
+ export class UserService {
15
+ @Autowired(CacheManager) // 名为 "default" 的缓存
16
+ protected readonly cache: CacheManager;
17
+
18
+ @Autowired(CacheManager) @Named('sessions')
19
+ protected readonly sessions: CacheManager;
20
+
21
+ getUser(id: string) {
22
+ return this.cache.wrap(`user:${id}`, () => this.load(id), 60_000); // 计算一次,保留 60 毫秒
23
+ }
24
+ }
25
+ ```
26
+
27
+ `CacheManager` 有 `get`、`set(key, value, ttl?)`、`del`、`reset` 和 `wrap(key, fn, ttl?)` 方法。首次注入时创建一个管理器。
28
+
29
+ ## 配置(`cache.config`)
30
+
31
+ 为每个缓存名称设置一个条目:
32
+
33
+ ```yaml
34
+ cache:
35
+ config:
36
+ default:
37
+ store: memory
38
+ max: 100 # 条目数
39
+ ttl: 3600000 # 毫秒
40
+ sessions:
41
+ store: redis-yet # 需要 cache-manager-redis-yet 包
42
+ url: "${env.REDIS_URL}"
43
+ ttl: 86400000
44
+ ```
45
+
46
+ - `ttl` 以**毫秒**为单位,与 cache-manager 5 一致。默认缓存保留条目 3600 毫秒。
47
+ - `store` 是 `memory` 或你安装的 store 包名:`redis`(`cache-manager-redis`)、`redis-store`、`redis-yet`、`ioredis`、`ioredis-yet`、`mongodb`、`mongoose`、`fs-binary`、`fs-hash`、`hazelcast`、`memcached-store`、`memory-store`(各有 `cache-manager-<name>` 包)。其他键是该 store 的选项。
48
+ - 条目列表组成多级缓存(`multiCaching`):读操作依次尝试每个 store。
49
+ - 注入未配置的名称会抛出 `Not found cache options.`
50
+
51
+ 绑定自己的 `CacheStoreFactory`(`rebind: true`)以另外的方式创建 store。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/cache",
3
- "version": "4.6.0",
3
+ "version": "4.7.0",
4
4
  "description": "cache component for cache-manager",
5
5
  "keywords": [
6
6
  "cell-component"
@@ -33,7 +33,7 @@
33
33
  "watch": "tsc -b -w --preserveWatchOutput"
34
34
  },
35
35
  "dependencies": {
36
- "@celljs/core": "4.6.0",
36
+ "@celljs/core": "4.7.0",
37
37
  "cache-manager": "^5.2.3"
38
38
  },
39
39
  "devDependencies": {