Skip to content

Commit

Permalink
cache curries to not repeat calls for concepts (#4640)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Chang <mwdchang@gmail.com>
  • Loading branch information
Tom-Szendrey and mwdchang authored Sep 3, 2024
1 parent e45da22 commit fcf0d10
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions packages/client/hmi-client/src/services/concept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { Curies, DatasetColumn, DKG, EntitySimilarityResult, State } from '
import { logger } from '@/utils/logger';
import { isEmpty } from 'lodash';
import { CalibrateMap } from '@/services/calibrate-workflow';
import { FIFOCache } from '@/utils/FifoCache';

interface Entity {
id: string;
Expand All @@ -20,14 +21,29 @@ interface EntityMap {

const curieNameCache = new Map<string, string>();

const currieEntityCache = new FIFOCache<Promise<{ data: any; status: number }>>(400);
/**
* Get DKG entities, either single ones or multiple at a time
*/
async function getCuriesEntities(curies: Array<string>): Promise<Array<DKG> | null> {
const response = await API.get(`/mira/currie/${curies.toString()}`);
if (response?.status === 200 && response?.data) return response.data;
if (response?.status === 204) console.warn('No DKG entities found for curies:', curies);
return null;
try {
const cacheKey = curies.toString();

let promise = currieEntityCache.get(cacheKey);
if (!promise) {
promise = API.get(`/mira/currie/${curies.toString()}`).then((res) => ({ data: res.data, status: res.status }));
currieEntityCache.set(cacheKey, promise);
}

// If a rename function is defined, loop over the first row
const response = await promise;
if (response?.status === 200 && response?.data) return response.data;
if (response?.status === 204) console.warn('No DKG entities found for curies:', curies);
return null;
} catch (err) {
logger.error(err);
return null;
}
}

async function searchCuriesEntities(query: string): Promise<Array<DKG>> {
Expand Down

0 comments on commit fcf0d10

Please sign in to comment.