Skip to content

Commit

Permalink
refactor: remove useless cache.set
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Sep 20, 2019
1 parent 50535a9 commit 11ef94b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 62 deletions.
29 changes: 11 additions & 18 deletions lib/routes/3dm/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,18 @@ module.exports = async (ctx) => {
.get();
}

const items = await Promise.all(
list.map(async (i) => {
const item = $(i);
const url = item.find('.bt').attr('href');
const items = list.map((i) => {
const item = $(i);
const url = item.find('.bt').attr('href');

const cache = await ctx.cache.get(url);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const single = {
title: item.find('.bt').text(),
pubDate: item.find('p').text(),
link: url,
guid: url,
};
ctx.cache.set(url, JSON.stringify(single));
return Promise.resolve(single);
})
);
const single = {
title: item.find('.bt').text(),
pubDate: item.find('p').text(),
link: url,
guid: url,
};
return single;
});

ctx.state.data = {
title: $('title')
Expand Down
57 changes: 24 additions & 33 deletions lib/routes/allpoetry/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,31 @@ module.exports = async (ctx) => {

const $ = cheerio.load(data);

const items = await Promise.all(
$('.sub')
.slice(0, 5)
.get()
.map(async (e) => {
let itemUrl = $(e)
const items = $('.sub')
.get()
.map((e) => {
let itemUrl = $(e)
.find('h1.title > a')
.attr('href');

itemUrl = url.resolve(host, itemUrl);

const single = {
title: $(e)
.find('h1.title > a')
.attr('href');

itemUrl = url.resolve(host, itemUrl);

const cache = await ctx.cache.get(itemUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}

const single = {
title: $(e)
.find('h1.title > a')
.text(),
description: $(e)
.find('div.poem_body')
.html(),
link: itemUrl,
author: $(e)
.find('div.bio >a ')
.attr('data-name'),
guid: itemUrl,
};

ctx.cache.set(itemUrl, JSON.stringify(single));
return Promise.resolve(single);
})
);
.text(),
description: $(e)
.find('div.poem_body')
.html(),
link: itemUrl,
author: $(e)
.find('div.bio >a ')
.attr('data-name'),
guid: itemUrl,
};

return single;
});

ctx.state.data = {
title: `All Poetry - ${order.toUpperCase() + order.slice(1)}`,
Expand Down
4 changes: 3 additions & 1 deletion lib/routes/douban/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ module.exports = async (ctx) => {
description: description,
};

ctx.cache.set(link, JSON.stringify(single));
if (type !== 'status') {
ctx.cache.set(link, JSON.stringify(single));
}
return Promise.resolve(single);
})
);
Expand Down
19 changes: 10 additions & 9 deletions lib/routes/duozhi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ module.exports = async (ctx) => {
description: '',
};

const description_key = 'duozhi_description_' + guid;
const description_value = await ctx.cache.get(description_key);
const key = 'duozhi_' + guid;
const cache = await ctx.cache.get(key);

const pubDate_key = 'duozhi_pubDate_' + guid;
const pubDate_value = await ctx.cache.get(pubDate_key);
if (cache) {
const value = JSON.parse(cache);

if (description_value && pubDate_value) {
single.description = description_value;
single.pubDate = pubDate_value;
single.description = value.description;
single.pubDate = value.pubDate;
} else {
const temp = await got(link);
single.description = $(temp.data)
Expand All @@ -47,8 +46,10 @@ module.exports = async (ctx) => {
.substr(0, 18)
).toUTCString();

ctx.cache.set(description_key, single.description);
ctx.cache.set(pubDate_key, single.pubDate);
ctx.cache.set(key, {
description: single.description,
pubDate: single.pubDate,
});
}

return Promise.resolve(single);
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/zhibo8/forum.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports = async (ctx) => {
$img.attr('src', 'https:' + src);
});
single.description = $('.detail_ent').html();
ctx.cache.set(key, single.description, 12 * 60 * 60);
ctx.cache.set(key, single.description);
}
return Promise.resolve(single);
})
Expand Down

0 comments on commit 11ef94b

Please sign in to comment.