-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
109 lines (92 loc) · 2.61 KB
/
Copy pathindex.ts
File metadata and controls
109 lines (92 loc) · 2.61 KB
1
2
3
4
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import type { RedisClientType } from "redis";
export default (redisClient: RedisClientType) => {
const checkIsNewest = async (options: {
list: string;
item: string;
limit?: number;
isFirstLoop?: boolean;
}) => {
const isListExist = options.isFirstLoop
? Number(await redisClient.exists(options.list)) !== 0
: false;
if (options.isFirstLoop && !isListExist) {
await redisClient.lPush(options.list, options.item);
return true;
}
const lastUploadedIndex = await redisClient.lPos(
options.list,
options.item
);
if (lastUploadedIndex !== null) return false;
await redisClient.lPush(options.list, options.item);
if (options.limit)
await redisClient.lTrim(options.list, 0, options.limit - 1);
return true;
};
const isCacheHit = async (key: string) => {
const isExist = Number(await redisClient.exists(key)) !== 0;
return isExist;
};
const getCache = async (key: string) => {
const data = await redisClient.get(key);
return data;
};
const setCache = async (options?: {
key: string;
value: string;
ttl: number;
}) => {
const { key, value, ttl } = options || {};
await redisClient.set(key, value, {
EX: ttl,
});
};
const invalidateCache = async (key: string) => {
await redisClient.del(key);
};
const setHashCache = async (options?: {
key: string;
field: string;
value: string;
}) => {
const { key, field, value } = options || {};
await redisClient.hSet(key, field, value);
};
const getHashCache = async (options?: { key: string; field: string }) => {
const { key, field } = options || {};
const data = await redisClient.hGet(key, field);
return data;
};
const invalidateHashCache = async (options?: {
key: string;
field: string;
}) => {
const { key, field } = options || {};
await redisClient.hDel(key, field);
};
const invalidateAllHashCache = async (key: string) => {
await redisClient.del(key);
};
const isHashCacheHit = async (options?: { key: string; field: string }) => {
const { key, field } = options || {};
const isExist = Number(await redisClient.hExists(key, field)) !== 0;
return isExist;
};
const setHashCacheExpire = async (options?: { key: string; ttl: number }) => {
const { key, ttl } = options || {};
await redisClient.expire(key, ttl);
};
return {
checkIsNewest,
isCacheHit,
isHashCacheHit,
getCache,
setCache,
invalidateCache,
setHashCache,
getHashCache,
invalidateHashCache,
invalidateAllHashCache,
setHashCacheExpire,
};
};