Merge pull request #263 from Hexastack/hotfix/clear-setting-cache

fix: clear setting cache once seeded
This commit is contained in:
Med Marrouchi 2024-10-23 08:30:09 +01:00 committed by GitHub
commit 28ff1b474a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 3 deletions

View File

@ -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). * 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 { BaseSchema } from '@/utils/generics/base-schema';
import { BaseSeeder } from '@/utils/generics/base-seeder'; import { BaseSeeder } from '@/utils/generics/base-seeder';
@ -16,7 +19,10 @@ import { Setting } from '../schemas/setting.schema';
@Injectable() @Injectable()
export class SettingSeeder extends BaseSeeder<Setting> { 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); super(settingRepository);
} }
@ -35,6 +41,9 @@ export class SettingSeeder extends BaseSeeder<Setting> {
if ((await this.repository.count({ group })) === 0) if ((await this.repository.count({ group })) === 0)
await this.repository.createMany(models); await this.repository.createMany(models);
}); });
await this.cacheManager.del(SETTING_CACHE_KEY);
return true; return true;
} }
} }

View File

@ -105,13 +105,20 @@ export class SettingService extends BaseService<Setting> {
return config; return config;
} }
/**
* Clears the settings cache
*/
async clearCache() {
this.cacheManager.del(SETTING_CACHE_KEY);
}
/** /**
* Event handler for setting updates. Listens to 'hook:setting:*' events * Event handler for setting updates. Listens to 'hook:setting:*' events
* and invalidates the cache for settings when triggered. * and invalidates the cache for settings when triggered.
*/ */
@OnEvent('hook:setting:*') @OnEvent('hook:setting:*')
async handleSettingUpdateEvent() { async handleSettingUpdateEvent() {
this.cacheManager.del(SETTING_CACHE_KEY); this.clearCache();
} }
/** /**