-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added caching for the jobs controller
- Loading branch information
1 parent
c081558
commit 0fa9858
Showing
8 changed files
with
185 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { CachingService } from './services/cache.service'; | ||
import { ConfigModule } from '../config/config.module'; | ||
import { ConfigService } from '../config/config/config.service'; | ||
import { CacheModule } from '@nestjs/cache-manager'; | ||
|
||
@Module({ | ||
imports: [ | ||
ConfigModule, | ||
CacheModule.registerAsync({ | ||
imports: [ConfigModule], | ||
useFactory: async (configService: ConfigService) => ({ | ||
ttl: configService.get('cache.ttl'), | ||
max: configService.get('cache.max'), | ||
}), | ||
inject: [ConfigService], | ||
}), | ||
], | ||
providers: [CachingService], | ||
exports: [CachingService], | ||
}) | ||
export class CachingModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Inject, Injectable, Logger } from '@nestjs/common'; | ||
import { CACHE_MANAGER } from '@nestjs/cache-manager'; | ||
import { Cache } from 'cache-manager'; | ||
|
||
@Injectable() | ||
export class CachingService { | ||
private logger = new Logger('CachingService'); | ||
|
||
constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {} | ||
|
||
/** | ||
* Retrieve an element from the cache. If the element could not be found or an error occured, it will return undefined. | ||
* @param key - Key to use for storing the results | ||
*/ | ||
public async checkCache<T>(key: string): Promise<T | undefined> { | ||
try { | ||
this.logger.debug(`Checking cache for key: ${key}`); | ||
const result = await this.cacheManager.get<T>(key); | ||
if (result) { | ||
this.logger.debug(`Found result in cache`); | ||
} else { | ||
this.logger.warn('Could not find entry in cache'); | ||
} | ||
return result; | ||
} catch (error) { | ||
this.logger.error(`Could not query cache: ${JSON.stringify(error)}`); | ||
return undefined; | ||
} | ||
} | ||
|
||
/** | ||
* Store a result into the cache of the application | ||
* @param key - Key used to store the value in the cache. | ||
* @param value - Value to store in the cache. | ||
*/ | ||
public async store(key: string, value: any) { | ||
try { | ||
this.logger.debug(`Storing body cache for key: ${key}`); | ||
await this.cacheManager.set(key, value); | ||
} catch (error) { | ||
this.logger.error( | ||
`Could not store value in cache cache: ${JSON.stringify(error)}`, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters