-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcacheable-members.provider.ts
32 lines (28 loc) · 1.12 KB
/
cacheable-members.provider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { CACHE_MANAGER, Inject, Injectable, Logger } from '@nestjs/common';
import { RetryablePioneerClient } from 'src/gql/pioneer.client';
import { Cache } from 'cache-manager';
import { MembersByHandlesQuery } from 'src/qntypes';
import {createHash as hash} from 'crypto';
@Injectable()
export class CacheableMembershipsProvider {
private readonly logger = new Logger(CacheableMembershipsProvider.name);
constructor(
protected readonly pioneerClient: RetryablePioneerClient,
@Inject(CACHE_MANAGER) private cacheManager: Cache
) { }
async getMembers(handles: string[]): Promise<MembersByHandlesQuery> {
const cacheKey = this.buildCacheKey(handles);
const cached: MembersByHandlesQuery | undefined = await this.cacheManager.get(cacheKey);
if (cached) { // hit
return cached;
} else { // miss
this.logger.debug('Cache miss');
const qnResult = await this.pioneerClient.membersByHandles(handles);
this.cacheManager.set(cacheKey, qnResult);
return qnResult;
}
}
buildCacheKey(handles: string[]): string {
return hash('sha1').update(handles.join(',')).digest('hex');
}
}