mirror of
https://github.com/benjamin-wilson/public-pool.git
synced 2026-09-29 09:05:06 -07:00
Restore in-memory API cache priority
This commit is contained in:
+34
-13
@@ -37,7 +37,7 @@ export class AppController {
|
|||||||
|
|
||||||
|
|
||||||
const CACHE_KEY = 'SITE_INFO';
|
const CACHE_KEY = 'SITE_INFO';
|
||||||
const cachedResult = await this.getCached(CACHE_KEY);
|
const cachedResult = await this.getCached(CACHE_KEY, 5 * 60 * 1000);
|
||||||
|
|
||||||
if (cachedResult != null) {
|
if (cachedResult != null) {
|
||||||
return cachedResult;
|
return cachedResult;
|
||||||
@@ -108,7 +108,7 @@ export class AppController {
|
|||||||
@Get('info/accounting')
|
@Get('info/accounting')
|
||||||
public async infoAccounting() {
|
public async infoAccounting() {
|
||||||
const CACHE_KEY = 'SITE_ACCOUNTING';
|
const CACHE_KEY = 'SITE_ACCOUNTING';
|
||||||
const cachedResult = await this.getCached(CACHE_KEY);
|
const cachedResult = await this.getCached(CACHE_KEY, 15 * 1000);
|
||||||
|
|
||||||
if (cachedResult != null) {
|
if (cachedResult != null) {
|
||||||
return cachedResult;
|
return cachedResult;
|
||||||
@@ -126,7 +126,7 @@ export class AppController {
|
|||||||
public async pool() {
|
public async pool() {
|
||||||
|
|
||||||
const CACHE_KEY = 'POOL_INFO';
|
const CACHE_KEY = 'POOL_INFO';
|
||||||
const cachedResult = await this.getCached(CACHE_KEY);
|
const cachedResult = await this.getCached(CACHE_KEY, 15 * 1000);
|
||||||
|
|
||||||
if (cachedResult != null) {
|
if (cachedResult != null) {
|
||||||
return cachedResult;
|
return cachedResult;
|
||||||
@@ -163,7 +163,7 @@ export class AppController {
|
|||||||
|
|
||||||
|
|
||||||
const CACHE_KEY = 'SITE_HASHRATE_GRAPH';
|
const CACHE_KEY = 'SITE_HASHRATE_GRAPH';
|
||||||
const cachedResult = await this.getCached(CACHE_KEY);
|
const cachedResult = await this.getCached(CACHE_KEY, 10 * 60 * 1000);
|
||||||
|
|
||||||
if (cachedResult != null) {
|
if (cachedResult != null) {
|
||||||
return cachedResult;
|
return cachedResult;
|
||||||
@@ -179,25 +179,32 @@ export class AppController {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getCached<T>(key: string): Promise<T | null> {
|
private async getCached<T>(key: string, localTtlMs: number): Promise<T | null> {
|
||||||
const shared = await this.redisMessagingService.getJsonCache<T>(`api:${key}`).catch(error => {
|
const local = await this.cacheManager.get<T>(key);
|
||||||
|
if (local != null) {
|
||||||
|
return local;
|
||||||
|
}
|
||||||
|
|
||||||
|
const shared = await this.withSecondaryCacheTimeout(
|
||||||
|
this.redisMessagingService.getJsonCache<T>(`api:${key}`).catch(error => {
|
||||||
console.error(`Shared API cache read failed for ${key}: ${error.message}`);
|
console.error(`Shared API cache read failed for ${key}: ${error.message}`);
|
||||||
return null;
|
return null;
|
||||||
});
|
}),
|
||||||
|
100
|
||||||
|
);
|
||||||
if (shared != null) {
|
if (shared != null) {
|
||||||
|
await this.cacheManager.set(key, shared, localTtlMs);
|
||||||
return shared;
|
return shared;
|
||||||
}
|
}
|
||||||
|
|
||||||
return await this.cacheManager.get<T>(key) ?? null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async setCached(key: string, value: unknown, ttlMs: number): Promise<void> {
|
private async setCached(key: string, value: unknown, ttlMs: number): Promise<void> {
|
||||||
await Promise.all([
|
await this.cacheManager.set(key, value, ttlMs);
|
||||||
this.cacheManager.set(key, value, ttlMs),
|
void this.redisMessagingService.setJsonCache(`api:${key}`, value, ttlMs).catch(error => {
|
||||||
this.redisMessagingService.setJsonCache(`api:${key}`, value, ttlMs).catch(error => {
|
|
||||||
console.error(`Shared API cache write failed for ${key}: ${error.message}`);
|
console.error(`Shared API cache write failed for ${key}: ${error.message}`);
|
||||||
}),
|
});
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async withTimeout<T>(
|
private async withTimeout<T>(
|
||||||
@@ -224,4 +231,18 @@ export class AppController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async withSecondaryCacheTimeout<T>(promise: Promise<T | null>, timeoutMs: number): Promise<T | null> {
|
||||||
|
let timeout: NodeJS.Timeout;
|
||||||
|
const timeoutPromise = new Promise<null>(resolve => {
|
||||||
|
timeout = setTimeout(() => resolve(null), timeoutMs);
|
||||||
|
timeout.unref?.();
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
return await Promise.race([promise, timeoutPromise]);
|
||||||
|
} finally {
|
||||||
|
clearTimeout(timeout);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user