mirror of
https://github.com/cnodejs/nodeclub.git
synced 2025-12-08 19:55:55 +00:00
fix 缓存无效问题
This commit is contained in:
parent
67c098202b
commit
4ab2d4c337
@ -16,32 +16,7 @@ var eventproxy = require('eventproxy');
|
||||
var cache = require('../common/cache');
|
||||
var xmlbuilder = require('xmlbuilder');
|
||||
var renderHelper = require('../common/render_helper');
|
||||
|
||||
// 主页的缓存工作。主页是需要主动缓存的
|
||||
function indexCache() {
|
||||
if (config.debug) {
|
||||
return;
|
||||
}
|
||||
var limit = config.list_topic_count;
|
||||
// 为所有版块(tab)做缓存
|
||||
[['', '全部']].concat(config.tabs).forEach(function (pair) {
|
||||
// 只缓存第一页, page = 1。options 之所以每次都生成是因为 mongoose 查询时,
|
||||
// 会改动它
|
||||
var options = { skip: (1 - 1) * limit, limit: limit, sort: '-top -last_reply_at'};
|
||||
var tabValue = pair[0];
|
||||
var query = {};
|
||||
if (tabValue) {
|
||||
query.tab = tabValue;
|
||||
}
|
||||
var optionsStr = JSON.stringify(query) + JSON.stringify(options);
|
||||
Topic.getTopicsByQuery(query, options, function (err, topics) {
|
||||
cache.set(optionsStr, topics);
|
||||
});
|
||||
});
|
||||
}
|
||||
setInterval(indexCache, 1000 * 5); // 五秒更新一次
|
||||
indexCache();
|
||||
// END 主页的缓存工作
|
||||
var _ = require('lodash');
|
||||
|
||||
exports.index = function (req, res, next) {
|
||||
var page = parseInt(req.query.page, 10) || 1;
|
||||
@ -63,17 +38,10 @@ exports.index = function (req, res, next) {
|
||||
|
||||
var limit = config.list_topic_count;
|
||||
var options = { skip: (page - 1) * limit, limit: limit, sort: '-top -last_reply_at'};
|
||||
var optionsStr = JSON.stringify(query) + JSON.stringify(options);
|
||||
|
||||
cache.get(optionsStr, proxy.done(function (topics) {
|
||||
if (topics) {
|
||||
return proxy.emit('topics', topics);
|
||||
}
|
||||
Topic.getTopicsByQuery(query, options, proxy.done('topics', function (topics) {
|
||||
return topics;
|
||||
}));
|
||||
Topic.getTopicsByQuery(query, options, proxy.done('topics', function (topics) {
|
||||
return topics;
|
||||
}));
|
||||
// END 取主题
|
||||
|
||||
// 取排行榜上的用户
|
||||
cache.get('tops', proxy.done(function (tops) {
|
||||
@ -93,6 +61,7 @@ exports.index = function (req, res, next) {
|
||||
);
|
||||
}
|
||||
}));
|
||||
// END 取排行榜上的用户
|
||||
|
||||
// 取0回复的主题
|
||||
cache.get('no_reply_topics', proxy.done(function (no_reply_topics) {
|
||||
@ -108,6 +77,7 @@ exports.index = function (req, res, next) {
|
||||
}));
|
||||
}
|
||||
}));
|
||||
// END 取0回复的主题
|
||||
|
||||
// 取分页数据
|
||||
cache.get('pages', proxy.done(function (pages) {
|
||||
@ -121,6 +91,7 @@ exports.index = function (req, res, next) {
|
||||
}));
|
||||
}
|
||||
}));
|
||||
// END 取分页数据
|
||||
|
||||
var tabName = renderHelper.tabName(tab);
|
||||
proxy.all('topics', 'tops', 'no_reply_topics', 'pages',
|
||||
|
||||
@ -1257,10 +1257,6 @@ textarea.editor {
|
||||
height: 200px;
|
||||
}
|
||||
|
||||
.jiathis_style_24x24 {
|
||||
display: inline-block;
|
||||
}
|
||||
|
||||
.cnode-app-download {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
var cache = require('../../common/cache');
|
||||
var should = require('should');
|
||||
|
||||
describe.only('test/common/cache.test.js', function () {
|
||||
describe('test/common/cache.test.js', function () {
|
||||
it('should set && get', function (done) {
|
||||
cache.set('alsotang', {age: 23}, function () {
|
||||
cache.get('alsotang', function (err, data) {
|
||||
|
||||
@ -134,19 +134,6 @@
|
||||
<div class='topic_content'>
|
||||
<%- markdown(topic.linkedContent) %>
|
||||
</div>
|
||||
<div class="share">
|
||||
<!-- JiaThis Button BEGIN -->
|
||||
<div class="jiathis_style_24x24">
|
||||
<a class="jiathis_button_qzone"></a>
|
||||
<a class="jiathis_button_tsina"></a>
|
||||
<a class="jiathis_button_tqq"></a>
|
||||
<a class="jiathis_button_weixin"></a>
|
||||
<a class="jiathis_button_renren"></a>
|
||||
<a class="jiathis_counter_style"></a>
|
||||
</div>
|
||||
<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js" charset="utf-8"></script>
|
||||
<!-- JiaThis Button END -->
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<% if (topic.replies && topic.replies.length > 0) { %>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user