@hile/cache 2.0.2 → 2.0.4
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.
- package/README.md +22 -7
- package/dist/define.d.ts +1 -0
- package/dist/define.js +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +8 -9
- package/package.json +4 -6
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @hile/cache
|
|
2
2
|
|
|
3
|
-
基于 Redis
|
|
3
|
+
基于 Redis 的类型安全读穿透缓存层。依赖 `ioredis`,构造时注入已连接的 `Redis` 实例(可与 `@hile/ioredis` 配合使用)。
|
|
4
4
|
|
|
5
5
|
## 安装
|
|
6
6
|
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
pnpm add @hile/cache
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
运行时需提供 `ioredis` 的 `Redis` 实例;在 Hile 应用中通常通过 `@hile/ioredis` 的 `createRedis()` 或 `loadService(ioredisService)` 获取。
|
|
12
12
|
|
|
13
13
|
## 快速开始
|
|
14
14
|
|
|
@@ -29,9 +29,12 @@ const userCache = defineCache('user:{id:string}:info', async (params) => {
|
|
|
29
29
|
### 2. 使用缓存
|
|
30
30
|
|
|
31
31
|
```typescript
|
|
32
|
+
import { loadService } from '@hile/core';
|
|
33
|
+
import redisService from '@hile/ioredis';
|
|
32
34
|
import { RedisCache } from '@hile/cache';
|
|
33
35
|
|
|
34
|
-
const
|
|
36
|
+
const redis = await loadService(redisService);
|
|
37
|
+
const cache = new RedisCache('myapp:', redis); // 前缀 + Redis 实例
|
|
35
38
|
|
|
36
39
|
const { read, write, remove, has } = await cache.loadCache(userCache);
|
|
37
40
|
|
|
@@ -100,7 +103,7 @@ function defineCache<T extends string = string, R = any>(
|
|
|
100
103
|
|
|
101
104
|
```typescript
|
|
102
105
|
class RedisCache {
|
|
103
|
-
constructor(prefix: string);
|
|
106
|
+
constructor(prefix: string, redis: Redis);
|
|
104
107
|
|
|
105
108
|
loadCache<T extends string, R>(
|
|
106
109
|
target: DefineCacheResult<T, R>
|
|
@@ -135,7 +138,7 @@ myapp:user:u-001:info
|
|
|
135
138
|
|
|
136
139
|
## 与 @hile/cli 集成
|
|
137
140
|
|
|
138
|
-
|
|
141
|
+
在 `package.json` 中配置自动加载 Redis,在 boot 或服务工厂里注入客户端:
|
|
139
142
|
|
|
140
143
|
```json
|
|
141
144
|
{
|
|
@@ -145,15 +148,27 @@ myapp:user:u-001:info
|
|
|
145
148
|
}
|
|
146
149
|
```
|
|
147
150
|
|
|
148
|
-
|
|
151
|
+
```typescript
|
|
152
|
+
import { loadService } from '@hile/core';
|
|
153
|
+
import redisService from '@hile/ioredis';
|
|
154
|
+
import { defineCache, Cache, RedisCache } from '@hile/cache';
|
|
155
|
+
|
|
156
|
+
// 在 defineService 工厂内
|
|
157
|
+
const redis = await loadService(redisService);
|
|
158
|
+
const cache = new RedisCache('myapp:', redis);
|
|
159
|
+
```
|
|
149
160
|
|
|
150
161
|
---
|
|
151
162
|
|
|
152
163
|
## 完整示例
|
|
153
164
|
|
|
154
165
|
```typescript
|
|
166
|
+
import { loadService } from '@hile/core';
|
|
167
|
+
import redisService from '@hile/ioredis';
|
|
155
168
|
import { defineCache, Cache, RedisCache } from '@hile/cache';
|
|
156
169
|
|
|
170
|
+
const redis = await loadService(redisService);
|
|
171
|
+
|
|
157
172
|
// 定义多条缓存
|
|
158
173
|
const userCache = defineCache('user:{id:string}:info', async ({ id }) => {
|
|
159
174
|
const user = await db.query('SELECT * FROM users WHERE id = $1', [id]);
|
|
@@ -166,7 +181,7 @@ const postCache = defineCache('post:{id:string}:detail', async ({ id }) => {
|
|
|
166
181
|
return new Cache(post).setExpire(3600);
|
|
167
182
|
});
|
|
168
183
|
|
|
169
|
-
const cache = new RedisCache('myapp:');
|
|
184
|
+
const cache = new RedisCache('myapp:', redis);
|
|
170
185
|
|
|
171
186
|
// 批量加载
|
|
172
187
|
const [userOps, postOps] = await Promise.all([
|
package/dist/define.d.ts
CHANGED
package/dist/define.js
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { DefineCacheResult, ExtractParams } from './define.js';
|
|
2
|
+
import { Redis } from 'ioredis';
|
|
2
3
|
export * from './define.js';
|
|
3
4
|
export declare class RedisCache {
|
|
4
5
|
private readonly prefix;
|
|
6
|
+
private readonly redis;
|
|
5
7
|
private readonly _regexp;
|
|
6
|
-
constructor(prefix: string);
|
|
8
|
+
constructor(prefix: string, redis: Redis);
|
|
7
9
|
private makeKey;
|
|
8
10
|
private _write;
|
|
9
11
|
private _read;
|
package/dist/index.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import { loadService } from "@hile/core";
|
|
2
|
-
import ioredisService from "@hile/ioredis";
|
|
3
|
-
import { Cache } from './define.js';
|
|
4
1
|
export * from './define.js';
|
|
5
2
|
export class RedisCache {
|
|
6
3
|
prefix;
|
|
4
|
+
redis;
|
|
7
5
|
_regexp = /\{([^\:]+):[^\}]+\}/g;
|
|
8
|
-
constructor(prefix) {
|
|
6
|
+
constructor(prefix, redis) {
|
|
9
7
|
this.prefix = prefix;
|
|
8
|
+
this.redis = redis;
|
|
10
9
|
}
|
|
11
10
|
makeKey(key, options) {
|
|
12
11
|
return this.prefix + key.replace(this._regexp, (_, key) => String(options[key]));
|
|
@@ -14,10 +13,10 @@ export class RedisCache {
|
|
|
14
13
|
async _write(target, params) {
|
|
15
14
|
const key = this.makeKey(target.key, params);
|
|
16
15
|
const cache = await target.fn(params);
|
|
17
|
-
if (
|
|
16
|
+
if (cache.__$flag__ !== 'cache') {
|
|
18
17
|
throw new Error('Cache result must be an instance of Cache');
|
|
19
18
|
}
|
|
20
|
-
const redis =
|
|
19
|
+
const redis = this.redis;
|
|
21
20
|
const exists = await redis.exists(key);
|
|
22
21
|
if (cache.data === undefined) {
|
|
23
22
|
if (exists) {
|
|
@@ -36,7 +35,7 @@ export class RedisCache {
|
|
|
36
35
|
}
|
|
37
36
|
async _read(target, params) {
|
|
38
37
|
const key = this.makeKey(target.key, params);
|
|
39
|
-
const redis =
|
|
38
|
+
const redis = this.redis;
|
|
40
39
|
const exists = await redis.exists(key);
|
|
41
40
|
if (!exists)
|
|
42
41
|
return await this._write(target, params);
|
|
@@ -47,7 +46,7 @@ export class RedisCache {
|
|
|
47
46
|
}
|
|
48
47
|
async _remove(target, params) {
|
|
49
48
|
const key = this.makeKey(target.key, params);
|
|
50
|
-
const redis =
|
|
49
|
+
const redis = this.redis;
|
|
51
50
|
const exists = await redis.exists(key);
|
|
52
51
|
if (!exists)
|
|
53
52
|
return 0;
|
|
@@ -55,7 +54,7 @@ export class RedisCache {
|
|
|
55
54
|
}
|
|
56
55
|
async _has(target, params) {
|
|
57
56
|
const key = this.makeKey(target.key, params);
|
|
58
|
-
const redis =
|
|
57
|
+
const redis = this.redis;
|
|
59
58
|
const exists = await redis.exists(key);
|
|
60
59
|
return !!exists;
|
|
61
60
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hile/cache",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -10,8 +10,7 @@
|
|
|
10
10
|
},
|
|
11
11
|
"files": [
|
|
12
12
|
"dist",
|
|
13
|
-
"README.md"
|
|
14
|
-
"SKILL.md"
|
|
13
|
+
"README.md"
|
|
15
14
|
],
|
|
16
15
|
"license": "MIT",
|
|
17
16
|
"publishConfig": {
|
|
@@ -22,8 +21,7 @@
|
|
|
22
21
|
"vitest": "^4.0.18"
|
|
23
22
|
},
|
|
24
23
|
"dependencies": {
|
|
25
|
-
"
|
|
26
|
-
"@hile/ioredis": "^2.0.2"
|
|
24
|
+
"ioredis": "^5.11.0"
|
|
27
25
|
},
|
|
28
|
-
"gitHead": "
|
|
26
|
+
"gitHead": "2d8091212ef354336c03e346d6fd49b6b335a780"
|
|
29
27
|
}
|