Skip to content

Commit

Permalink
Feat: Add some more cached methods (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
RikudouSage authored Oct 8, 2023
1 parent 9a86518 commit 39cca52
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class MyEndorsementsComponent implements OnInit {
this.titleService.title = this.translator.get('app.endorsements.my');

const responses = await Promise.all([
toPromise(this.api.getEndorsementsForInstance(this.authManager.currentInstanceSnapshot.name)),
toPromise(this.cachedApi.getEndorsementsForInstance(this.authManager.currentInstanceSnapshot.name)),
toPromise(this.cachedApi.getEndorsementsByInstances([this.authManager.currentInstanceSnapshot.name])),
toPromise(this.cachedApi.getCurrentInstanceInfo()),
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ export class InstanceDetailComponent implements OnInit {
const responses = await Promise.all([
toPromise(this.api.getCensuresForInstance(instanceDomain)),
toPromise(this.cachedApi.getCensuresByInstances([instanceDomain])),
toPromise(this.api.getEndorsementsForInstance(instanceDomain)),
toPromise(this.cachedApi.getEndorsementsForInstance(instanceDomain)),
toPromise(this.cachedApi.getEndorsementsByInstances([instanceDomain])),
toPromise(this.cachedApi.getGuaranteesByInstance(instanceDomain)),
toPromise(this.api.getInstanceInfo(instanceDomain)),
toPromise(this.cachedApi.getInstanceInfo(instanceDomain)),
toPromise(this.api.getHesitationsForInstance(instanceDomain)),
toPromise(this.cachedApi.getHesitationsByInstances([instanceDomain])),
]);
Expand Down
32 changes: 32 additions & 0 deletions src/app/services/cached-fediseer-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ export class CachedFediseerApiService {
);
}

public getInstanceInfo(instance: string, cacheConfig: CacheConfiguration = {}): Observable<ApiResponse<InstanceDetailResponse>> {
cacheConfig.type ??= CacheType.Permanent;
cacheConfig.ttl ??= 60;

const cacheKey = `api.instance_info.${cacheConfig.ttl}.${instance}`;

const item = this.getCacheItem<InstanceDetailResponse>(cacheKey, cacheConfig)!;
if (item.isHit && !cacheConfig.clear) {
return this.getSuccessResponse(item);
}

return this.api.getInstanceInfo(instance).pipe(
tap (this.storeResponse(item, cacheConfig)),
);
}

public getCensuresByInstances(instances: string[], cacheConfig: CacheConfiguration = {}): Observable<ApiResponse<InstanceListResponse<InstanceDetailResponse>>> {
cacheConfig.type ??= CacheType.Permanent;
cacheConfig.ttl ??= 60;
Expand Down Expand Up @@ -98,6 +114,22 @@ export class CachedFediseerApiService {
);
}

public getEndorsementsForInstance(instance: string, cacheConfig: CacheConfiguration = {}): Observable<ApiResponse<InstanceListResponse<InstanceDetailResponse>>> {
cacheConfig.type ??= CacheType.Permanent;
cacheConfig.ttl ??= 180;

const cacheKey = `api.endorsements_for_instance${cacheConfig.ttl}.${instance}`;

const item = this.getCacheItem<InstanceListResponse<InstanceDetailResponse>>(cacheKey, cacheConfig)!;
if (item.isHit && !cacheConfig.clear) {
return this.getSuccessResponse(item);
}

return this.api.getEndorsementsForInstance(instance).pipe(
tap(this.storeResponse(item, cacheConfig)),
);
}

public getEndorsementsByInstances(instances: string[], cacheConfig: CacheConfiguration = {}): Observable<ApiResponse<InstanceListResponse<InstanceDetailResponse>>> {
cacheConfig.type ??= CacheType.Permanent;
cacheConfig.ttl ??= 60;
Expand Down

0 comments on commit 39cca52

Please sign in to comment.