improve(db): do not use keys in redis
This commit is contained in:
@@ -76,7 +76,7 @@ export function wsVisitors(
|
|||||||
const { params } = req;
|
const { params } = req;
|
||||||
|
|
||||||
getRedisSub().subscribe('event:received');
|
getRedisSub().subscribe('event:received');
|
||||||
getRedisSub().psubscribe('__key*:expired');
|
getRedisSub().psubscribe('__keyevent@0__:expired');
|
||||||
|
|
||||||
const message = (channel: string, message: string) => {
|
const message = (channel: string, message: string) => {
|
||||||
if (channel === 'event:received') {
|
if (channel === 'event:received') {
|
||||||
@@ -89,6 +89,10 @@ export function wsVisitors(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
const pmessage = (pattern: string, channel: string, message: string) => {
|
const pmessage = (pattern: string, channel: string, message: string) => {
|
||||||
|
if (!message.startsWith('live:visitors:')) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
const [projectId] = getLiveEventInfo(message);
|
const [projectId] = getLiveEventInfo(message);
|
||||||
if (projectId && projectId === params.projectId) {
|
if (projectId && projectId === params.projectId) {
|
||||||
getLiveVisitors(params.projectId).then((count) => {
|
getLiveVisitors(params.projectId).then((count) => {
|
||||||
@@ -102,7 +106,7 @@ export function wsVisitors(
|
|||||||
|
|
||||||
connection.socket.on('close', () => {
|
connection.socket.on('close', () => {
|
||||||
getRedisSub().unsubscribe('event:saved');
|
getRedisSub().unsubscribe('event:saved');
|
||||||
getRedisSub().punsubscribe('__key*:expired');
|
getRedisSub().punsubscribe('__keyevent@0__:expired');
|
||||||
getRedisSub().off('message', message);
|
getRedisSub().off('message', message);
|
||||||
getRedisSub().off('pmessage', pmessage);
|
getRedisSub().off('pmessage', pmessage);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -246,12 +246,8 @@ return "OK"
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (event.profile_id) {
|
if (event.profile_id) {
|
||||||
multi.set(
|
multi.sadd(`live:visitors:${event.project_id}`, event.profile_id);
|
||||||
`live:event:${event.project_id}:${event.profile_id}`,
|
multi.expire(`live:visitors:${event.project_id}`, 60 * 5); // 5 minutes
|
||||||
'',
|
|
||||||
'EX',
|
|
||||||
60 * 5,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_multi) {
|
if (!_multi) {
|
||||||
|
|||||||
@@ -235,8 +235,7 @@ export function transformMinimalEvent(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function getLiveVisitors(projectId: string) {
|
export async function getLiveVisitors(projectId: string) {
|
||||||
const keys = await getRedisCache().keys(`live:event:${projectId}:*`);
|
return getRedisCache().scard(`live:visitors:${projectId}`);
|
||||||
return keys.length;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getEvents(
|
export async function getEvents(
|
||||||
|
|||||||
Reference in New Issue
Block a user