improve(db): do not use keys in redis

This commit is contained in:
Carl-Gerhard Lindesvärd
2025-02-11 12:05:08 +01:00
parent 0cf73e299a
commit 848f475412
3 changed files with 9 additions and 10 deletions

View File

@@ -76,7 +76,7 @@ export function wsVisitors(
const { params } = req;
getRedisSub().subscribe('event:received');
getRedisSub().psubscribe('__key*:expired');
getRedisSub().psubscribe('__keyevent@0__:expired');
const message = (channel: string, message: string) => {
if (channel === 'event:received') {
@@ -89,6 +89,10 @@ export function wsVisitors(
}
};
const pmessage = (pattern: string, channel: string, message: string) => {
if (!message.startsWith('live:visitors:')) {
return null;
}
const [projectId] = getLiveEventInfo(message);
if (projectId && projectId === params.projectId) {
getLiveVisitors(params.projectId).then((count) => {
@@ -102,7 +106,7 @@ export function wsVisitors(
connection.socket.on('close', () => {
getRedisSub().unsubscribe('event:saved');
getRedisSub().punsubscribe('__key*:expired');
getRedisSub().punsubscribe('__keyevent@0__:expired');
getRedisSub().off('message', message);
getRedisSub().off('pmessage', pmessage);
});

View File

@@ -246,12 +246,8 @@ return "OK"
}
if (event.profile_id) {
multi.set(
`live:event:${event.project_id}:${event.profile_id}`,
'',
'EX',
60 * 5,
);
multi.sadd(`live:visitors:${event.project_id}`, event.profile_id);
multi.expire(`live:visitors:${event.project_id}`, 60 * 5); // 5 minutes
}
if (!_multi) {

View File

@@ -235,8 +235,7 @@ export function transformMinimalEvent(
}
export async function getLiveVisitors(projectId: string) {
const keys = await getRedisCache().keys(`live:event:${projectId}:*`);
return keys.length;
return getRedisCache().scard(`live:visitors:${projectId}`);
}
export async function getEvents(