Skip to content

Commit

Permalink
Feat: Add cache for reasons (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
RikudouSage authored Sep 25, 2023
1 parent 9411f5d commit a52d158
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class CensureInstanceComponent implements OnInit {

this.form.patchValue({instance: query['instance']});
});
let availableReasons = await toPromise(this.api.getUsedReasons());
let availableReasons = await toPromise(this.cachedApi.getUsedReasons());
if (availableReasons === null) {
this.messageService.createWarning(`Couldn't get list of reasons you've used previously, autocompletion won't work.`);
availableReasons = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class EditCensureReasonsComponent implements OnInit {

this.activatedRoute.params.subscribe(async params => {
const targetInstance = params['instance'] as string;
let availableReasons = await toPromise(this.api.getUsedReasons());
let availableReasons = await toPromise(this.cachedApi.getUsedReasons());
if (availableReasons === null) {
this.messageService.createWarning(`Couldn't get list of reasons that were used previously, autocompletion won't work.`);
availableReasons = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class EditEndorsementReasonsComponent implements OnInit {

this.activatedRoute.params.subscribe(async params => {
const targetInstance = params['instance'] as string;
let availableReasons = await toPromise(this.api.usedEndorsementReasons);
let availableReasons = await toPromise(this.cachedApi.getUsedEndorsementReasons());
if (availableReasons === null) {
this.messageService.createWarning(this.translator.get('error.reasons.autocompletion.fetch'));
availableReasons = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class EndorseInstanceComponent implements OnInit {
public async ngOnInit(): Promise<void> {
this.titleService.title = this.translator.get('app.endorsements.add.title');

const reasons = await toPromise(this.api.usedEndorsementReasons);
const reasons = await toPromise(this.cachedApi.getUsedEndorsementReasons());
if (reasons === null) {
this.messageService.createWarning(this.translator.get('error.reasons.autocompletion.fetch'));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class EditHesitationReasonsComponent {

this.activatedRoute.params.subscribe(async params => {
const targetInstance = params['instance'] as string;
let availableReasons = await toPromise(this.api.getUsedReasons());
let availableReasons = await toPromise(this.cachedApi.getUsedReasons());
if (availableReasons === null) {
this.messageService.createWarning(`Couldn't get list of reasons that were used previously, autocompletion won't work.`);
availableReasons = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class HesitateOnInstanceComponent implements OnInit {

this.form.patchValue({instance: query['instance']});
});
let availableReasons = await toPromise(this.api.getUsedReasons());
let availableReasons = await toPromise(this.cachedApi.getUsedReasons());
if (availableReasons === null) {
this.messageService.createWarning(`Couldn't get list of reasons you've used previously, autocompletion won't work.`);
availableReasons = [];
Expand Down
50 changes: 50 additions & 0 deletions src/app/services/cached-fediseer-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,56 @@ export class CachedFediseerApiService {
);
}

public getUsedEndorsementReasons(cacheConfig: CacheConfiguration = {}): Observable<string[] | null> {
cacheConfig.type ??= CacheType.Permanent;
cacheConfig.ttl ??= 300;

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

const item = this.getCacheItem<string[] | null>(cacheKey, cacheConfig)!;
if (item.isHit && !cacheConfig.clear) {
return of(item.value!);
}

return this.api.getUsedEndorsementReasons().pipe(
tap(result => {
item.value = result;
if (item.value === null) {
return;
}
if (cacheConfig.ttl! >= 0) {
item.expiresAt = new Date(new Date().getTime() + (cacheConfig.ttl! * 1_000));
}
this.saveCacheItem(item, cacheConfig);
}),
);
}

public getUsedReasons(instances: string[] = [], cacheConfig: CacheConfiguration = {}): Observable<string[] | null> {
cacheConfig.type ??= CacheType.Permanent;
cacheConfig.ttl ??= 300;

const cacheKey = `api.reasons${cacheConfig.ttl}.${instances.join('_')}`;

const item = this.getCacheItem<string[] | null>(cacheKey, cacheConfig)!;
if (item.isHit && !cacheConfig.clear) {
return of(item.value!);
}

return this.api.getUsedReasons(instances).pipe(
tap(result => {
item.value = result;
if (item.value === null) {
return;
}
if (cacheConfig.ttl! >= 0) {
item.expiresAt = new Date(new Date().getTime() + (cacheConfig.ttl! * 1_000));
}
this.saveCacheItem(item, cacheConfig);
}),
);
}

public clearCache(): void {
this.runtimeCache.clear();
this.permanentCache.clear();
Expand Down
2 changes: 1 addition & 1 deletion src/app/services/fediseer-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export class FediseerApiService {
);
}

public get usedEndorsementReasons(): Observable<string[] | null> {
public getUsedEndorsementReasons(): Observable<string[] | null> {
const cacheItem = this.runtimeCache.getItem<string[]>(`used_endorsement_reasons`);
if (cacheItem.isHit) {
return of(cacheItem.value!);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export class FilterFormComponent<TSettings extends SynchronizationSettings> impl
private async loadReasons() {
if (this.availableReasons === null) {
this.loadingReasons = true;
const reasons = await toPromise(this.api.getUsedReasons());
const reasons = await toPromise(this.cachedApi.getUsedReasons());
if (reasons === null) {
this.messageService.createError('Failed getting list of reasons from the server');
}
Expand Down

0 comments on commit a52d158

Please sign in to comment.