60 lines
2.0 KiB
JavaScript
60 lines
2.0 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.serializePoolsToHash = serializePoolsToHash;
|
|
exports.parsePoolsFromHash = parsePoolsFromHash;
|
|
exports.setPoolCache = setPoolCache;
|
|
exports.getPoolCache = getPoolCache;
|
|
exports.incrementPoolCache = incrementPoolCache;
|
|
const keys_1 = require("./keys");
|
|
const client_1 = require("./client");
|
|
const POOL_TTL_SECONDS = 86400;
|
|
function serializePoolsToHash(pools) {
|
|
return {
|
|
short: String(pools.short),
|
|
long: String(pools.long),
|
|
};
|
|
}
|
|
function parsePoolsFromHash(hash) {
|
|
if (hash.short == null && hash.long == null)
|
|
return null;
|
|
return {
|
|
short: Number(hash.short ?? 0),
|
|
long: Number(hash.long ?? 0),
|
|
};
|
|
}
|
|
async function setPoolCache(intervalMinutes, openMs, pools) {
|
|
const redis = (0, client_1.getRedisClient)();
|
|
if (!redis)
|
|
return;
|
|
const key = keys_1.redisKeys.pools(intervalMinutes, openMs);
|
|
await redis.hset(key, serializePoolsToHash(pools));
|
|
await redis.expire(key, POOL_TTL_SECONDS);
|
|
}
|
|
async function getPoolCache(intervalMinutes, openMs) {
|
|
const redis = (0, client_1.getRedisClient)();
|
|
if (!redis)
|
|
return null;
|
|
try {
|
|
const key = keys_1.redisKeys.pools(intervalMinutes, openMs);
|
|
const hash = await redis.hgetall(key);
|
|
if (!hash || !Object.keys(hash).length)
|
|
return null;
|
|
return parsePoolsFromHash(hash);
|
|
}
|
|
catch (e) {
|
|
console.error('[poolCache] getPoolCache failed:', e);
|
|
return null;
|
|
}
|
|
}
|
|
async function incrementPoolCache(intervalMinutes, openMs, delta) {
|
|
const redis = (0, client_1.getRedisClient)();
|
|
if (!redis)
|
|
return;
|
|
const key = keys_1.redisKeys.pools(intervalMinutes, openMs);
|
|
if (delta.short)
|
|
await redis.hincrbyfloat(key, 'short', delta.short);
|
|
if (delta.long)
|
|
await redis.hincrbyfloat(key, 'long', delta.long);
|
|
await redis.expire(key, POOL_TTL_SECONDS);
|
|
}
|
|
//# sourceMappingURL=poolCache.js.map
|