mirror of
https://github.com/hyperdxio/hyperdx
synced 2026-04-21 13:37:15 +00:00
1. introduce a caching abstraction SimpleCache 2. add time range filtering to getMetricsTags query
45 lines
1 KiB
TypeScript
45 lines
1 KiB
TypeScript
import { createClient } from 'redis';
|
|
import { serializeError } from 'serialize-error';
|
|
|
|
import * as config from '@/config';
|
|
import logger from '@/utils/logger';
|
|
|
|
const client = createClient({
|
|
url: config.REDIS_URL,
|
|
});
|
|
|
|
client.on('error', (err: any) => {
|
|
logger.error('Redis error: ', serializeError(err));
|
|
});
|
|
|
|
class SimpleCache<T> {
|
|
constructor(
|
|
private readonly key: string,
|
|
private readonly ttlInMs: number,
|
|
private readonly fetcher: () => Promise<T>,
|
|
) {}
|
|
|
|
async get(): Promise<T> {
|
|
const cached = await client.get(this.key);
|
|
if (cached != null) {
|
|
logger.info({
|
|
message: 'SimpleCache: cache hit',
|
|
key: this.key,
|
|
});
|
|
return JSON.parse(cached);
|
|
}
|
|
logger.info({
|
|
message: 'SimpleCache: cache miss',
|
|
key: this.key,
|
|
});
|
|
const result = await this.fetcher();
|
|
await client.set(this.key, JSON.stringify(result), {
|
|
PX: this.ttlInMs,
|
|
});
|
|
return result;
|
|
}
|
|
}
|
|
|
|
export default client;
|
|
|
|
export { client as redisClient, SimpleCache };
|