restifyx.js
Version:
Advanced API endpoint handler system with automatic documentation
71 lines • 2.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetCache = GetCache;
exports.SetCache = SetCache;
exports.DeleteCache = DeleteCache;
exports.ClearCache = ClearCache;
const cache_1 = require("../utils/cache");
const logger_1 = require("../utils/logger");
/**
* Get data from the cache
*
* @param options - Options for retrieving cache data
* @returns The cached data or undefined if not found
*/
async function GetCache(options = {}) {
const logger = (0, logger_1.getLogger)();
const { type = "return", key } = options;
if (!key) {
const req = global.CURRENT_REQUEST;
if (!req) {
logger.error("GetCache used outside of an endpoint handler context");
throw new Error("GetCache must be used within an endpoint handler or provide a key");
}
const cacheKey = `${req.method}:${req.originalUrl || req.url}`;
const cachedData = cache_1.memoryCache.get(cacheKey);
if (type === "console.log") {
logger.info(`Cache for ${cacheKey}: ${cachedData ? "HIT" : "MISS"}`);
if (cachedData) {
logger.info(`Cached data: ${JSON.stringify(cachedData)}`);
}
return cachedData;
}
return cachedData;
}
else {
const cachedData = cache_1.memoryCache.get(key);
if (type === "console.log") {
logger.info(`Cache for ${key}: ${cachedData ? "HIT" : "MISS"}`);
if (cachedData) {
logger.info(`Cached data: ${JSON.stringify(cachedData)}`);
}
return cachedData;
}
return cachedData;
}
}
/**
* Set data in the cache
*
* @param key - Cache key
* @param data - Data to cache
* @param duration - Cache duration in seconds
*/
function SetCache(key, data, duration) {
cache_1.memoryCache.set(key, data, duration);
}
/**
* Delete data from the cache
*
* @param key - Cache key to delete
*/
function DeleteCache(key) {
cache_1.memoryCache.delete(key);
}
/**
* Clear the entire cache
*/
function ClearCache() {
cache_1.memoryCache.clear();
}
//# sourceMappingURL=GetCache.js.map