-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db659f1
commit 6c5c83b
Showing
6 changed files
with
90 additions
and
81 deletions.
There are no files selected for viewing
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
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,61 @@ | ||
import { | ||
CacheService, | ||
ICacheService, | ||
} from '@/datasources/cache/cache.service.interface'; | ||
import { CacheDir } from '@/datasources/cache/entities/cache-dir.entity'; | ||
import { ILoggingService, LoggingService } from '@/logging/logging.interface'; | ||
import { asError } from '@/logging/utils'; | ||
import { | ||
Inject, | ||
Injectable, | ||
InternalServerErrorException, | ||
} from '@nestjs/common'; | ||
import postgres from 'postgres'; | ||
|
||
@Injectable() | ||
// TODO: add/implement interface | ||
export class CachedQueryResolver { | ||
constructor( | ||
@Inject(LoggingService) private readonly loggingService: ILoggingService, | ||
@Inject(CacheService) private readonly cacheService: ICacheService, | ||
) {} | ||
|
||
/** | ||
* Returns the content from cache or executes the query and caches the result. | ||
* If the specified {@link CacheDir} is empty, the query is executed and the result is cached. | ||
* If the specified {@link CacheDir} is not empty, the pointed content is returned. | ||
* | ||
* @param cacheDir {@link CacheDir} to use for caching | ||
* @param query query to execute | ||
* @param ttl time to live for the cache | ||
* @returns content from cache or query result | ||
*/ | ||
async get<T extends postgres.MaybeRow[]>(args: { | ||
cacheDir: CacheDir; | ||
query: postgres.PendingQuery<T>; | ||
ttl: number; | ||
}): Promise<T> { | ||
const { key, field } = args.cacheDir; | ||
const cached = await this.cacheService.get(args.cacheDir); | ||
if (cached != null) { | ||
this.loggingService.debug({ type: 'cache_hit', key, field }); | ||
return JSON.parse(cached); | ||
} | ||
this.loggingService.debug({ type: 'cache_miss', key, field }); | ||
|
||
// log & hide database errors | ||
const result = await args.query.catch((e) => { | ||
this.loggingService.error(asError(e).message); | ||
throw new InternalServerErrorException(); | ||
}); | ||
|
||
if (result.count > 0) { | ||
await this.cacheService.set( | ||
args.cacheDir, | ||
JSON.stringify(result), | ||
args.ttl, | ||
); | ||
} | ||
return result; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.