Adds cache to nginx

This commit is contained in:
Gabriel Matte 2024-11-25 20:40:48 -05:00
parent 878fd302a4
commit c45a674c72
2 changed files with 79 additions and 34 deletions

View file

@ -1,4 +1,6 @@
js_shared_dict_zone zone=cache:10m;
js_import njs/main.js; js_import njs/main.js;
js_set $cache_dict main.get_cache_dict;
map $http_upgrade $connection_upgrade { map $http_upgrade $connection_upgrade {
default upgrade; default upgrade;

View file

@ -1,6 +1,50 @@
async function fetchRoomInfo(r) { function get_cache_dict(r) {
return '';
}
function getCachedData(r, key) {
try { try {
// Make request to API to get room info const cached = ngx.shared.cache.get(key);
if (cached) {
const data = JSON.parse(cached);
const now = Date.now();
// 2 minutes cache - let game rooms rotate
if (now - data.timestamp < 120000) {
r.error(`Debug: Cache hit for ${key}, age: ${(now - data.timestamp)/1000}s`);
return data.value;
}
r.error(`Debug: Cache expired for ${key}, age: ${(now - data.timestamp)/1000}s`);
}
return null;
} catch (error) {
r.error(`Cache read error: ${error}`);
return null;
}
}
function setCachedData(r, key, value) {
try {
const data = {
timestamp: Date.now(),
value: value
};
ngx.shared.cache.set(key, JSON.stringify(data));
r.error(`Debug: Cached ${key}`);
} catch (error) {
r.error(`Cache write error: ${error}`);
}
}
async function fetchRoomInfo(r) {
const cacheKey = `room:${r.variables.room_id}`;
try {
const cachedRoom = getCachedData(r, cacheKey);
if (cachedRoom) {
r.error(`Debug: Room info from cache: ${JSON.stringify(cachedRoom)}`);
return cachedRoom;
}
let res = await r.subrequest('/api/room/' + r.variables.room_id, { let res = await r.subrequest('/api/room/' + r.variables.room_id, {
method: 'GET' method: 'GET'
}); });
@ -11,15 +55,19 @@ async function fetchRoomInfo(r) {
} }
let room = JSON.parse(res.responseText); let room = JSON.parse(res.responseText);
r.error(`Debug: Room info: ${JSON.stringify(room)}`); // Debug log setCachedData(r, cacheKey, room);
r.error(`Debug: Room info fetched and cached: ${JSON.stringify(room)}`);
return room; return room;
} catch (error) { } catch (error) {
r.error(`Error fetching room info: ${error}`); r.error(`Error fetching/caching room info: ${error}`);
return null; return null;
} }
} }
async function routeWebSocket(r) { export default {
get_cache_dict,
routeWebSocket: async function(r) {
try { try {
const roomInfo = await fetchRoomInfo(r); const roomInfo = await fetchRoomInfo(r);
@ -29,7 +77,6 @@ async function routeWebSocket(r) {
return; return;
} }
// Make sure the host includes protocol if not already present
let proxyUrl = roomInfo.host; let proxyUrl = roomInfo.host;
if (!proxyUrl.startsWith('http://') && !proxyUrl.startsWith('https://')) { if (!proxyUrl.startsWith('http://') && !proxyUrl.startsWith('https://')) {
proxyUrl = 'http://' + proxyUrl; proxyUrl = 'http://' + proxyUrl;
@ -39,10 +86,7 @@ async function routeWebSocket(r) {
r.error(`Debug: Setting proxy target to: ${proxyUrl}`); r.error(`Debug: Setting proxy target to: ${proxyUrl}`);
r.error(`Debug: Headers: ${JSON.stringify(r.headersIn)}`); r.error(`Debug: Headers: ${JSON.stringify(r.headersIn)}`);
// Set the proxy target variable
r.variables.proxy_target = proxyUrl; r.variables.proxy_target = proxyUrl;
// Redirect to the websocket proxy
r.internalRedirect('@websocket_proxy'); r.internalRedirect('@websocket_proxy');
} catch (error) { } catch (error) {
@ -50,5 +94,4 @@ async function routeWebSocket(r) {
r.return(500, 'Internal routing error'); r.return(500, 'Internal routing error');
} }
} }
};
export default { routeWebSocket };