From 629d3efb082abd5cfe30202922197a80c244bc4e Mon Sep 17 00:00:00 2001 From: argirok Date: Mon, 5 Feb 2024 12:08:34 +0200 Subject: [PATCH] [ develop | DONE | ADDED] Cache services: add initialization method to initiate grouped requests from utils service. Init after clear and when cache starts --- services/cache/mecache/beta-properties.file | 1 + services/cache/mecache/cache.js | 15 ++++++++++++++- services/cache/mecache/package.json | 1 + services/cache/mecache/production-properties.file | 1 + services/cache/mecache/properties.file | 1 + 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/services/cache/mecache/beta-properties.file b/services/cache/mecache/beta-properties.file index 471fad82..d39459e8 100644 --- a/services/cache/mecache/beta-properties.file +++ b/services/cache/mecache/beta-properties.file @@ -1 +1,2 @@ port = 4000 +utilsService = https://beta.explore.openaire.eu/utils-service diff --git a/services/cache/mecache/cache.js b/services/cache/mecache/cache.js index 73d1e472..11710b4e 100644 --- a/services/cache/mecache/cache.js +++ b/services/cache/mecache/cache.js @@ -1,5 +1,5 @@ 'use strict'; - +const axios = require('axios'); let express = require('express'); let app = express(); let mcache = require('memory-cache'); @@ -142,6 +142,7 @@ const server = app.listen(properties.get('port'), function () { console.log(`Example app listening on port`, server.address().port) //run the timer resetAtMidnight(); + initCache(); }); function getResponse(code, message) { @@ -155,6 +156,18 @@ function clearCache() { console.log("cache is cleared!"); mcache.clear(); entries.set(mcache.size()); + initCache(); +} + +async function initCache() { + try { + const requests = await axios.get(properties.get('utilsService') + '/grouped-requests'); + const additionalDataPromises = requests.data.map((url) => axios.get('http://localhost:'+properties.get('port') + '/get?url=' + properties.get('utilsService') + url)); + const additionalDataResponses = await Promise.all(additionalDataPromises); + console.log("Cache initialized!") + } catch (error) { + console.error('Error fetching data: Cache initialize failed', error.message); + } } function checkForLongCachedRequests(url) { diff --git a/services/cache/mecache/package.json b/services/cache/mecache/package.json index e7530a97..1118af1a 100644 --- a/services/cache/mecache/package.json +++ b/services/cache/mecache/package.json @@ -10,6 +10,7 @@ "prepare-prod": " npm run prepare-dist; cp production-properties.file ./dist/properties.file" }, "dependencies": { + "axios": "^1.6.7", "cors": "^2.8.5", "express": "^4.18.2", "memory-cache": "^0.2.0", diff --git a/services/cache/mecache/production-properties.file b/services/cache/mecache/production-properties.file index 471fad82..40248189 100644 --- a/services/cache/mecache/production-properties.file +++ b/services/cache/mecache/production-properties.file @@ -1 +1,2 @@ port = 4000 +utilsService = https://explore.openaire.eu/utils-service diff --git a/services/cache/mecache/properties.file b/services/cache/mecache/properties.file index 74f6260b..6734892a 100644 --- a/services/cache/mecache/properties.file +++ b/services/cache/mecache/properties.file @@ -1 +1,2 @@ port = 3200 +utilsService= http://scoobydoo.di.uoa.gr:8000