@aomex/cache 0.0.7 → 2.0.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.
package/CHANGELOG.md CHANGED
@@ -1,29 +1,60 @@
1
- # @aomex/cache
1
+ # Change Log
2
2
 
3
- ## 0.0.7
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
4
5
 
5
- ### Patch Changes
6
+ # [2.0.0](https://github.com/aomex/aomex/compare/v1.7.0...v2.0.0) (2024-07-26)
6
7
 
7
- - [`bf2c963`](https://github.com/aomex/aomex/commit/bf2c9639a609402be2ff094ceabe4fd2231214f8) Thanks [@geekact](https://github.com/geekact)! - fix(cache): forget to return while calling addValue
8
8
 
9
- - [`e3b1be4`](https://github.com/aomex/aomex/commit/e3b1be4d02793001d441a1f1b1ced060e95b4f8a) Thanks [@geekact](https://github.com/geekact)! - fix(cache): addValue may be incorrect with multiple threads
9
+ ### Features
10
10
 
11
- ## 0.0.6
11
+ * **cache:** 新的缓存库 ([cca0b52](https://github.com/aomex/aomex/commit/cca0b52d86029fb1303cd9bd1f362d2dd8b406b6))
12
12
 
13
- ### Patch Changes
14
13
 
15
- - [`827b86d`](https://github.com/aomex/aomex/commit/827b86d692786b5708c8602e5985eedabf632981) Thanks [@geekact](https://github.com/geekact)! - feat(cache): change lru as public field
14
+ ### BREAKING CHANGES
16
15
 
17
- - [`cc01922`](https://github.com/aomex/aomex/commit/cc019228eb3dca43e354467d60c744103424e47b) Thanks [@geekact](https://github.com/geekact)! - chore(cache): upgrade lru-cache from 7.17.0 to 8.0.0
16
+ * **cache:** 1. 删除了memory-cache,redis-cache,file-cache
17
+ 2. 采用适配器形式创建缓存实例
18
18
 
19
- ## 0.0.5
20
19
 
21
- ### Patch Changes
22
20
 
23
- - [`9c0bb28`](https://github.com/aomex/aomex/commit/9c0bb285d25594310e5450634223100a01bf1b5d) Thanks [@geekact](https://github.com/geekact)! - feat(cache): getOrSet always returning fresh value from api
24
21
 
25
- ## 0.0.3
26
22
 
27
- ### Patch Changes
23
+ # [1.7.0](https://github.com/aomex/aomex/compare/v1.6.0...v1.7.0) (2024-07-13)
28
24
 
29
- - [`ceb2d82`](https://github.com/aomex/aomex/commit/ceb2d8269755dca3f79b755224259b8a99bc65d0) Thanks [@geekact](https://github.com/geekact)! - chore(cache,web): upgrade lru-cache from 7.14.1 to 7.17.0
25
+ **Note:** Version bump only for package @aomex/internal-cache
26
+
27
+
28
+
29
+
30
+
31
+ # [1.6.0](https://github.com/aomex/aomex/compare/v1.5.1...v1.6.0) (2024-07-05)
32
+
33
+ **Note:** Version bump only for package @aomex/internal-cache
34
+
35
+
36
+
37
+
38
+
39
+ ## [1.5.1](https://github.com/aomex/aomex/compare/v1.5.0...v1.5.1) (2024-07-04)
40
+
41
+ **Note:** Version bump only for package @aomex/internal-cache
42
+
43
+
44
+
45
+
46
+
47
+ # [1.3.0](https://github.com/aomex/aomex/compare/v1.2.0...v1.3.0) (2024-06-28)
48
+
49
+ **Note:** Version bump only for package @aomex/internal-cache
50
+
51
+
52
+
53
+
54
+
55
+ # 1.1.0 (2024-06-27)
56
+
57
+
58
+ ### Features
59
+
60
+ * 初始化 ([3350159](https://github.com/aomex/aomex/commit/3350159454ad230e6d910405f907293b059b1f49))
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2023 geekact
3
+ Copyright (c) 2023-2024 geekact
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/dist/index.d.ts CHANGED
@@ -1,90 +1,148 @@
1
- import LRU from 'lru-cache';
1
+ import { I18nMessage } from '@aomex/core';
2
2
 
3
- interface CacheOptions {
4
- keyPrefix?: string;
3
+ declare module '@aomex/core' {
4
+ namespace I18n {
5
+ interface Definition {
6
+ cache: {
7
+ wrong_type: I18nMessage;
8
+ not_integer: I18nMessage;
9
+ };
10
+ }
11
+ }
5
12
  }
6
- declare abstract class Cache {
7
- protected readonly keyPrefix: string;
8
- constructor(config: CacheOptions);
13
+
14
+ declare abstract class Store {
15
+ connect(): Promise<void>;
16
+ gc(): Promise<void>;
17
+ abstract existsKey(key: string): Promise<boolean>;
18
+ abstract getValue(key: string): Promise<string | null>;
19
+ abstract setValue(key: string, value: string, durationMS?: number): Promise<boolean>;
20
+ abstract setNotExistValue(key: string, value: string, durationMS?: number): Promise<boolean>;
21
+ abstract deleteValue(key: string): Promise<boolean>;
22
+ abstract deleteAllValues(): Promise<boolean>;
23
+ abstract increaseValue(key: string): Promise<number>;
24
+ abstract decreaseValue(key: string): Promise<number>;
25
+ abstract expireKey(key: string, durationMS: number): Promise<boolean>;
26
+ abstract ttlKey(key: string): Promise<number>;
27
+ abstract leftPushValue(key: string, ...values: string[]): Promise<boolean>;
28
+ abstract rightPopValue(key: string): Promise<string | null>;
29
+ }
30
+
31
+ declare namespace Caching {
32
+ interface Options {
33
+ /**
34
+ * 删除过期缓存防止占用空间,加快读取速度。概率取值范围:`1-100`。默认值:`10`
35
+ */
36
+ gcProbability?: number;
37
+ }
38
+ type Types = string | number | object | boolean;
39
+ }
40
+ declare class Caching<S extends Store = Store, T extends object = object> {
41
+ protected readonly gcProbability: number;
42
+ readonly store: S;
43
+ constructor(StoreClass: new (config: T) => S, config: T & Caching.Options);
9
44
  /**
10
- * Make sure key is not expired.
45
+ * 查看缓存是否存在
11
46
  */
12
47
  exists(key: string): Promise<boolean>;
13
48
  /**
14
- * Get value from specific key and return `null` if defaultValue is not provided.
15
- * ```typescript
16
- * await cache.get('non-exist'); // null
17
- * await cache.get('non-exist', 'my-value'); // my-value
18
- * ```
19
- */
20
- get<T>(key: string, defaultValue: T): Promise<T>;
21
- get<T extends string | number | object | boolean>(key: string): Promise<T | null>;
22
- /**
23
- * Get value from key, a set callback will be triggered when the value is `null`.
24
49
  *
50
+ * 获取缓存。如果未找到缓存并且未提供默认值,则返回`null`
25
51
  * ```typescript
26
52
  * await cache.get('key'); // null
27
- * await cache.getOrSet('key', () => 'value'); // 'value'
28
- * await cache.get('key'); // 'value'
53
+ * await cache.get('key', 'my-value'); // my-value
29
54
  * ```
30
55
  */
31
- getOrSet<T extends string | number | object | boolean>(key: string, orSet: () => T, duration?: number): Promise<T>;
32
- getOrSet<T extends string | number | object | boolean>(key: string, orSet: () => Promise<T>, duration?: number): Promise<T>;
56
+ get<T>(key: string, defaultValue: T): Promise<T>;
57
+ get<T extends Caching.Types>(key: string): Promise<T | null>;
33
58
  /**
34
- * Set value
59
+ * 设置缓存。可以指定过期时间(毫秒)
35
60
  *
36
61
  * ```typescript
37
62
  * await cache.set('key', 'value');
38
63
  * await cache.set('key', 'value', 3600);
39
64
  * ```
40
65
  */
41
- set(key: string, value: string | number | object | boolean, duration?: number): Promise<boolean>;
66
+ set(key: string, value: Caching.Types, durationMs?: number): Promise<boolean>;
42
67
  /**
43
- * Set value if a key doesn't exists
68
+ * 设置缓存。如果缓存已经存在,则设置失败,返回`false`
44
69
  *
45
70
  * ```typescript
46
71
  * await cache.exists('key'); // false
47
- * await cache.add('key', 'value'); // true
72
+ * await cache.setNX('key', 'value'); // true
48
73
  *
49
74
  * await cache.exists('key'); // true
50
- * await cache.add('key', 'value'); // false
75
+ * await cache.setNX('key', 'value'); // false
51
76
  * ```
52
77
  */
53
- add(key: string, value: any, duration?: number): Promise<boolean>;
78
+ setNX(key: string, value: Caching.Types, durationMs?: number): Promise<boolean>;
79
+ /**
80
+ * 将一个或多个值按顺序插入到列表头部。当 key 存在但不是列表类型时,返回一个错误。
81
+ *
82
+ * 假设当前一个列表 foo=[s],执行 `leftPush('foo', a, b, c)` 后列表 foo=[a, b, c, s]
83
+ */
84
+ leftPush(key: string, ...values: Caching.Types[]): Promise<boolean>;
85
+ /**
86
+ * 移除列表的最后一个元素,返回值为被移除的元素
87
+ */
88
+ rightPop<T extends Caching.Types>(key: string): Promise<T | null>;
54
89
  /**
55
- * Delete a key
90
+ * 将key中储存的数字值增一。
91
+ *
92
+ * 如果key不存在,那么key的值会先被初始化为0,然后再执行操作。
93
+ *
94
+ * 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。
95
+ */
96
+ increment(key: string): Promise<number>;
97
+ /**
98
+ * 重新设置缓存时间。如果key不存在,则返回`false`
99
+ */
100
+ expire(key: string, durationMs: number): Promise<boolean>;
101
+ /**
102
+ * 检测缓存的剩余生存时间(Time to live)
103
+ * - 不存在则返回`-2`
104
+ * - 未设置过期时间则返回`-1`
105
+ * - 已设置过期时间则返回剩余时间,单位**毫秒**
106
+ */
107
+ ttl(key: string): Promise<number>;
108
+ /**
109
+ * 将key中储存的数字值减一。
110
+ *
111
+ * 如果key不存在,那么key的值会先被初始化为0,然后再执行操作。
112
+ *
113
+ * 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。
114
+ */
115
+ decrement(key: string): Promise<number>;
116
+ /**
117
+ * 删除指定缓存
56
118
  */
57
119
  delete(key: string): Promise<boolean>;
58
120
  /**
59
- * Delete all keys
121
+ * 删除所有缓存。若指定了keyPrefix,则只删除有该前缀的缓存
60
122
  */
61
123
  deleteAll(): Promise<boolean>;
62
124
  protected buildKey(key: string): string;
63
- protected addValue(key: string, value: string, duration?: number): Promise<boolean>;
64
- protected abstract existsKey(key: string): Promise<boolean>;
65
- protected abstract getValue(key: string): Promise<string | null>;
66
- protected abstract setValue(key: string, value: string, duration?: number): Promise<boolean>;
67
- protected abstract deleteValue(key: string): Promise<boolean>;
68
- protected abstract deleteAllValues(): Promise<boolean>;
125
+ protected parseValue(value: string | null, defaultValue?: Caching.Types): any;
126
+ protected applyGC(): Promise<void>;
69
127
  }
70
128
 
71
- interface MemoryCacheOptions extends CacheOptions {
72
- /**
73
- * Maximum key length, defaults `1000`
74
- */
75
- maxItems?: number;
76
- gcProbability?: number;
77
- }
78
- declare class MemoryCache extends Cache {
79
- readonly lru: LRU<string, string>;
80
- protected readonly gcProbability: number;
81
- constructor(options?: MemoryCacheOptions);
82
- protected existsKey(key: string): Promise<boolean>;
83
- protected getValue(key: string): Promise<string | null>;
84
- protected setValue(key: string, value: string, duration?: number): Promise<boolean>;
85
- protected deleteValue(key: string): Promise<boolean>;
86
- protected deleteAllValues(): Promise<boolean>;
87
- protected gc(): void;
129
+ declare class CacheMemoryStore extends Store {
130
+ private data;
131
+ existsKey(key: string): Promise<boolean>;
132
+ getValue(key: string): Promise<string | null>;
133
+ setValue(key: string, value: string, duration?: number): Promise<boolean>;
134
+ setNotExistValue(key: string, value: string, duration?: number): Promise<boolean>;
135
+ leftPushValue(key: string, ...values: string[]): Promise<boolean>;
136
+ rightPopValue(key: string): Promise<string | null>;
137
+ increaseValue(key: string): Promise<number>;
138
+ decreaseValue(key: string): Promise<number>;
139
+ deleteValue(key: string): Promise<boolean>;
140
+ deleteAllValues(): Promise<boolean>;
141
+ expireKey(key: string, duration: number): Promise<boolean>;
142
+ ttlKey(key: string): Promise<number>;
143
+ protected getValueSync<T extends 'string' | 'array' | 'any'>(key: string, assertType: T): (T extends 'any' ? string | string[] : T extends 'string' ? string : Array<string>) | null;
144
+ protected setValueSync(key: string, value: string | string[], duration?: number): boolean;
145
+ gc(): Promise<void>;
88
146
  }
89
147
 
90
- export { Cache, CacheOptions, MemoryCache, MemoryCacheOptions };
148
+ export { CacheMemoryStore, Caching, Store };
package/dist/index.js CHANGED
@@ -1,129 +1,308 @@
1
- // src/cache.ts
1
+ // src/i18n/locales/en-us.ts
2
+ import { i18n } from "@aomex/core";
3
+ i18n.register("en_US", "cache", {
4
+ wrong_type: "wrong kind of value",
5
+ not_integer: "value is not an integer"
6
+ });
7
+
8
+ // src/i18n/locales/zh-cn.ts
9
+ import { i18n as i18n2 } from "@aomex/core";
10
+ i18n2.register("zh_CN", "cache", {
11
+ wrong_type: "\u9519\u8BEF\u7684\u503C\u7C7B\u578B",
12
+ not_integer: "\u503C\u4E0D\u662F\u6574\u6570\u7C7B\u578B"
13
+ });
14
+
15
+ // src/caching.ts
2
16
  import { createHash } from "node:crypto";
3
- var Cache = class {
4
- keyPrefix;
5
- constructor(config) {
6
- this.keyPrefix = config.keyPrefix ?? "";
17
+
18
+ // src/store.ts
19
+ var Store = class {
20
+ async connect() {
21
+ }
22
+ async gc() {
23
+ }
24
+ };
25
+
26
+ // src/caching.ts
27
+ var Caching = class {
28
+ gcProbability;
29
+ store;
30
+ constructor(StoreClass, config) {
31
+ this.gcProbability = (config.gcProbability ?? 10) / 100;
32
+ this.store = new StoreClass(config);
33
+ this.store.connect();
7
34
  }
8
35
  /**
9
- * Make sure key is not expired.
36
+ * 查看缓存是否存在
10
37
  */
11
38
  async exists(key) {
12
- return this.existsKey(this.buildKey(key));
39
+ await this.store.connect();
40
+ return this.store.existsKey(this.buildKey(key));
13
41
  }
14
42
  async get(key, defaultValue) {
43
+ await this.store.connect();
15
44
  const hashKey = this.buildKey(key);
16
- const result = await this.getValue(hashKey);
17
- if (result === null) {
18
- return defaultValue === void 0 ? null : defaultValue;
19
- }
20
- try {
21
- return JSON.parse(result);
22
- } catch {
23
- return null;
24
- }
25
- }
26
- async getOrSet(key, orSet, duration) {
27
- let value = await this.get(key);
28
- if (value !== null)
29
- return value;
30
- await this.set(key, await orSet(), duration);
31
- return await this.get(key);
45
+ const result = await this.store.getValue(hashKey);
46
+ return this.parseValue(result, defaultValue);
32
47
  }
33
48
  /**
34
- * Set value
49
+ * 设置缓存。可以指定过期时间(毫秒)
35
50
  *
36
51
  * ```typescript
37
52
  * await cache.set('key', 'value');
38
53
  * await cache.set('key', 'value', 3600);
39
54
  * ```
40
55
  */
41
- async set(key, value, duration) {
56
+ async set(key, value, durationMs) {
57
+ await this.store.connect();
58
+ await this.applyGC();
42
59
  const hashKey = this.buildKey(key);
43
- return this.setValue(hashKey, JSON.stringify(value), duration);
60
+ return this.store.setValue(hashKey, JSON.stringify(value), durationMs);
44
61
  }
45
62
  /**
46
- * Set value if a key doesn't exists
63
+ * 设置缓存。如果缓存已经存在,则设置失败,返回`false`
47
64
  *
48
65
  * ```typescript
49
66
  * await cache.exists('key'); // false
50
- * await cache.add('key', 'value'); // true
67
+ * await cache.setNX('key', 'value'); // true
51
68
  *
52
69
  * await cache.exists('key'); // true
53
- * await cache.add('key', 'value'); // false
70
+ * await cache.setNX('key', 'value'); // false
54
71
  * ```
55
72
  */
56
- async add(key, value, duration) {
73
+ async setNX(key, value, durationMs) {
74
+ await this.store.connect();
75
+ await this.applyGC();
76
+ const hashKey = this.buildKey(key);
77
+ return this.store.setNotExistValue(hashKey, JSON.stringify(value), durationMs);
78
+ }
79
+ /**
80
+ * 将一个或多个值按顺序插入到列表头部。当 key 存在但不是列表类型时,返回一个错误。
81
+ *
82
+ * 假设当前一个列表 foo=[s],执行 `leftPush('foo', a, b, c)` 后列表 foo=[a, b, c, s]
83
+ */
84
+ async leftPush(key, ...values) {
85
+ if (!values.length) return false;
86
+ await this.store.connect();
87
+ const hashKey = this.buildKey(key);
88
+ return this.store.leftPushValue(
89
+ hashKey,
90
+ ...values.map((value) => JSON.stringify(value))
91
+ );
92
+ }
93
+ /**
94
+ * 移除列表的最后一个元素,返回值为被移除的元素
95
+ */
96
+ async rightPop(key) {
97
+ await this.store.connect();
57
98
  const hashKey = this.buildKey(key);
58
- return this.addValue(hashKey, JSON.stringify(value), duration);
99
+ const result = await this.store.rightPopValue(hashKey);
100
+ return this.parseValue(result);
59
101
  }
60
102
  /**
61
- * Delete a key
103
+ * 将key中储存的数字值增一。
104
+ *
105
+ * 如果key不存在,那么key的值会先被初始化为0,然后再执行操作。
106
+ *
107
+ * 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。
108
+ */
109
+ async increment(key) {
110
+ await this.store.connect();
111
+ const hashKey = this.buildKey(key);
112
+ return this.store.increaseValue(hashKey);
113
+ }
114
+ /**
115
+ * 重新设置缓存时间。如果key不存在,则返回`false`
116
+ */
117
+ async expire(key, durationMs) {
118
+ await this.store.connect();
119
+ const hashKey = this.buildKey(key);
120
+ return this.store.expireKey(hashKey, durationMs);
121
+ }
122
+ /**
123
+ * 检测缓存的剩余生存时间(Time to live)
124
+ * - 不存在则返回`-2`
125
+ * - 未设置过期时间则返回`-1`
126
+ * - 已设置过期时间则返回剩余时间,单位**毫秒**
127
+ */
128
+ async ttl(key) {
129
+ await this.store.connect();
130
+ const hashKey = this.buildKey(key);
131
+ return this.store.ttlKey(hashKey);
132
+ }
133
+ /**
134
+ * 将key中储存的数字值减一。
135
+ *
136
+ * 如果key不存在,那么key的值会先被初始化为0,然后再执行操作。
137
+ *
138
+ * 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。
139
+ */
140
+ async decrement(key) {
141
+ await this.store.connect();
142
+ const hashKey = this.buildKey(key);
143
+ return this.store.decreaseValue(hashKey);
144
+ }
145
+ /**
146
+ * 删除指定缓存
62
147
  */
63
148
  async delete(key) {
149
+ await this.store.connect();
64
150
  const hashKey = this.buildKey(key);
65
- return this.deleteValue(hashKey);
151
+ return this.store.deleteValue(hashKey);
66
152
  }
67
153
  /**
68
- * Delete all keys
154
+ * 删除所有缓存。若指定了keyPrefix,则只删除有该前缀的缓存
69
155
  */
70
156
  async deleteAll() {
71
- return this.deleteAllValues();
157
+ await this.store.connect();
158
+ return this.store.deleteAllValues();
72
159
  }
73
160
  buildKey(key) {
74
- const hashKey = key.length <= 32 ? key : createHash("md5").update(key).digest("hex");
75
- return this.keyPrefix + hashKey;
161
+ return key.length < 32 ? key : createHash("md5").update(key).digest("hex");
76
162
  }
77
- async addValue(key, value, duration) {
78
- if (await this.existsKey(key))
79
- return false;
80
- if (!await this.setValue(key, value, duration))
81
- return false;
82
- return await this.getValue(key) === value;
163
+ parseValue(value, defaultValue) {
164
+ if (value === null) {
165
+ return defaultValue === void 0 ? null : defaultValue;
166
+ }
167
+ try {
168
+ return JSON.parse(value);
169
+ } catch {
170
+ return null;
171
+ }
172
+ }
173
+ async applyGC() {
174
+ if (Math.random() > this.gcProbability) return;
175
+ await this.store.gc();
83
176
  }
84
177
  };
85
178
 
86
- // src/memory-cache.ts
87
- import LRU from "lru-cache";
88
- var MemoryCache = class extends Cache {
89
- lru;
90
- gcProbability;
91
- constructor(options = {}) {
92
- super(options);
93
- this.gcProbability = (options.gcProbability ?? 10) / 100;
94
- this.lru = new LRU({
95
- max: options.maxItems || 1e3
96
- });
97
- }
179
+ // src/cache-memory-store.ts
180
+ import { i18n as i18n3 } from "@aomex/core";
181
+ var CacheMemoryStore = class extends Store {
182
+ data = /* @__PURE__ */ new Map();
98
183
  async existsKey(key) {
99
- return this.lru.has(key);
184
+ return this.getValueSync(key, "any") !== null;
100
185
  }
101
186
  async getValue(key) {
102
- const data = this.lru.get(key);
103
- return data === void 0 ? null : data;
187
+ const value = this.getValueSync(key, "string");
188
+ return value;
104
189
  }
105
190
  async setValue(key, value, duration) {
106
- this.gc();
107
- this.lru.set(key, value, {
108
- ttl: duration
109
- });
191
+ return this.setValueSync(key, value, duration);
192
+ }
193
+ async setNotExistValue(key, value, duration) {
194
+ const currentValue = this.getValueSync(key, "string");
195
+ if (currentValue !== null) return false;
196
+ return this.setValueSync(key, value, duration);
197
+ }
198
+ async leftPushValue(key, ...values) {
199
+ let result = this.getValueSync(key, "array");
200
+ if (result === null) result = [];
201
+ result.push(...values);
202
+ this.setValueSync(key, result);
110
203
  return true;
111
204
  }
205
+ async rightPopValue(key) {
206
+ let result = this.getValueSync(key, "array");
207
+ if (result === null || !result.length) return null;
208
+ const popValue = result.pop();
209
+ if (result.length) {
210
+ this.setValueSync(key, result);
211
+ } else {
212
+ this.data.delete(key);
213
+ }
214
+ return popValue;
215
+ }
216
+ async increaseValue(key) {
217
+ const result = this.getValueSync(key, "string");
218
+ let value;
219
+ if (result === null) {
220
+ this.data.set(key, { value: "0" });
221
+ value = 0;
222
+ } else {
223
+ value = Number(result);
224
+ }
225
+ if (Number.isNaN(value) || !Number.isInteger(value)) {
226
+ throw new Error("\u7F13\u5B58\u503C\u4E0D\u662F\u6574\u6570\u7C7B\u578B");
227
+ }
228
+ this.setValueSync(key, String(++value));
229
+ return value;
230
+ }
231
+ async decreaseValue(key) {
232
+ const result = this.getValueSync(key, "string");
233
+ let value;
234
+ if (result === null) {
235
+ this.data.set(key, { value: "0" });
236
+ value = 0;
237
+ } else {
238
+ value = Number(result);
239
+ }
240
+ if (Number.isNaN(value) || !Number.isInteger(value)) {
241
+ throw new Error(i18n3.t("cache.not_integer"));
242
+ }
243
+ this.setValueSync(key, String(--value));
244
+ return value;
245
+ }
112
246
  async deleteValue(key) {
113
- return this.lru.delete(key);
247
+ this.data.delete(key);
248
+ return true;
114
249
  }
115
250
  async deleteAllValues() {
116
- this.lru.clear();
251
+ this.data.clear();
252
+ return true;
253
+ }
254
+ async expireKey(key, duration) {
255
+ const result = this.data.get(key);
256
+ if (!result) return false;
257
+ if (result.expires && result.expires < Date.now()) return false;
258
+ result.expires = Date.now() + duration;
259
+ return true;
260
+ }
261
+ async ttlKey(key) {
262
+ const result = this.data.get(key);
263
+ if (!result) return -2;
264
+ if (!result.expires) return -1;
265
+ if (result.expires < Date.now()) return -2;
266
+ return result.expires - Date.now();
267
+ }
268
+ getValueSync(key, assertType) {
269
+ const result = this.data.get(key);
270
+ if (!result) return null;
271
+ if (result.expires && result.expires < Date.now()) return null;
272
+ const value = result.value;
273
+ let isValid = false;
274
+ switch (assertType) {
275
+ case "string":
276
+ isValid = typeof value === "string";
277
+ break;
278
+ case "array":
279
+ isValid = Array.isArray(value);
280
+ break;
281
+ case "any":
282
+ isValid = true;
283
+ break;
284
+ }
285
+ if (!isValid) {
286
+ throw new Error(i18n3.t("cache.wrong_type"));
287
+ }
288
+ return value;
289
+ }
290
+ setValueSync(key, value, duration) {
291
+ this.data.set(key, { value, expires: duration ? Date.now() + duration : void 0 });
117
292
  return true;
118
293
  }
119
- gc() {
120
- if (Math.random() > this.gcProbability)
121
- return;
122
- this.lru.purgeStale();
294
+ async gc() {
295
+ const now = Date.now();
296
+ this.data = new Map(
297
+ Array.from(this.data.entries()).filter(
298
+ ([_, item]) => !item.expires || item.expires > now
299
+ )
300
+ );
123
301
  }
124
302
  };
125
303
  export {
126
- Cache,
127
- MemoryCache
304
+ CacheMemoryStore,
305
+ Caching,
306
+ Store
128
307
  };
129
308
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/cache.ts","../src/memory-cache.ts"],"sourcesContent":["import { createHash } from 'node:crypto';\n\nexport interface CacheOptions {\n keyPrefix?: string;\n}\n\nexport abstract class Cache {\n protected readonly keyPrefix: string;\n\n constructor(config: CacheOptions) {\n this.keyPrefix = config.keyPrefix ?? '';\n }\n\n /**\n * Make sure key is not expired.\n */\n async exists(key: string): Promise<boolean> {\n return this.existsKey(this.buildKey(key));\n }\n\n /**\n * Get value from specific key and return `null` if defaultValue is not provided.\n * ```typescript\n * await cache.get('non-exist'); // null\n * await cache.get('non-exist', 'my-value'); // my-value\n * ```\n */\n async get<T>(key: string, defaultValue: T): Promise<T>;\n async get<T extends string | number | object | boolean>(\n key: string,\n ): Promise<T | null>;\n async get(\n key: string,\n defaultValue?: string | number | object | boolean,\n ): Promise<any> {\n const hashKey = this.buildKey(key);\n const result = await this.getValue(hashKey);\n\n if (result === null) {\n return defaultValue === void 0 ? null : defaultValue;\n }\n\n try {\n return JSON.parse(result);\n } catch {\n return null;\n }\n }\n\n /**\n * Get value from key, a set callback will be triggered when the value is `null`.\n *\n * ```typescript\n * await cache.get('key'); // null\n * await cache.getOrSet('key', () => 'value'); // 'value'\n * await cache.get('key'); // 'value'\n * ```\n */\n async getOrSet<T extends string | number | object | boolean>(\n key: string,\n orSet: () => T,\n duration?: number,\n ): Promise<T>;\n async getOrSet<T extends string | number | object | boolean>(\n key: string,\n orSet: () => Promise<T>,\n duration?: number,\n ): Promise<T>;\n async getOrSet<T extends string | number | object | boolean>(\n key: string,\n orSet: () => T | Promise<T>,\n duration?: number,\n ): Promise<T> {\n let value: T | null = await this.get(key);\n if (value !== null) return value;\n await this.set(key, await orSet(), duration);\n return (await this.get<T>(key))!;\n }\n\n /**\n * Set value\n *\n * ```typescript\n * await cache.set('key', 'value');\n * await cache.set('key', 'value', 3600);\n * ```\n */\n async set(\n key: string,\n value: string | number | object | boolean,\n duration?: number,\n ): Promise<boolean> {\n const hashKey = this.buildKey(key);\n return this.setValue(hashKey, JSON.stringify(value), duration);\n }\n\n /**\n * Set value if a key doesn't exists\n *\n * ```typescript\n * await cache.exists('key'); // false\n * await cache.add('key', 'value'); // true\n *\n * await cache.exists('key'); // true\n * await cache.add('key', 'value'); // false\n * ```\n */\n async add(key: string, value: any, duration?: number): Promise<boolean> {\n const hashKey = this.buildKey(key);\n return this.addValue(hashKey, JSON.stringify(value), duration);\n }\n\n /**\n * Delete a key\n */\n async delete(key: string): Promise<boolean> {\n const hashKey = this.buildKey(key);\n return this.deleteValue(hashKey);\n }\n\n /**\n * Delete all keys\n */\n async deleteAll(): Promise<boolean> {\n return this.deleteAllValues();\n }\n\n protected buildKey(key: string): string {\n const hashKey =\n key.length <= 32 ? key : createHash('md5').update(key).digest('hex');\n return this.keyPrefix + hashKey;\n }\n\n protected async addValue(\n key: string,\n value: string,\n duration?: number,\n ): Promise<boolean> {\n if (await this.existsKey(key)) return false;\n if (!(await this.setValue(key, value, duration))) return false;\n return (await this.getValue(key)) === value;\n }\n\n protected abstract existsKey(key: string): Promise<boolean>;\n protected abstract getValue(key: string): Promise<string | null>;\n protected abstract setValue(\n key: string,\n value: string,\n duration?: number,\n ): Promise<boolean>;\n protected abstract deleteValue(key: string): Promise<boolean>;\n protected abstract deleteAllValues(): Promise<boolean>;\n}\n","import LRU from 'lru-cache';\nimport { Cache, type CacheOptions } from './cache';\n\nexport interface MemoryCacheOptions extends CacheOptions {\n /**\n * Maximum key length, defaults `1000`\n */\n maxItems?: number;\n gcProbability?: number;\n}\n\nexport class MemoryCache extends Cache {\n public readonly lru: LRU<string, string>;\n protected readonly gcProbability: number;\n\n constructor(options: MemoryCacheOptions = {}) {\n super(options);\n this.gcProbability = (options.gcProbability ?? 10) / 100;\n this.lru = new LRU<string, string>({\n max: options.maxItems || 1000,\n });\n }\n\n protected override async existsKey(key: string): Promise<boolean> {\n return this.lru.has(key);\n }\n\n protected async getValue(key: string): Promise<string | null> {\n const data = this.lru.get(key);\n return data === undefined ? null : data;\n }\n\n protected async setValue(\n key: string,\n value: string,\n duration?: number,\n ): Promise<boolean> {\n this.gc();\n this.lru.set(key, value, {\n ttl: duration,\n });\n return true;\n }\n\n protected async deleteValue(key: string): Promise<boolean> {\n return this.lru.delete(key);\n }\n\n protected async deleteAllValues(): Promise<boolean> {\n this.lru.clear();\n return true;\n }\n\n protected gc() {\n if (Math.random() > this.gcProbability) return;\n this.lru.purgeStale();\n }\n}\n"],"mappings":";AAAA,SAAS,kBAAkB;AAMpB,IAAe,QAAf,MAAqB;AAAA,EACP;AAAA,EAEnB,YAAY,QAAsB;AAChC,SAAK,YAAY,OAAO,aAAa;AAAA,EACvC;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,OAAO,KAA+B;AAC1C,WAAO,KAAK,UAAU,KAAK,SAAS,GAAG,CAAC;AAAA,EAC1C;AAAA,EAaA,MAAM,IACJ,KACA,cACc;AACd,UAAM,UAAU,KAAK,SAAS,GAAG;AACjC,UAAM,SAAS,MAAM,KAAK,SAAS,OAAO;AAE1C,QAAI,WAAW,MAAM;AACnB,aAAO,iBAAiB,SAAS,OAAO;AAAA,IAC1C;AAEA,QAAI;AACF,aAAO,KAAK,MAAM,MAAM;AAAA,IAC1B,QAAE;AACA,aAAO;AAAA,IACT;AAAA,EACF;AAAA,EAqBA,MAAM,SACJ,KACA,OACA,UACY;AACZ,QAAI,QAAkB,MAAM,KAAK,IAAI,GAAG;AACxC,QAAI,UAAU;AAAM,aAAO;AAC3B,UAAM,KAAK,IAAI,KAAK,MAAM,MAAM,GAAG,QAAQ;AAC3C,WAAQ,MAAM,KAAK,IAAO,GAAG;AAAA,EAC/B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA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A,EAEA,MAAyB,UAAU,KAA+B;AAChE,WAAO,KAAK,IAAI,IAAI,GAAG;AAAA,EACzB;AAAA,EAEA,MAAgB,SAAS,KAAqC;AAC5D,UAAM,OAAO,KAAK,IAAI,IAAI,GAAG;AAC7B,WAAO,SAAS,SAAY,OAAO;AAAA,EACrC;AAAA,EAEA,MAAgB,SACd,KACA,OACA,UACkB;AAClB,SAAK,GAAG;AACR,SAAK,IAAI,IAAI,KAAK,OAAO;AAAA,MACvB,KAAK;AAAA,IACP,CAAC;AACD,WAAO;AAAA,EACT;AAAA,EAEA,MAAgB,YAAY,KAA+B;AACzD,WAAO,KAAK,IAAI,OAAO,GAAG;AAAA,EAC5B;AAAA,EAEA,MAAgB,kBAAoC;AAClD,SAAK,IAAI,MAAM;AACf,WAAO;AAAA,EACT;AAAA,EAEU,KAAK;AACb,QAAI,KAAK,OAAO,IAAI,KAAK;AAAe;AACxC,SAAK,IAAI,WAAW;AAAA,EACtB;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/i18n/locales/en-us.ts","../src/i18n/locales/zh-cn.ts","../src/caching.ts","../src/store.ts","../src/cache-memory-store.ts"],"sourcesContent":["import { i18n } from '@aomex/core';\n\ni18n.register('en_US', 'cache', {\n wrong_type: 'wrong kind of value',\n not_integer: 'value is not an integer',\n});\n","import { i18n } from '@aomex/core';\n\ni18n.register('zh_CN', 'cache', {\n wrong_type: '错误的值类型',\n not_integer: '值不是整数类型',\n});\n","import { createHash } from 'node:crypto';\nimport { Store } from './store';\n\nexport namespace Caching {\n export interface Options {\n /**\n * 删除过期缓存防止占用空间,加快读取速度。概率取值范围:`1-100`。默认值:`10`\n */\n gcProbability?: number;\n }\n\n export type Types = string | number | object | boolean;\n}\n\nexport class Caching<S extends Store = Store, T extends object = object> {\n protected readonly gcProbability: number;\n readonly store: S;\n\n constructor(StoreClass: new (config: T) => S, config: T & Caching.Options) {\n this.gcProbability = (config.gcProbability ?? 10) / 100;\n this.store = new StoreClass(config);\n this.store.connect();\n }\n\n /**\n * 查看缓存是否存在\n */\n async exists(key: string): Promise<boolean> {\n await this.store.connect();\n return this.store.existsKey(this.buildKey(key));\n }\n\n /**\n *\n * 获取缓存。如果未找到缓存并且未提供默认值,则返回`null`\n * ```typescript\n * await cache.get('key'); // null\n * await cache.get('key', 'my-value'); // my-value\n * ```\n */\n async get<T>(key: string, defaultValue: T): Promise<T>;\n async get<T extends Caching.Types>(key: string): Promise<T | null>;\n async get(key: string, defaultValue?: Caching.Types): Promise<any> {\n await this.store.connect();\n const hashKey = this.buildKey(key);\n const result = await this.store.getValue(hashKey);\n return this.parseValue(result, defaultValue);\n }\n\n /**\n * 设置缓存。可以指定过期时间(毫秒)\n *\n * ```typescript\n * await cache.set('key', 'value');\n * await cache.set('key', 'value', 3600);\n * ```\n */\n async set(key: string, value: Caching.Types, durationMs?: number): Promise<boolean> {\n await this.store.connect();\n await this.applyGC();\n const hashKey = this.buildKey(key);\n return this.store.setValue(hashKey, JSON.stringify(value), durationMs);\n }\n\n /**\n * 设置缓存。如果缓存已经存在,则设置失败,返回`false`\n *\n * ```typescript\n * await cache.exists('key'); // false\n * await cache.setNX('key', 'value'); // true\n *\n * await cache.exists('key'); // true\n * await cache.setNX('key', 'value'); // false\n * ```\n */\n async setNX(key: string, value: Caching.Types, durationMs?: number): Promise<boolean> {\n await this.store.connect();\n await this.applyGC();\n const hashKey = this.buildKey(key);\n return this.store.setNotExistValue(hashKey, JSON.stringify(value), durationMs);\n }\n\n /**\n * 将一个或多个值按顺序插入到列表头部。当 key 存在但不是列表类型时,返回一个错误。\n *\n * 假设当前一个列表 foo=[s],执行 `leftPush('foo', a, b, c)` 后列表 foo=[a, b, c, s]\n */\n async leftPush(key: string, ...values: Caching.Types[]): Promise<boolean> {\n if (!values.length) return false;\n await this.store.connect();\n const hashKey = this.buildKey(key);\n return this.store.leftPushValue(\n hashKey,\n ...values.map((value) => JSON.stringify(value)),\n );\n }\n\n /**\n * 移除列表的最后一个元素,返回值为被移除的元素\n */\n async rightPop<T extends Caching.Types>(key: string): Promise<T | null> {\n await this.store.connect();\n const hashKey = this.buildKey(key);\n const result = await this.store.rightPopValue(hashKey);\n return this.parseValue(result);\n }\n\n /**\n * 将key中储存的数字值增一。\n *\n * 如果key不存在,那么key的值会先被初始化为0,然后再执行操作。\n *\n * 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。\n */\n async increment(key: string): Promise<number> {\n await this.store.connect();\n const hashKey = this.buildKey(key);\n return this.store.increaseValue(hashKey);\n }\n\n /**\n * 重新设置缓存时间。如果key不存在,则返回`false`\n */\n async expire(key: string, durationMs: number): Promise<boolean> {\n await this.store.connect();\n const hashKey = this.buildKey(key);\n return this.store.expireKey(hashKey, durationMs);\n }\n\n /**\n * 检测缓存的剩余生存时间(Time to live)\n * - 不存在则返回`-2`\n * - 未设置过期时间则返回`-1`\n * - 已设置过期时间则返回剩余时间,单位**毫秒**\n */\n async ttl(key: string) {\n await this.store.connect();\n const hashKey = this.buildKey(key);\n return this.store.ttlKey(hashKey);\n }\n\n /**\n * 将key中储存的数字值减一。\n *\n * 如果key不存在,那么key的值会先被初始化为0,然后再执行操作。\n *\n * 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。\n */\n async decrement(key: string): Promise<number> {\n await this.store.connect();\n const hashKey = this.buildKey(key);\n return this.store.decreaseValue(hashKey);\n }\n\n /**\n * 删除指定缓存\n */\n async delete(key: string): Promise<boolean> {\n await this.store.connect();\n const hashKey = this.buildKey(key);\n return this.store.deleteValue(hashKey);\n }\n\n /**\n * 删除所有缓存。若指定了keyPrefix,则只删除有该前缀的缓存\n */\n async deleteAll(): Promise<boolean> {\n await this.store.connect();\n return this.store.deleteAllValues();\n }\n\n protected buildKey(key: string): string {\n return key.length < 32 ? key : createHash('md5').update(key).digest('hex');\n }\n\n protected parseValue(value: string | null, defaultValue?: Caching.Types) {\n if (value === null) {\n return defaultValue === void 0 ? null : defaultValue;\n }\n\n try {\n return JSON.parse(value);\n } catch {\n return null;\n }\n }\n\n protected async applyGC() {\n if (Math.random() > this.gcProbability) return;\n await this.store.gc();\n }\n}\n","export abstract class Store {\n async connect() {}\n async gc() {}\n\n abstract existsKey(key: string): Promise<boolean>;\n abstract getValue(key: string): Promise<string | null>;\n abstract setValue(key: string, value: string, durationMS?: number): Promise<boolean>;\n abstract setNotExistValue(\n key: string,\n value: string,\n durationMS?: number,\n ): Promise<boolean>;\n abstract deleteValue(key: string): Promise<boolean>;\n abstract deleteAllValues(): Promise<boolean>;\n abstract increaseValue(key: string): Promise<number>;\n abstract decreaseValue(key: string): Promise<number>;\n abstract expireKey(key: string, durationMS: number): Promise<boolean>;\n abstract ttlKey(key: string): Promise<number>;\n abstract leftPushValue(key: string, ...values: string[]): Promise<boolean>;\n abstract rightPopValue(key: string): Promise<string | null>;\n}\n","import { i18n } from '@aomex/core';\nimport { Store } from './store';\n\nexport class CacheMemoryStore extends Store {\n private data = new Map<string, { value: string | string[]; expires?: number }>();\n\n override async existsKey(key: string): Promise<boolean> {\n return this.getValueSync(key, 'any') !== null;\n }\n\n override async getValue(key: string): Promise<string | null> {\n const value = this.getValueSync(key, 'string');\n return value;\n }\n\n override async setValue(\n key: string,\n value: string,\n duration?: number,\n ): Promise<boolean> {\n return this.setValueSync(key, value, duration);\n }\n\n override async setNotExistValue(\n key: string,\n value: string,\n duration?: number,\n ): Promise<boolean> {\n const currentValue = this.getValueSync(key, 'string');\n if (currentValue !== null) return false;\n return this.setValueSync(key, value, duration);\n }\n\n override async leftPushValue(key: string, ...values: string[]): Promise<boolean> {\n let result = this.getValueSync(key, 'array');\n if (result === null) result = [];\n result.push(...values);\n this.setValueSync(key, result);\n return true;\n }\n\n override async rightPopValue(key: string): Promise<string | null> {\n let result = this.getValueSync(key, 'array');\n if (result === null || !result.length) return null;\n const popValue = result.pop()!;\n if (result.length) {\n this.setValueSync(key, result);\n } else {\n this.data.delete(key);\n }\n return popValue;\n }\n\n override async increaseValue(key: string): Promise<number> {\n const result = this.getValueSync(key, 'string');\n let value: number;\n if (result === null) {\n this.data.set(key, { value: '0' });\n value = 0;\n } else {\n value = Number(result);\n }\n\n if (Number.isNaN(value) || !Number.isInteger(value)) {\n throw new Error('缓存值不是整数类型');\n }\n\n this.setValueSync(key, String(++value));\n return value;\n }\n\n override async decreaseValue(key: string): Promise<number> {\n const result = this.getValueSync(key, 'string');\n let value: number;\n if (result === null) {\n this.data.set(key, { value: '0' });\n value = 0;\n } else {\n value = Number(result);\n }\n\n if (Number.isNaN(value) || !Number.isInteger(value)) {\n throw new Error(i18n.t('cache.not_integer'));\n }\n\n this.setValueSync(key, String(--value));\n return value;\n }\n\n override async deleteValue(key: string): Promise<boolean> {\n this.data.delete(key);\n return true;\n }\n\n override async deleteAllValues(): Promise<boolean> {\n this.data.clear();\n return true;\n }\n\n override async expireKey(key: string, duration: number): Promise<boolean> {\n const result = this.data.get(key);\n if (!result) return false;\n if (result.expires && result.expires < Date.now()) return false;\n result.expires = Date.now() + duration;\n return true;\n }\n\n override async ttlKey(key: string): Promise<number> {\n const result = this.data.get(key);\n if (!result) return -2;\n if (!result.expires) return -1;\n if (result.expires < Date.now()) return -2;\n return result.expires - Date.now();\n }\n\n protected getValueSync<T extends 'string' | 'array' | 'any'>(\n key: string,\n assertType: T,\n ):\n | (T extends 'any' ? string | string[] : T extends 'string' ? string : Array<string>)\n | null {\n const result = this.data.get(key);\n if (!result) return null;\n if (result.expires && result.expires < Date.now()) return null;\n const value = result.value;\n\n let isValid = false;\n switch (assertType) {\n case 'string':\n isValid = typeof value === 'string';\n break;\n case 'array':\n isValid = Array.isArray(value);\n break;\n case 'any':\n isValid = true;\n break;\n }\n\n if (!isValid) {\n throw new Error(i18n.t('cache.wrong_type'));\n }\n\n return value as any;\n }\n\n protected setValueSync(\n key: string,\n value: string | string[],\n duration?: number,\n ): boolean {\n this.data.set(key, { value, expires: duration ? Date.now() + duration : undefined });\n return true;\n }\n\n override async gc() {\n const now = Date.now();\n this.data = new Map(\n Array.from(this.data.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Q,OAAO;AAErB,QAAI,UAAU;AACd,YAAQ,YAAY;AAAA,MAClB,KAAK;AACH,kBAAU,OAAO,UAAU;AAC3B;AAAA,MACF,KAAK;AACH,kBAAU,MAAM,QAAQ,KAAK;AAC7B;AAAA,MACF,KAAK;AACH,kBAAU;AACV;AAAA,IACJ;AAEA,QAAI,CAAC,SAAS;AACZ,YAAM,IAAI,MAAMA,MAAK,EAAE,kBAAkB,CAAC;AAAA,IAC5C;AAEA,WAAO;AAAA,EACT;AAAA,EAEU,aACR,KACA,OACA,UACS;AACT,SAAK,KAAK,IAAI,KAAK,EAAE,OAAO,SAAS,WAAW,KAAK,IAAI,IAAI,WAAW,OAAU,CAAC;AACnF,WAAO;AAAA,EACT;AAAA,EAEA,MAAe,KAAK;AAClB,UAAM,MAAM,KAAK,IAAI;AACrB,SAAK,OAAO,IAAI;AAAA,MACd,MAAM,KAAK,KAAK,KAAK,QAAQ,CAAC,EAAE;AAAA,QAC9B,CAAC,CAAC,GAAG,IAAI,MAAM,CAAC,KAAK,WAAW,KAAK,UAAU;AAAA,MACjD;AAAA,IACF;AAAA,EACF;AACF;","names":["i18n","i18n","i18n"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@aomex/cache",
3
- "version": "0.0.7",
4
- "description": "",
3
+ "version": "2.0.0",
4
+ "description": "缓存基础类",
5
5
  "type": "module",
6
6
  "types": "dist/index.d.ts",
7
7
  "exports": {
@@ -21,12 +21,18 @@
21
21
  "README.md",
22
22
  "CHANGELOG.md"
23
23
  ],
24
- "keywords": [],
25
24
  "repository": "git@github.com:aomex/aomex.git",
26
25
  "homepage": "https://aomex.js.org",
27
26
  "license": "MIT",
28
- "dependencies": {
29
- "lru-cache": "^8.0.0"
27
+ "engines": {
28
+ "node": "^20.13.0 || ^22"
29
+ },
30
+ "peerDependencies": {
31
+ "@aomex/core": "^1.0.0"
32
+ },
33
+ "devDependencies": {
34
+ "@aomex/core": "^2.0.0",
35
+ "@aomex/internal-tools": "^2.0.0"
30
36
  },
31
37
  "scripts": {}
32
38
  }