Skip to content

Commit

Permalink
Merge pull request #263 from Hexastack/hotfix/clear-setting-cache
Browse files Browse the repository at this point in the history
fix: clear setting cache once seeded
  • Loading branch information
marrouchi authored Oct 23, 2024
2 parents 16c4852 + 5524d3c commit 28ff1b4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
13 changes: 11 additions & 2 deletions api/src/setting/seeds/setting.seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/

import { Injectable } from '@nestjs/common';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Inject, Injectable } from '@nestjs/common';
import { Cache } from 'cache-manager';

import { SETTING_CACHE_KEY } from '@/utils/constants/cache';
import { BaseSchema } from '@/utils/generics/base-schema';
import { BaseSeeder } from '@/utils/generics/base-seeder';

Expand All @@ -16,7 +19,10 @@ import { Setting } from '../schemas/setting.schema';

@Injectable()
export class SettingSeeder extends BaseSeeder<Setting> {
constructor(private readonly settingRepository: SettingRepository) {
constructor(
private readonly settingRepository: SettingRepository,
@Inject(CACHE_MANAGER) private readonly cacheManager: Cache,
) {
super(settingRepository);
}

Expand All @@ -35,6 +41,9 @@ export class SettingSeeder extends BaseSeeder<Setting> {
if ((await this.repository.count({ group })) === 0)
await this.repository.createMany(models);
});

await this.cacheManager.del(SETTING_CACHE_KEY);

return true;
}
}
9 changes: 8 additions & 1 deletion api/src/setting/services/setting.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,20 @@ export class SettingService extends BaseService<Setting> {
return config;
}

/**
* Clears the settings cache
*/
async clearCache() {
this.cacheManager.del(SETTING_CACHE_KEY);
}

/**
* Event handler for setting updates. Listens to 'hook:setting:*' events
* and invalidates the cache for settings when triggered.
*/
@OnEvent('hook:setting:*')
async handleSettingUpdateEvent() {
this.cacheManager.del(SETTING_CACHE_KEY);
this.clearCache();
}

/**
Expand Down

0 comments on commit 28ff1b4

Please sign in to comment.