@aomex/cache-redis-adapter 2.2.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 ADDED
@@ -0,0 +1,114 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ # [2.2.0](https://github.com/aomex/aomex/compare/v2.1.0...v2.2.0) (2024-08-05)
7
+
8
+
9
+ ### Features
10
+
11
+ * **cache:** 适配器模式 ([ac8c318](https://github.com/aomex/aomex/commit/ac8c318ec67d6f880c8d291fc341f5156a6b47d9))
12
+
13
+
14
+
15
+
16
+
17
+ # [2.1.0](https://github.com/aomex/aomex/compare/v2.0.2...v2.1.0) (2024-07-28)
18
+
19
+ **Note:** Version bump only for package @aomex/cache-redis-store
20
+
21
+
22
+
23
+
24
+
25
+ ## [2.0.2](https://github.com/aomex/aomex/compare/v2.0.1...v2.0.2) (2024-07-27)
26
+
27
+ **Note:** Version bump only for package @aomex/cache-redis-store
28
+
29
+
30
+
31
+
32
+
33
+ ## [2.0.1](https://github.com/aomex/aomex/compare/v2.0.0...v2.0.1) (2024-07-26)
34
+
35
+
36
+ ### Bug Fixes
37
+
38
+ * **cache-redis-store:** 包名错误 ([b6839f8](https://github.com/aomex/aomex/commit/b6839f89a7291ba0cfa765c5acd79233d789d052))
39
+
40
+
41
+
42
+
43
+
44
+ # [2.0.0](https://github.com/aomex/aomex/compare/v1.7.0...v2.0.0) (2024-07-26)
45
+
46
+
47
+ ### Features
48
+
49
+ * **cache:** 新的缓存库 ([cca0b52](https://github.com/aomex/aomex/commit/cca0b52d86029fb1303cd9bd1f362d2dd8b406b6))
50
+
51
+
52
+ ### BREAKING CHANGES
53
+
54
+ * **cache:** 1. 删除了memory-cache,redis-cache,file-cache
55
+ 2. 采用适配器形式创建缓存实例
56
+
57
+
58
+
59
+
60
+
61
+ # [1.7.0](https://github.com/aomex/aomex/compare/v1.6.0...v1.7.0) (2024-07-13)
62
+
63
+ **Note:** Version bump only for package @aomex/redis-cache
64
+
65
+
66
+
67
+
68
+
69
+ # [1.6.0](https://github.com/aomex/aomex/compare/v1.5.1...v1.6.0) (2024-07-05)
70
+
71
+ **Note:** Version bump only for package @aomex/redis-cache
72
+
73
+
74
+
75
+
76
+
77
+ ## [1.5.1](https://github.com/aomex/aomex/compare/v1.5.0...v1.5.1) (2024-07-04)
78
+
79
+ **Note:** Version bump only for package @aomex/redis-cache
80
+
81
+
82
+
83
+
84
+
85
+ # [1.5.0](https://github.com/aomex/aomex/compare/v1.4.0...v1.5.0) (2024-07-04)
86
+
87
+ **Note:** Version bump only for package @aomex/redis-cache
88
+
89
+
90
+
91
+
92
+
93
+ # [1.4.0](https://github.com/aomex/aomex/compare/v1.3.0...v1.4.0) (2024-06-29)
94
+
95
+ **Note:** Version bump only for package @aomex/redis-cache
96
+
97
+
98
+
99
+
100
+
101
+ # [1.3.0](https://github.com/aomex/aomex/compare/v1.2.0...v1.3.0) (2024-06-28)
102
+
103
+ **Note:** Version bump only for package @aomex/redis-cache
104
+
105
+
106
+
107
+
108
+
109
+ # 1.1.0 (2024-06-27)
110
+
111
+
112
+ ### Features
113
+
114
+ * 初始化 ([3350159](https://github.com/aomex/aomex/commit/3350159454ad230e6d910405f907293b059b1f49))
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-2024 geekact
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # @aomex/redis-cache
2
+
3
+ 文档:https://aomex.js.org
@@ -0,0 +1,22 @@
1
+ import { CacheAdapter } from '@aomex/cache';
2
+ import { Redis, RedisOptions } from 'ioredis';
3
+
4
+ declare class CacheRedisAdapter extends CacheAdapter {
5
+ readonly redis: Redis;
6
+ protected version?: string;
7
+ constructor(config: RedisOptions);
8
+ getValue(key: string): Promise<string | null>;
9
+ setValue(key: string, value: string, duration?: number | undefined): Promise<boolean>;
10
+ setNotExistValue(key: string, value: string, duration?: number | undefined): Promise<boolean>;
11
+ existsKey(key: string): Promise<boolean>;
12
+ deleteValue(key: string): Promise<boolean>;
13
+ deleteAllValues(): Promise<boolean>;
14
+ increaseValue(key: string): Promise<number>;
15
+ decreaseValue(key: string): Promise<number>;
16
+ expireKey(key: string, duration: number): Promise<boolean>;
17
+ ttlKey(key: string): Promise<number>;
18
+ leftPushValue(key: string, ...values: string[]): Promise<boolean>;
19
+ rightPopValue(key: string): Promise<string | null>;
20
+ }
21
+
22
+ export { CacheRedisAdapter };
package/dist/index.js ADDED
@@ -0,0 +1,84 @@
1
+ // src/index.ts
2
+ import { CacheAdapter } from "@aomex/cache";
3
+ import { Redis } from "ioredis";
4
+ var CacheRedisAdapter = class extends CacheAdapter {
5
+ redis;
6
+ version;
7
+ constructor(config) {
8
+ super();
9
+ this.redis = new Redis(config);
10
+ }
11
+ getValue(key) {
12
+ return this.redis.get(key);
13
+ }
14
+ async setValue(key, value, duration) {
15
+ let result;
16
+ if (duration !== void 0) {
17
+ result = await this.redis.set(key, value, "PX", duration);
18
+ } else {
19
+ result = await this.redis.set(key, value);
20
+ }
21
+ return result === "OK";
22
+ }
23
+ async setNotExistValue(key, value, duration) {
24
+ let result;
25
+ if (duration !== void 0) {
26
+ result = await this.redis.set(key, value, "PX", duration, "NX");
27
+ } else {
28
+ result = await this.redis.set(key, value, "NX");
29
+ }
30
+ return result === "OK";
31
+ }
32
+ async existsKey(key) {
33
+ const count = await this.redis.exists(key);
34
+ return count === 1;
35
+ }
36
+ async deleteValue(key) {
37
+ await this.redis.del(key);
38
+ return true;
39
+ }
40
+ async deleteAllValues() {
41
+ const { redis } = this;
42
+ const redisPrefix = redis.options.keyPrefix ?? "";
43
+ if (redisPrefix) {
44
+ await redis.eval(
45
+ `
46
+ for _,k in ipairs(redis.call('keys', ARGV[1])) do
47
+ redis.call('del', k)
48
+ end
49
+ `,
50
+ 0,
51
+ `${redisPrefix}*`
52
+ );
53
+ } else {
54
+ await redis.flushdb();
55
+ }
56
+ return true;
57
+ }
58
+ increaseValue(key) {
59
+ return this.redis.incr(key);
60
+ }
61
+ decreaseValue(key) {
62
+ return this.redis.decr(key);
63
+ }
64
+ async expireKey(key, duration) {
65
+ const changeRow = await this.redis.pexpire(key, duration);
66
+ return changeRow === 1;
67
+ }
68
+ async ttlKey(key) {
69
+ const result = await this.redis.pttl(key);
70
+ return result;
71
+ }
72
+ async leftPushValue(key, ...values) {
73
+ const changeRow = await this.redis.lpush(key, ...values.reverse());
74
+ return changeRow >= 1;
75
+ }
76
+ async rightPopValue(key) {
77
+ const result = await this.redis.rpop(key);
78
+ return result;
79
+ }
80
+ };
81
+ export {
82
+ CacheRedisAdapter
83
+ };
84
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { CacheAdapter } from '@aomex/cache';\nimport { Redis, type RedisOptions } from 'ioredis';\n\nexport class CacheRedisAdapter extends CacheAdapter {\n public readonly redis: Redis;\n protected version?: string;\n\n constructor(config: RedisOptions) {\n super();\n this.redis = new Redis(config);\n }\n\n getValue(key: string): Promise<string | null> {\n return this.redis.get(key);\n }\n\n async setValue(\n key: string,\n value: string,\n duration?: number | undefined,\n ): Promise<boolean> {\n let result: 'OK';\n if (duration !== undefined) {\n result = await this.redis.set(key, value, 'PX', duration);\n } else {\n result = await this.redis.set(key, value);\n }\n return result === 'OK';\n }\n\n override async setNotExistValue(\n key: string,\n value: string,\n duration?: number | undefined,\n ): Promise<boolean> {\n let result: 'OK' | null;\n if (duration !== undefined) {\n result = await this.redis.set(key, value, 'PX', duration, 'NX');\n } else {\n result = await this.redis.set(key, value, 'NX');\n }\n return result === 'OK';\n }\n\n async existsKey(key: string): Promise<boolean> {\n const count = await this.redis.exists(key);\n return count === 1;\n }\n\n async deleteValue(key: string): Promise<boolean> {\n await this.redis.del(key);\n return true;\n }\n\n async deleteAllValues(): Promise<boolean> {\n const { redis } = this;\n const redisPrefix = redis.options.keyPrefix ?? '';\n\n if (redisPrefix) {\n await redis.eval(\n `\nfor _,k in ipairs(redis.call('keys', ARGV[1])) do \n redis.call('del', k) \nend\n`,\n 0,\n `${redisPrefix}*`,\n );\n } else {\n await redis.flushdb();\n }\n return true;\n }\n\n override increaseValue(key: string): Promise<number> {\n return this.redis.incr(key);\n }\n\n override decreaseValue(key: string): Promise<number> {\n return this.redis.decr(key);\n }\n\n override async expireKey(key: string, duration: number): Promise<boolean> {\n const changeRow = await this.redis.pexpire(key, duration);\n return changeRow === 1;\n }\n\n override async ttlKey(key: string): Promise<number> {\n const result = await this.redis.pttl(key);\n return result;\n }\n\n override async leftPushValue(key: string, ...values: string[]): Promise<boolean> {\n const changeRow = await this.redis.lpush(key, ...values.reverse());\n return changeRow >= 1;\n }\n\n override async rightPopValue(key: string): Promise<string | null> {\n const result = await this.redis.rpop(key);\n return result;\n }\n}\n"],"mapp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AACA,WAAO;AAAA,EACT;AAAA,EAES,cAAc,KAA8B;AACnD,WAAO,KAAK,MAAM,KAAK,GAAG;AAAA,EAC5B;AAAA,EAES,cAAc,KAA8B;AACnD,WAAO,KAAK,MAAM,KAAK,GAAG;AAAA,EAC5B;AAAA,EAEA,MAAe,UAAU,KAAa,UAAoC;AACxE,UAAM,YAAY,MAAM,KAAK,MAAM,QAAQ,KAAK,QAAQ;AACxD,WAAO,cAAc;AAAA,EACvB;AAAA,EAEA,MAAe,OAAO,KAA8B;AAClD,UAAM,SAAS,MAAM,KAAK,MAAM,KAAK,GAAG;AACxC,WAAO;AAAA,EACT;AAAA,EAEA,MAAe,cAAc,QAAgB,QAAoC;AAC/E,UAAM,YAAY,MAAM,KAAK,MAAM,MAAM,KAAK,GAAG,OAAO,QAAQ,CAAC;AACjE,WAAO,aAAa;AAAA,EACtB;AAAA,EAEA,MAAe,cAAc,KAAqC;AAChE,UAAM,SAAS,MAAM,KAAK,MAAM,KAAK,GAAG;AACxC,WAAO;AAAA,EACT;AACF;","names":[]}
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@aomex/cache-redis-adapter",
3
+ "version": "2.2.0",
4
+ "description": "aomex缓存库redis适配器",
5
+ "type": "module",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js"
11
+ },
12
+ "./package.json": "./package.json"
13
+ },
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "files": [
18
+ "dist",
19
+ "LICENSE",
20
+ "package.json",
21
+ "README.md",
22
+ "CHANGELOG.md"
23
+ ],
24
+ "keywords": [],
25
+ "repository": "git@github.com:aomex/aomex.git",
26
+ "homepage": "https://aomex.js.org",
27
+ "license": "MIT",
28
+ "engines": {
29
+ "node": "^20.13.0 || ^22"
30
+ },
31
+ "dependencies": {
32
+ "ioredis": "^5.4.1",
33
+ "@aomex/cache": "^2.2.0"
34
+ },
35
+ "devDependencies": {
36
+ "@aomex/internal-tools": "^2.2.0"
37
+ },
38
+ "scripts": {}
39
+ }