fix(buffer): check if key exists

This commit is contained in:
Carl-Gerhard Lindesvärd
2025-02-26 12:59:41 +01:00
parent 80fa34469e
commit 5abf7b988d

View File

@@ -44,6 +44,14 @@ export class ProfileBuffer extends BaseBuffer {
return `${this.redisProfilePrefix}${projectId}:${profileId}`; return `${this.redisProfilePrefix}${projectId}:${profileId}`;
} }
async alreadyExists(profile: IClickhouseProfile) {
const cacheKey = this.getProfileCacheKey({
profileId: profile.id,
projectId: profile.project_id,
});
return (await getRedisCache().exists(cacheKey)) === 1;
}
async add(profile: IClickhouseProfile, isFromEvent = false) { async add(profile: IClickhouseProfile, isFromEvent = false) {
const logger = this.logger.child({ const logger = this.logger.child({
projectId: profile.project_id, projectId: profile.project_id,
@@ -53,13 +61,13 @@ export class ProfileBuffer extends BaseBuffer {
try { try {
logger.debug('Adding profile'); logger.debug('Adding profile');
const existingProfile = await this.fetchFromCache(profile, logger); if (isFromEvent && (await this.alreadyExists(profile))) {
if (isFromEvent && existingProfile) {
logger.debug('Profile already created, skipping'); logger.debug('Profile already created, skipping');
return; return;
} }
const existingProfile = await this.fetchProfile(profile, logger);
const mergedProfile: IClickhouseProfile = existingProfile const mergedProfile: IClickhouseProfile = existingProfile
? deepMergeObjects(existingProfile, profile) ? deepMergeObjects(existingProfile, profile)
: profile; : profile;
@@ -126,7 +134,7 @@ export class ProfileBuffer extends BaseBuffer {
} }
} }
private async fetchFromCache( private async fetchProfile(
profile: IClickhouseProfile, profile: IClickhouseProfile,
logger: ILogger, logger: ILogger,
): Promise<IClickhouseProfile | null> { ): Promise<IClickhouseProfile | null> {