const renderAndCache = function renderAndCache( req, res, pagePath, queryParams ) { const key = getCacheKey(req); if (ssrCache.has(key) && !isDev) { console.log(`CACHE HIT: ${key}`); res.send(ssrCache.get(key)); return; } app .renderToHTML(req, res, pagePath, queryParams) .then(html => { // Let's cache this page if (!isDev) { console.log(`CACHE MISS: ${key}`); ssrCache.set(key, html); } res.send(html); }) .catch(err => { app.renderError(err, req, res, pagePath, queryParams); }); }
/** * Get data from cache by key * * @param {any} key * @returns {Promise} * * @memberof MemoryLRUCacher */ get(key) { this.logger.debug(`GET ${key}`); this.metrics.increment(METRIC.MOLECULER_CACHER_GET_TOTAL); const timeEnd = this.metrics.timer(METRIC.MOLECULER_CACHER_GET_TIME); if (this.cache.has(key)) { this.logger.debug(`FOUND ${key}`); this.metrics.increment(METRIC.MOLECULER_CACHER_FOUND_TOTAL); let item = this.cache.get(key); const res = this.clone ? this.clone(item) : item; timeEnd(); return this.broker.Promise.resolve(res); } else { timeEnd(); } return this.broker.Promise.resolve(null); }
async function fetchExistingLabels (options) { const cacheKey = `${options.owner}:${options.repo}` if (existingLabelsCache.has(cacheKey)) { return existingLabelsCache.get(cacheKey) } const labelsResult = await fetchLabelPages(options, 1) const existingLabels = labelsResult.data || labelsResult || [] const existingLabelNames = existingLabels.map((label) => label.name) // cache labels so we don't have to fetch these *all the time* existingLabelsCache.set(cacheKey, existingLabelNames) options.logger.debug('Filled existing repo labels cache: ' + existingLabelNames) return existingLabelNames }
function renderAndCache(req, res, pagePath, queryParams) { if (ssrCache.has(req.url)) { console.log(`CACHE HIT: ${req.url}`); res.send(ssrCache.get(req.url)); return; } app.renderToHTML(req, res, pagePath, queryParams) .then((html) => { console.log(`CACHE MISS: ${req.url}`); ssrCache.set(req.url, html); res.send(html); }) .catch((err) => { app.renderError(err, req, res, pagePath, queryParams); }); }
async function getList(channel) { if (cache.has(channel)) { return cache.get(channel); } if (cache_lock[channel] && cache_bak.has(channel)) { return cache_bak.get(channel) || {}; } cache_lock[channel] = true; const data = await searchFromData(channel); cache.set(channel, data); cache_lock[channel] = false; return data; }
function renderAndCache(req, res, pagePath, queryParams) { if (dev) { return app.render(req, res, pagePath, queryParams); } const key = req.url; if (ssrCache.has(key)) { console.log(`CACHE HIT: ${key}`); res.send(ssrCache.get(key)); return; // eslint-disable-line } return app.renderToHTML(req, res, pagePath, queryParams) .then((html) => { console.log(`CACHE MISS: ${key}`); ssrCache.set(key, html); res.send(html); }) .catch(err => app.renderError(err, req, res, pagePath, queryParams)); }
async function renderAndCache(req, res, pagePath, queryParams) { const key = getCacheKey(req); if (ssrCache.has(key)) { res.setHeader("x-cache", "HIT"); res.send(ssrCache.get(key)); return; } try { const html = await app.renderToHTML(req, res, pagePath, queryParams); if (res.statusCode !== 200) { res.send(html); return; } ssrCache.set(key, html); res.setHeader("x-cache", "MISS"); res.send(html); } catch (err) { app.renderError(err, req, res, pagePath, queryParams); } }