From 6dec922dc9c7f7c218b295df1deee46a135dd714 Mon Sep 17 00:00:00 2001 From: Thomas Feldtkeller Date: Tue, 5 Nov 2024 09:52:38 +0100 Subject: [PATCH 1/4] adjust roles cache to use different tokens for different operations --- .../src/modules/school/api/test/school-users.api.spec.ts | 3 ++- apps/server/src/shared/repo/role/role.repo.ts | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/apps/server/src/modules/school/api/test/school-users.api.spec.ts b/apps/server/src/modules/school/api/test/school-users.api.spec.ts index f514c9468bc..2f02e3f459c 100644 --- a/apps/server/src/modules/school/api/test/school-users.api.spec.ts +++ b/apps/server/src/modules/school/api/test/school-users.api.spec.ts @@ -29,7 +29,8 @@ describe('School Controller (API)', () => { beforeEach(async () => { await cleanupCollections(em); - await em.clearCache('roles-cache-teacher'); + await em.clearCache('roles-cache-byname-teacher'); + await em.clearCache('bymultiplenames-byname-teacher'); }); afterAll(async () => { diff --git a/apps/server/src/shared/repo/role/role.repo.ts b/apps/server/src/shared/repo/role/role.repo.ts index 02527a4bfb2..37e64e8d5bc 100644 --- a/apps/server/src/shared/repo/role/role.repo.ts +++ b/apps/server/src/shared/repo/role/role.repo.ts @@ -16,7 +16,7 @@ export class RoleRepo extends BaseRepo { const promise: Promise = this._em.findOneOrFail( Role, { name }, - { cache: [`roles-cache-${name}`, this.cacheExpiration] } + { cache: [`roles-cache-bymultiplenames-${name}`, this.cacheExpiration] } ); return promise; } @@ -30,7 +30,7 @@ export class RoleRepo extends BaseRepo { const promise: Promise = this._em.find( Role, { name: { $in: names } }, - { cache: [`roles-cache-${names.join('-')}`, this.cacheExpiration] } + { cache: [`roles-cache-bymultiplenames-${names.join('-')}`, this.cacheExpiration] } ); return promise; } From 7a625a450667fdb7e1ab8b4e77ec5b5ac1945c86 Mon Sep 17 00:00:00 2001 From: Thomas Feldtkeller Date: Tue, 5 Nov 2024 10:54:04 +0100 Subject: [PATCH 2/4] fix cache names --- .../src/modules/school/api/test/school-users.api.spec.ts | 2 +- apps/server/src/shared/repo/role/role.repo.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/server/src/modules/school/api/test/school-users.api.spec.ts b/apps/server/src/modules/school/api/test/school-users.api.spec.ts index 2f02e3f459c..2c02af91a27 100644 --- a/apps/server/src/modules/school/api/test/school-users.api.spec.ts +++ b/apps/server/src/modules/school/api/test/school-users.api.spec.ts @@ -30,7 +30,7 @@ describe('School Controller (API)', () => { beforeEach(async () => { await cleanupCollections(em); await em.clearCache('roles-cache-byname-teacher'); - await em.clearCache('bymultiplenames-byname-teacher'); + await em.clearCache('roles-cache-bynames-teacher'); }); afterAll(async () => { diff --git a/apps/server/src/shared/repo/role/role.repo.ts b/apps/server/src/shared/repo/role/role.repo.ts index 37e64e8d5bc..e11e6615fda 100644 --- a/apps/server/src/shared/repo/role/role.repo.ts +++ b/apps/server/src/shared/repo/role/role.repo.ts @@ -16,7 +16,7 @@ export class RoleRepo extends BaseRepo { const promise: Promise = this._em.findOneOrFail( Role, { name }, - { cache: [`roles-cache-bymultiplenames-${name}`, this.cacheExpiration] } + { cache: [`roles-cache-byname-${name}`, this.cacheExpiration] } ); return promise; } @@ -30,7 +30,7 @@ export class RoleRepo extends BaseRepo { const promise: Promise = this._em.find( Role, { name: { $in: names } }, - { cache: [`roles-cache-bymultiplenames-${names.join('-')}`, this.cacheExpiration] } + { cache: [`roles-cache-bynames-${names.join('-')}`, this.cacheExpiration] } ); return promise; } From a7d946747a0ee5b2c2a9166de76a6db4beaaf304 Mon Sep 17 00:00:00 2001 From: Thomas Feldtkeller Date: Tue, 5 Nov 2024 11:01:39 +0100 Subject: [PATCH 3/4] test for using different role caches in sequence --- .../shared/repo/role/role.repo.integration.spec.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/apps/server/src/shared/repo/role/role.repo.integration.spec.ts b/apps/server/src/shared/repo/role/role.repo.integration.spec.ts index 91b2a083fff..b9061cb04cc 100644 --- a/apps/server/src/shared/repo/role/role.repo.integration.spec.ts +++ b/apps/server/src/shared/repo/role/role.repo.integration.spec.ts @@ -148,4 +148,18 @@ describe('role repo', () => { expect(result).toContainEqual(roleB); }); }); + + describe('roles cache', () => { + it('should successfully use different caches in sequence', async () => { + const roleA = roleFactory.build(); + const roleB = roleFactory.build(); + + await em.persistAndFlush([roleA, roleB]); + + const firstResult = await repo.findByName(roleA.name); + expect(firstResult).toEqual(roleA.name); + const secondResult = await repo.findByNames([roleA.name]); + expect(secondResult).toEqual([roleA.name]); + }); + }); }); From 82cac37430e090b88e0c1bd217e1a00f15647980 Mon Sep 17 00:00:00 2001 From: Thomas Feldtkeller Date: Tue, 5 Nov 2024 12:26:47 +0100 Subject: [PATCH 4/4] fix new test --- .../server/src/shared/repo/role/role.repo.integration.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/server/src/shared/repo/role/role.repo.integration.spec.ts b/apps/server/src/shared/repo/role/role.repo.integration.spec.ts index b9061cb04cc..91df9f230a7 100644 --- a/apps/server/src/shared/repo/role/role.repo.integration.spec.ts +++ b/apps/server/src/shared/repo/role/role.repo.integration.spec.ts @@ -157,9 +157,9 @@ describe('role repo', () => { await em.persistAndFlush([roleA, roleB]); const firstResult = await repo.findByName(roleA.name); - expect(firstResult).toEqual(roleA.name); + expect(firstResult).toEqual(roleA); const secondResult = await repo.findByNames([roleA.name]); - expect(secondResult).toEqual([roleA.name]); + expect(secondResult).toEqual([roleA]); }); }); });