Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core): unified memory cache #9395

Merged
merged 1 commit into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/middleware/cache/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ if (config.cache.type === 'redis') {
globalCache.set = cacheModule.set;
} else if (config.cache.type === 'memory') {
cacheModule = require('./memory');
const { pageCache } = cacheModule.clients;
const { memoryCache } = cacheModule.clients;
globalCache.get = (key) => {
if (key && cacheModule.status.available) {
return pageCache.get(key);
return memoryCache.get(key, { updateAgeOnGet: false });
}
};
globalCache.set = (key, value, maxAge) => {
Expand All @@ -41,7 +41,7 @@ if (config.cache.type === 'redis') {
value = JSON.stringify(value);
}
if (key) {
return pageCache.set(key, value, { ttl: maxAge * 1000 });
return memoryCache.set(key, value, { ttl: maxAge * 1000 });
}
};
} else {
Expand Down
16 changes: 5 additions & 11 deletions lib/middleware/cache/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,34 @@ const config = require('@/config').value;

const status = { available: false };

const pageCache = new Lru({
const memoryCache = new Lru({
ttl: config.cache.routeExpire * 1000,
max: config.memory.max,
});

const routeCache = new Lru({
ttl: config.cache.routeExpire * 1000,
max: config.memory.max,
updateAgeOnGet: true,
});

status.available = true;

module.exports = {
get: (key, refresh = true) => {
if (key && status.available) {
let value = (refresh ? routeCache : pageCache).get(key);
let value = memoryCache.get(key, { updateAgeOnGet: refresh });
if (value) {
value = value + '';
}
return value;
}
},
set: (key, value, maxAge = config.cache.contentExpire, refresh = true) => {
set: (key, value, maxAge = config.cache.contentExpire) => {
if (!value || value === 'undefined') {
value = '';
}
if (typeof value === 'object') {
value = JSON.stringify(value);
}
if (key && status.available) {
return (refresh ? routeCache : pageCache).set(key, value, { ttl: maxAge * 1000 });
return memoryCache.set(key, value, { ttl: maxAge * 1000 });
}
},
clients: { pageCache, routeCache },
clients: { memoryCache },
status,
};
2 changes: 1 addition & 1 deletion lib/routes/amd/graphicsdrivers.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = async (ctx) => {
if (!data) {
const res = await got.get(url);
data = res.data;
ctx.cache.set(url, data, config.cache.contentExpire, false);
ctx.cache.set(url, data, config.cache.contentExpire);
}

const $ = cheerio.load(data, { xmlMode: true });
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/weibo/timeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ module.exports = async (ctx) => {
const token = rep.data.access_token;
const uid = rep.data.uid;
const expires_in = rep.data.expires_in;
await ctx.cache.set('weibotimelineuid' + uid, token, expires_in, false);
await ctx.cache.set('weibotimelineuid' + uid, token, expires_in);

ctx.set({
'Content-Type': 'text/html; charset=UTF-8',
Expand Down
2 changes: 1 addition & 1 deletion lib/v2/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ module.exports = async (ctx) => {
}
if (!noRefresh) {
noRefresh = '0';
await ctx.cache.set('noRefreshCache', '1', undefined, false);
await ctx.cache.set('noRefreshCache', '1', undefined);
}
item.push({
title: 'Cache Title',
Expand Down