@less-is-more/less-js 1.2.20 → 1.2.21
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/package.json +1 -1
- package/src/cache.js +28 -28
- package/src/redis.js +2 -1
- package/test/test-redis.js +49 -38
package/package.json
CHANGED
package/src/cache.js
CHANGED
|
@@ -2,32 +2,32 @@ const Redis = require("./redis");
|
|
|
2
2
|
const Param = require("./param");
|
|
3
3
|
|
|
4
4
|
module.exports = class Cache {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
5
|
+
/**
|
|
6
|
+
* 缓存并获取结果
|
|
7
|
+
* @param {*} key 缓存关键词,拼接参数
|
|
8
|
+
* @param {number} timeSecond 缓存秒数
|
|
9
|
+
* @param {object} thisObject this指向的对象
|
|
10
|
+
* @param {function} targetMethod 没有缓存时,执行目标。暂时支持返回文本、对象、数组
|
|
11
|
+
* @param {...any} args 没有缓存时,参数
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
static async get(key, timeSecond, thisObject, targetMethod, ...args) {
|
|
15
|
+
let fullKey = key;
|
|
16
|
+
args.forEach((nArg) => (fullKey += ":" + nArg));
|
|
17
|
+
let savedData = await Redis.exec("get", fullKey);
|
|
18
|
+
if (savedData == null) {
|
|
19
|
+
let result = await targetMethod.apply(thisObject, args);
|
|
20
|
+
if (!Param.isBlank(result)) {
|
|
21
|
+
if (typeof result === "string") {
|
|
22
|
+
await Redis.exec("setex", fullKey, timeSecond + "", result);
|
|
23
|
+
return result;
|
|
24
|
+
} else {
|
|
25
|
+
throw new Error("只支持返回值是文本的方法");
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
} else {
|
|
29
|
+
console.log("Found cache", fullKey);
|
|
30
|
+
return savedData;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
33
|
};
|
package/src/redis.js
CHANGED
|
@@ -33,7 +33,8 @@ module.exports = class Redis {
|
|
|
33
33
|
|
|
34
34
|
static async exec(name, ...args) {
|
|
35
35
|
let client = await Redis.getInstance();
|
|
36
|
-
|
|
36
|
+
args.unshift(name);
|
|
37
|
+
return await client.sendCommand(args);
|
|
37
38
|
}
|
|
38
39
|
|
|
39
40
|
static formatKey(name, params = {}) {
|
package/test/test-redis.js
CHANGED
|
@@ -2,42 +2,53 @@ const assert = require("assert");
|
|
|
2
2
|
const Redis = require("../src/redis.js");
|
|
3
3
|
|
|
4
4
|
describe("redis.js", () => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
5
|
+
before(() => {
|
|
6
|
+
console.log("init");
|
|
7
|
+
Redis.init("127.0.0.1", "default", "KS5UggH4LNyyLBdr");
|
|
8
|
+
});
|
|
9
|
+
describe("getInstance()", () => {
|
|
10
|
+
it("connect", async () => {
|
|
11
|
+
try {
|
|
12
|
+
let result = await Redis.exec("get", "test");
|
|
13
|
+
console.log(result);
|
|
14
|
+
console.log("Connection has been established successfully.");
|
|
15
|
+
assert.ok(true);
|
|
16
|
+
} catch (error) {
|
|
17
|
+
console.error("Unable to connect to the redis:", error);
|
|
18
|
+
assert.ok(false);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
it("set", async () => {
|
|
22
|
+
try {
|
|
23
|
+
await Redis.exec("set", "test", "OK");
|
|
24
|
+
let reply = await Redis.exec("get", "test");
|
|
25
|
+
console.log(reply);
|
|
26
|
+
assert.ok(true);
|
|
27
|
+
} catch (error) {
|
|
28
|
+
assert.ok(false);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
it("set ex", async () => {
|
|
32
|
+
try {
|
|
33
|
+
await Redis.exec("setex", "test", "100", "OK");
|
|
34
|
+
let reply = await Redis.exec("get", "test");
|
|
35
|
+
console.log(reply);
|
|
36
|
+
assert.ok(true);
|
|
37
|
+
} catch (error) {
|
|
38
|
+
console.log(error);
|
|
39
|
+
assert.ok(false);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
describe("formatKey()", () => {
|
|
44
|
+
it("no params", async () => {
|
|
45
|
+
let result = await Redis.formatKey("order", {});
|
|
46
|
+
assert(result == "order");
|
|
47
|
+
});
|
|
48
|
+
it("has params", async () => {
|
|
49
|
+
let result = await Redis.formatKey("order", { code: "123" });
|
|
50
|
+
console.log(result);
|
|
51
|
+
assert(result == "order:code:123");
|
|
52
|
+
});
|
|
53
|
+
});
|
|
43
54
|
});
|