Skip to content

Commit

Permalink
BC-8354 - adjust roles cache to use different tokens for different op…
Browse files Browse the repository at this point in the history
…erations (#5322)

in the roles repo, findByNames and findByName used the same cache result (when only a single roleName was selected). This resulted in an error, when an array result was expected but the cache contained a single result, or when a single result was expected but cache contained an array with a single element.
  • Loading branch information
Metauriel committed Nov 6, 2024
1 parent 128159e commit 9604fef
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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('roles-cache-bynames-teacher');
});

afterAll(async () => {
Expand Down
14 changes: 14 additions & 0 deletions apps/server/src/shared/repo/role/role.repo.integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
const secondResult = await repo.findByNames([roleA.name]);
expect(secondResult).toEqual([roleA]);
});
});
});
4 changes: 2 additions & 2 deletions apps/server/src/shared/repo/role/role.repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class RoleRepo extends BaseRepo<Role> {
const promise: Promise<Role> = this._em.findOneOrFail(
Role,
{ name },
{ cache: [`roles-cache-${name}`, this.cacheExpiration] }
{ cache: [`roles-cache-byname-${name}`, this.cacheExpiration] }
);
return promise;
}
Expand All @@ -30,7 +30,7 @@ export class RoleRepo extends BaseRepo<Role> {
const promise: Promise<Role[]> = this._em.find(
Role,
{ name: { $in: names } },
{ cache: [`roles-cache-${names.join('-')}`, this.cacheExpiration] }
{ cache: [`roles-cache-bynames-${names.join('-')}`, this.cacheExpiration] }
);
return promise;
}
Expand Down

0 comments on commit 9604fef

Please sign in to comment.