Use Map for caches instead of objects

This commit is contained in:
Cohee
2024-04-23 16:15:54 +03:00
parent d1f292f462
commit 75372ad0cc
2 changed files with 19 additions and 9 deletions

View File

@@ -5,7 +5,10 @@ const TASK = 'text-classification';
const router = express.Router();
const cacheObject = {};
/**
* @type {Map<string, object>} Cache for classification results
*/
const cacheObject = new Map();
router.post('/labels', jsonParser, async (req, res) => {
try {
@@ -23,15 +26,20 @@ router.post('/', jsonParser, async (req, res) => {
try {
const { text } = req.body;
/**
* Get classification result for a given text
* @param {string} text Text to classify
* @returns {Promise<object>} Classification result
*/
async function getResult(text) {
if (Object.hasOwn(cacheObject, text)) {
return cacheObject[text];
if (cacheObject.has(text)) {
return cacheObject.get(text);
} else {
const module = await import('../transformers.mjs');
const pipe = await module.default.getPipeline(TASK);
const result = await pipe(text, { topk: 5 });
result.sort((a, b) => b.score - a.score);
cacheObject[text] = result;
cacheObject.set(text, result);
return result;
}
}