@hile/cache 2.0.1 → 2.0.3

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # @hile/cache
2
2
 
3
- 基于 Redis 的类型安全读穿透缓存层,构建在 `@hile/core`(DI 容器)与 `@hile/ioredis`(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
- 依赖:`@hile/core`、`@hile/ioredis`。
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 cache = new RedisCache('myapp:'); // 统一前缀
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
- 可在 `package.json` 中配置自动加载 Redis
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
- 然后任何地方通过 `loadService(ioredisService)` 获取 Redis 客户端,`@hile/cache` 内部已自动处理。
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/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,12 @@
1
- import { loadService } from "@hile/core";
2
- import ioredisService from "@hile/ioredis";
3
1
  import { Cache } from './define.js';
4
2
  export * from './define.js';
5
3
  export class RedisCache {
6
4
  prefix;
5
+ redis;
7
6
  _regexp = /\{([^\:]+):[^\}]+\}/g;
8
- constructor(prefix) {
7
+ constructor(prefix, redis) {
9
8
  this.prefix = prefix;
9
+ this.redis = redis;
10
10
  }
11
11
  makeKey(key, options) {
12
12
  return this.prefix + key.replace(this._regexp, (_, key) => String(options[key]));
@@ -17,7 +17,7 @@ export class RedisCache {
17
17
  if (!(cache instanceof Cache)) {
18
18
  throw new Error('Cache result must be an instance of Cache');
19
19
  }
20
- const redis = await loadService(ioredisService);
20
+ const redis = this.redis;
21
21
  const exists = await redis.exists(key);
22
22
  if (cache.data === undefined) {
23
23
  if (exists) {
@@ -36,7 +36,7 @@ export class RedisCache {
36
36
  }
37
37
  async _read(target, params) {
38
38
  const key = this.makeKey(target.key, params);
39
- const redis = await loadService(ioredisService);
39
+ const redis = this.redis;
40
40
  const exists = await redis.exists(key);
41
41
  if (!exists)
42
42
  return await this._write(target, params);
@@ -47,7 +47,7 @@ export class RedisCache {
47
47
  }
48
48
  async _remove(target, params) {
49
49
  const key = this.makeKey(target.key, params);
50
- const redis = await loadService(ioredisService);
50
+ const redis = this.redis;
51
51
  const exists = await redis.exists(key);
52
52
  if (!exists)
53
53
  return 0;
@@ -55,7 +55,7 @@ export class RedisCache {
55
55
  }
56
56
  async _has(target, params) {
57
57
  const key = this.makeKey(target.key, params);
58
- const redis = await loadService(ioredisService);
58
+ const redis = this.redis;
59
59
  const exists = await redis.exists(key);
60
60
  return !!exists;
61
61
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hile/cache",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
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
- "@hile/core": "^2.0.1",
26
- "@hile/ioredis": "^2.0.1"
24
+ "ioredis": "^5.11.0"
27
25
  },
28
- "gitHead": "2c8011db01f2815e5ce34de964d5492640396828"
26
+ "gitHead": "9f8e7074872709eccd3cac128999f872521b061e"
29
27
  }