@domain.js/main 0.3.5 → 0.3.6
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.
|
@@ -5,12 +5,14 @@ interface Cnf {
|
|
|
5
5
|
};
|
|
6
6
|
}
|
|
7
7
|
interface Deps {
|
|
8
|
-
redis: Pick<Redis, "hget" | "hset" | "hincrby">;
|
|
8
|
+
redis: Pick<Redis, "hget" | "hset" | "hincrby" | "hmget">;
|
|
9
9
|
}
|
|
10
10
|
export declare function Main(cnf: Cnf, deps: Deps): {
|
|
11
|
+
mget: (keys: string[]) => Promise<number[]>;
|
|
11
12
|
get: (key: string) => Promise<number>;
|
|
12
13
|
set: (key: string, val: number) => Promise<number>;
|
|
13
14
|
incr: (key: string) => Promise<number>;
|
|
15
|
+
decr: (key: string) => Promise<number>;
|
|
14
16
|
};
|
|
15
17
|
export declare const Deps: string[];
|
|
16
18
|
export {};
|
|
@@ -4,13 +4,39 @@ exports.Deps = exports.Main = void 0;
|
|
|
4
4
|
function Main(cnf, deps) {
|
|
5
5
|
const { counter: { key: REDIS_KEY }, } = cnf;
|
|
6
6
|
const { redis } = deps;
|
|
7
|
+
/**
|
|
8
|
+
* 获取指定 key 的统计数
|
|
9
|
+
* @param key 要获取的 key
|
|
10
|
+
*/
|
|
7
11
|
const get = async (key) => {
|
|
8
12
|
const num = await redis.hget(REDIS_KEY, key);
|
|
9
13
|
return Number(num) | 0;
|
|
10
14
|
};
|
|
15
|
+
/**
|
|
16
|
+
* 主动设置某个key为一个数字
|
|
17
|
+
* @param key 要设置的key
|
|
18
|
+
* @param val 要设置的值
|
|
19
|
+
*/
|
|
11
20
|
const set = (key, val) => redis.hset(REDIS_KEY, key, Math.max(0, val | 0));
|
|
21
|
+
/**
|
|
22
|
+
* 某个key自增长1
|
|
23
|
+
* @param key 要自增长的key
|
|
24
|
+
*/
|
|
12
25
|
const incr = (key) => redis.hincrby(REDIS_KEY, key, 1);
|
|
13
|
-
|
|
26
|
+
/**
|
|
27
|
+
* 某个key自减少1
|
|
28
|
+
* @param key 要自减少的key
|
|
29
|
+
*/
|
|
30
|
+
const decr = (key) => redis.hincrby(REDIS_KEY, key, -1);
|
|
31
|
+
/**
|
|
32
|
+
* 一次获取多个key的统计值
|
|
33
|
+
* @param keys 多个key按序输入, 返回的统计数据为数组,和keys数组保持对应关系
|
|
34
|
+
*/
|
|
35
|
+
const mget = async (keys) => {
|
|
36
|
+
const res = await redis.hmget(REDIS_KEY, ...keys);
|
|
37
|
+
return res.map((x) => Number(x) | 0);
|
|
38
|
+
};
|
|
39
|
+
return { mget, get, set, incr, decr };
|
|
14
40
|
}
|
|
15
41
|
exports.Main = Main;
|
|
16
42
|
exports.Deps = ["redis"];
|