@quell/server
Version:
Quell is an open-source NPM package providing a light-weight caching layer implementation and cache invalidation for GraphQL responses on both the client- and server-side. Use Quell to prevent redundant client-side API requests and to minimize costly serv
117 lines (116 loc) • 4.92 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createClearAllCaches = exports.createDeleteCacheById = exports.createClearCache = void 0;
/**
* Creates a clearCache middleware function that flushes the Redis cache
* @param {InvalidateCacheConfig} config - Configuration containing the Redis client
* @returns {Function} Express middleware function
*/
function createClearCache(config) {
const { redisCache, idCache } = config;
/**
* Flushes the Redis cache. To clear the cache from the client, establish an endpoint that
* passes the request and response objects to an instance of QuellCache.clearCache.
* @param {Object} req - Express request object.
* @param {Object} res - Express response object.
* @param {Function} next - Express next middleware function.
*/
return function clearCache(req, res, next) {
console.log("Clearing Redis Cache");
// Clear Redis cache
redisCache.flushAll();
// Clear ID cache - reset it to empty object
Object.keys(idCache).forEach(key => delete idCache[key]);
return next();
};
}
exports.createClearCache = createClearCache;
/**
* Creates a function to remove a specific key-value from the cache
* @param {InvalidateCacheConfig} config - Configuration containing the Redis client
* @returns {Function} Function that deletes cache by ID
*/
function createDeleteCacheById(config) {
const { redisCache } = config;
/**
* Removes a specific key-value pair from the Redis cache
* @param {string} key - The cache key to delete
*/
return function deleteCacheById(key) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield redisCache.del(key);
}
catch (error) {
const err = {
log: `Error inside deleteCacheById function, ${error}`,
status: 400,
message: {
err: "Error in redis - deleteCacheById, Check server log for more details.",
},
};
console.log(err);
}
});
};
}
exports.createDeleteCacheById = createDeleteCacheById;
/**
* Creates a function to clear the entire cache and reset the idCache
* This is a utility function that combines both Redis and ID cache clearing
* @param {InvalidateCacheConfig} config - Configuration containing the Redis client and ID cache
* @returns {Function} Function that clears all caches
*/
function createClearAllCaches(config) {
const { redisCache, idCache } = config;
/**
* Clears both Redis cache and ID cache
*/
return function clearAllCaches() {
return __awaiter(this, void 0, void 0, function* () {
console.log("Clearing all caches");
try {
// Clear Redis cache
yield redisCache.flushAll();
// Clear ID cache - reset it to empty object
Object.keys(idCache).forEach(key => delete idCache[key]);
console.log("All caches cleared successfully");
}
catch (error) {
const err = {
log: `Error inside clearAllCaches function, ${error}`,
status: 400,
message: {
err: "Error clearing caches, Check server log for more details.",
},
};
console.log(err);
throw error; // Re-throw to let caller handle
}
});
};
}
exports.createClearAllCaches = createClearAllCaches;
/**
* FIX ME:
* Flushes the Redis cache. To clear the cache from the client, establish an endpoint that
* passes the request and response objects to an instance of QuellCache.clearCache.
* @param {Object} req - Express request object.
* @param {Object} res - Express response object.
* @param {Function} next - Express next middleware function.
*/
// clearCache(req: Request, res: Response, next: NextFunction) {
// console.log("Clearing Redis Cache");
// this.redisCache.flushAll();
// idCache = {};
// return next();
// }