@@ -13,7 +13,7 @@ var User = require('../proxy').User;
1313var Topic = require ( '../proxy' ) . Topic ;
1414var config = require ( '../config' ) ;
1515var eventproxy = require ( 'eventproxy' ) ;
16- var mcache = require ( 'memory- cache' ) ;
16+ var cache = require ( '../common/ cache' ) ;
1717var xmlbuilder = require ( 'xmlbuilder' ) ;
1818var renderHelpers = require ( '../common/render_helpers' ) ;
1919
@@ -32,8 +32,7 @@ function indexCache() {
3232 }
3333 var optionsStr = JSON . stringify ( query ) + JSON . stringify ( options ) ;
3434 Topic . getTopicsByQuery ( query , options , function ( err , topics ) {
35- mcache . put ( optionsStr , topics ) ;
36- return topics ;
35+ cache . set ( optionsStr , topics ) ;
3736 } ) ;
3837 } ) ;
3938}
@@ -61,53 +60,62 @@ exports.index = function (req, res, next) {
6160 var options = { skip : ( page - 1 ) * limit , limit : limit , sort : '-top -last_reply_at' } ;
6261 var optionsStr = JSON . stringify ( query ) + JSON . stringify ( options ) ;
6362
64- if ( mcache . get ( optionsStr ) ) {
65- proxy . emitLater ( 'topics' , mcache . get ( optionsStr ) ) ;
66- } else {
63+ cache . get ( optionsStr , proxy . done ( function ( topics ) {
64+ if ( topics ) {
65+ return proxy . emit ( 'topics' , topics ) ;
66+ }
6767 Topic . getTopicsByQuery ( query , options , proxy . done ( 'topics' , function ( topics ) {
6868 return topics ;
6969 } ) ) ;
70- }
70+ } ) ) ;
7171 // END 取主题
7272
7373 // 取排行榜上的用户
74- if ( mcache . get ( 'tops' ) ) {
75- proxy . emitLater ( 'tops' , mcache . get ( 'tops' ) ) ;
76- } else {
77- User . getUsersByQuery (
78- { '$or' : [
79- { is_block : { '$exists' : false } } ,
80- { is_block : false }
81- ] } ,
82- { limit : 10 , sort : '-score' } ,
83- proxy . done ( 'tops' , function ( tops ) {
84- mcache . put ( 'tops' , tops , 1000 * 60 * 1 ) ;
85- return tops ;
86- } )
87- ) ;
88- }
74+ cache . get ( 'tops' , proxy . done ( function ( tops ) {
75+ if ( tops ) {
76+ proxy . emit ( 'tops' , tops ) ;
77+ } else {
78+ User . getUsersByQuery (
79+ { '$or' : [
80+ { is_block : { '$exists' : false } } ,
81+ { is_block : false }
82+ ] } ,
83+ { limit : 10 , sort : '-score' } ,
84+ proxy . done ( 'tops' , function ( tops ) {
85+ cache . set ( 'tops' , tops , 1000 * 60 * 1 ) ;
86+ return tops ;
87+ } )
88+ ) ;
89+ }
90+ } ) ) ;
91+
8992 // 取0回复的主题
90- if ( mcache . get ( 'no_reply_topics' ) ) {
91- proxy . emitLater ( 'no_reply_topics' , mcache . get ( 'no_reply_topics' ) ) ;
92- } else {
93- Topic . getTopicsByQuery (
94- { reply_count : 0 } ,
95- { limit : 5 , sort : '-create_at' } ,
96- proxy . done ( 'no_reply_topics' , function ( no_reply_topics ) {
97- mcache . put ( 'no_reply_topics' , no_reply_topics , 1000 * 60 * 1 ) ;
98- return no_reply_topics ;
99- } ) ) ;
100- }
93+ cache . get ( 'no_reply_topics' , proxy . done ( function ( no_reply_topics ) {
94+ if ( no_reply_topics ) {
95+ proxy . emit ( 'no_reply_topics' , no_reply_topics ) ;
96+ } else {
97+ Topic . getTopicsByQuery (
98+ { reply_count : 0 } ,
99+ { limit : 5 , sort : '-create_at' } ,
100+ proxy . done ( 'no_reply_topics' , function ( no_reply_topics ) {
101+ cache . set ( 'no_reply_topics' , no_reply_topics , 1000 * 60 * 1 ) ;
102+ return no_reply_topics ;
103+ } ) ) ;
104+ }
105+ } ) ) ;
106+
101107 // 取分页数据
102- if ( mcache . get ( 'pages' ) ) {
103- proxy . emitLater ( 'pages' , mcache . get ( 'pages' ) ) ;
104- } else {
105- Topic . getCountByQuery ( query , proxy . done ( function ( all_topics_count ) {
106- var pages = Math . ceil ( all_topics_count / limit ) ;
107- mcache . put ( JSON . stringify ( query ) + 'pages' , pages , 1000 * 60 * 1 ) ;
108+ cache . get ( 'pages' , proxy . done ( function ( pages ) {
109+ if ( pages ) {
108110 proxy . emit ( 'pages' , pages ) ;
109- } ) ) ;
110- }
111+ } else {
112+ Topic . getCountByQuery ( query , proxy . done ( function ( all_topics_count ) {
113+ var pages = Math . ceil ( all_topics_count / limit ) ;
114+ cache . set ( JSON . stringify ( query ) + 'pages' , pages , 1000 * 60 * 1 ) ;
115+ proxy . emit ( 'pages' , pages ) ;
116+ } ) ) ;
117+ }
118+ } ) ) ;
111119
112120 var tabName = renderHelpers . tabName ( tab ) ;
113121 proxy . all ( 'topics' , 'tops' , 'no_reply_topics' , 'pages' ,
@@ -139,22 +147,23 @@ exports.sitemap = function (req, res, next) {
139147 res . send ( sitemap ) ;
140148 } ) ;
141149
142- var sitemapData = mcache . get ( 'sitemap' ) ;
143- if ( sitemapData ) {
144- ep . emit ( 'sitemap' , sitemapData ) ;
145- } else {
146- Topic . getLimit5w ( function ( err , topics ) {
147- if ( err ) {
148- return next ( err ) ;
149- }
150- topics . forEach ( function ( topic ) {
151- urlset . ele ( 'url' ) . ele ( 'loc' , 'http://cnodejs.org/topic/' + topic . _id ) ;
152- } ) ;
153-
154- var sitemapData = urlset . end ( ) ;
155- // 缓存一天
156- mcache . put ( 'sitemap' , sitemapData , 1000 * 3600 * 24 ) ;
150+ cache . get ( 'sitemap' , ep . done ( function ( sitemapData ) {
151+ if ( sitemapData ) {
157152 ep . emit ( 'sitemap' , sitemapData ) ;
158- } ) ;
159- }
153+ } else {
154+ Topic . getLimit5w ( function ( err , topics ) {
155+ if ( err ) {
156+ return next ( err ) ;
157+ }
158+ topics . forEach ( function ( topic ) {
159+ urlset . ele ( 'url' ) . ele ( 'loc' , 'http://cnodejs.org/topic/' + topic . _id ) ;
160+ } ) ;
161+
162+ var sitemapData = urlset . end ( ) ;
163+ // 缓存一天
164+ cache . set ( 'sitemap' , sitemapData , 1000 * 3600 * 24 ) ;
165+ ep . emit ( 'sitemap' , sitemapData ) ;
166+ } ) ;
167+ }
168+ } ) ) ;
160169} ;
0 commit comments