n8n-nodes-smartsuite
Version:
n8n community node for SmartSuite
97 lines • 2.65 kB
JavaScript
;
// src/nodes/SmartSuite/helpers/cache.ts
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCache = getCache;
exports.setCache = setCache;
exports.clearCache = clearCache;
exports.clearAllCache = clearAllCache;
exports.getCacheSize = getCacheSize;
exports.cleanupExpiredCache = cleanupExpiredCache;
exports.generateFieldsCacheKey = generateFieldsCacheKey;
// Global cache storage
const cacheStore = new Map();
// Default TTL in milliseconds (60 seconds)
const DEFAULT_TTL_MS = 60000;
/**
* Get a value from cache if it exists and hasn't expired
* @param key - The cache key
* @param ttlMs - Optional TTL in milliseconds (defaults to 60 seconds)
* @returns The cached value or undefined if not found/expired
*/
function getCache(key, ttlMs = DEFAULT_TTL_MS) {
const entry = cacheStore.get(key);
if (!entry) {
return undefined;
}
const now = Date.now();
const age = now - entry.timestamp;
// Check if entry has expired
if (age > ttlMs) {
// Remove expired entry
cacheStore.delete(key);
return undefined;
}
return entry.value;
}
/**
* Set a value in the cache with timestamp
* @param key - The cache key
* @param value - The value to cache
* @returns The cached value
*/
function setCache(key, value) {
const entry = {
value,
timestamp: Date.now(),
};
cacheStore.set(key, entry);
return value;
}
/**
* Clear a specific cache entry
* @param key - The cache key to clear
*/
function clearCache(key) {
return cacheStore.delete(key);
}
/**
* Clear all cache entries
*/
function clearAllCache() {
cacheStore.clear();
}
/**
* Get the size of the cache
* @returns Number of entries in the cache
*/
function getCacheSize() {
return cacheStore.size;
}
/**
* Clean up expired cache entries
* @param ttlMs - TTL to check against (defaults to 60 seconds)
*/
function cleanupExpiredCache(ttlMs = DEFAULT_TTL_MS) {
const now = Date.now();
let cleaned = 0;
for (const [key, entry] of cacheStore.entries()) {
const age = now - entry.timestamp;
if (age > ttlMs) {
cacheStore.delete(key);
cleaned++;
}
}
return cleaned;
}
/**
* Generate a cache key for field options
* @param solutionId - The solution ID
* @param tableId - The table ID
* @param suffix - Optional suffix for the key
* @returns A formatted cache key
*/
function generateFieldsCacheKey(solutionId, tableId, suffix) {
const baseKey = `fields:${solutionId}:${tableId}`;
return suffix ? `${baseKey}:${suffix}` : baseKey;
}
//# sourceMappingURL=cache.js.map