mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
Adds cache to nginx
This commit is contained in:
parent
878fd302a4
commit
c45a674c72
2 changed files with 79 additions and 34 deletions
|
|
@ -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;
|
||||||
|
|
|
||||||
|
|
@ -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,16 +86,12 @@ 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) {
|
||||||
r.error(`WebSocket routing error: ${error}`);
|
r.error(`WebSocket routing error: ${error}`);
|
||||||
r.return(500, 'Internal routing error');
|
r.return(500, 'Internal routing error');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
export default { routeWebSocket };
|
|
||||||
Loading…
Reference in a new issue