signalk-parquet
Version:
Vessel data Parquet file archive with automated value and geospatial triggers. History API compliant with cloud backups and queries.
144 lines • 4.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCachedPaths = getCachedPaths;
exports.setCachedPaths = setCachedPaths;
exports.clearPathCache = clearPathCache;
exports.getPathCacheStats = getPathCacheStats;
exports.getCachedContexts = getCachedContexts;
exports.setCachedContexts = setCachedContexts;
exports.clearContextCache = clearContextCache;
exports.clearAllCaches = clearAllCaches;
exports.getAllCacheStats = getAllCacheStats;
const cache_defaults_1 = require("../config/cache-defaults");
const pathCache = new Map();
const contextCache = new Map();
/**
* Round timestamp to nearest minute for cache key generation
* This allows queries within the same minute to share cache entries
* @param dateTime - ZonedDateTime to round
* @returns ISO string rounded to the minute (e.g., "2025-11-02T10:15:00Z")
*/
function roundToMinute(dateTime) {
// Get the instant and convert to epoch milliseconds
const epochMs = dateTime.toInstant().toEpochMilli();
// Round to nearest minute (60000 ms)
const roundedMs = Math.floor(epochMs / 60000) * 60000;
// Convert back to ISO string
return new Date(roundedMs).toISOString();
}
/**
* Get cached paths for a specific context and time range
*/
function getCachedPaths(context, from, to) {
// Use rounded timestamps for cache key to improve hit rate
const key = `${context}:${roundToMinute(from)}:${roundToMinute(to)}`;
const cached = pathCache.get(key);
if (cached && Date.now() - cached.timestamp < cache_defaults_1.CACHE_TTL.PATH_CONTEXT) {
return cached.paths;
}
// Clean up expired entry
if (cached) {
pathCache.delete(key);
}
return null;
}
/**
* Cache paths for a specific context and time range
*/
function setCachedPaths(context, from, to, paths) {
// Use rounded timestamps for cache key to improve hit rate
const key = `${context}:${roundToMinute(from)}:${roundToMinute(to)}`;
pathCache.set(key, {
timeRange: { from: from.toString(), to: to.toString() },
paths,
timestamp: Date.now(),
});
// Clean up old entries if cache is too large
if (pathCache.size > cache_defaults_1.CACHE_SIZE.PATH_CONTEXT_MAX) {
const oldestKey = Array.from(pathCache.entries()).sort((a, b) => a[1].timestamp - b[1].timestamp)[0][0];
pathCache.delete(oldestKey);
}
}
/**
* Clear all cached paths
*/
function clearPathCache() {
pathCache.clear();
}
/**
* Get cache statistics for monitoring
*/
function getPathCacheStats() {
return {
size: pathCache.size,
maxSize: cache_defaults_1.CACHE_SIZE.PATH_CONTEXT_MAX,
ttlMs: cache_defaults_1.CACHE_TTL.PATH_CONTEXT,
};
}
// ============================================================================
// Context Caching Functions
// ============================================================================
/**
* Get cached contexts for a specific time range
*/
function getCachedContexts(from, to) {
// Use rounded timestamps for cache key to improve hit rate
const key = `${roundToMinute(from)}:${roundToMinute(to)}`;
const cached = contextCache.get(key);
if (cached && Date.now() - cached.timestamp < cache_defaults_1.CACHE_TTL.PATH_CONTEXT) {
return cached.contexts;
}
// Clean up expired entry
if (cached) {
contextCache.delete(key);
}
return null;
}
/**
* Cache contexts for a specific time range
*/
function setCachedContexts(from, to, contexts) {
// Use rounded timestamps for cache key to improve hit rate
const key = `${roundToMinute(from)}:${roundToMinute(to)}`;
contextCache.set(key, {
timeRange: { from: from.toString(), to: to.toString() },
contexts,
timestamp: Date.now(),
});
// Clean up old entries if cache is too large
if (contextCache.size > cache_defaults_1.CACHE_SIZE.PATH_CONTEXT_MAX) {
const oldestKey = Array.from(contextCache.entries()).sort((a, b) => a[1].timestamp - b[1].timestamp)[0][0];
contextCache.delete(oldestKey);
}
}
/**
* Clear all cached contexts
*/
function clearContextCache() {
contextCache.clear();
}
/**
* Clear all caches (paths and contexts)
*/
function clearAllCaches() {
pathCache.clear();
contextCache.clear();
}
/**
* Get all cache statistics
*/
function getAllCacheStats() {
return {
paths: {
size: pathCache.size,
maxSize: cache_defaults_1.CACHE_SIZE.PATH_CONTEXT_MAX,
ttlMs: cache_defaults_1.CACHE_TTL.PATH_CONTEXT,
},
contexts: {
size: contextCache.size,
maxSize: cache_defaults_1.CACHE_SIZE.PATH_CONTEXT_MAX,
ttlMs: cache_defaults_1.CACHE_TTL.PATH_CONTEXT,
},
};
}
//# sourceMappingURL=path-cache.js.map