@@ -15,9 +15,11 @@ function Redmoon (config) {
1515 host : '127.0.0.1' ,
1616 port : 6379 ,
1717 timeout : 5000 , // 5 sec
18- buffer : 100
18+ buffer : 100 ,
19+ ttl : 5
1920 }
2021 this . key = {
22+ atomic : [ this . config . scope , 'atomic' ] . join ( ':' ) , // hash
2123 meta : [ this . config . scope , 'meta' ] . join ( ':' ) , // hash
2224 cycle : [ this . config . scope , 'cycle' ] . join ( ':' ) // set (sorted)
2325 }
@@ -94,24 +96,24 @@ Redmoon.prototype.load = function (cb, key, page, limit) {
9496
9597 // migrate meta
9698 var meta = { }
97- var totalcount = 0
99+ var loadedcount = 0
98100 for ( var j = 1 , parsed = null ; j < metas . length ; j += 2 ) {
99101 parsed = JSON . parse ( metas [ j ] )
100102 meta [ parsed . provider ] = parsed
101- totalcount += parseInt ( parsed . totalcount , 10 )
103+ loadedcount += ( parsed . page - 1 ) * ( parsed . limit || 0 )
102104 }
103105
104- cb ( null , items , meta )
105-
106- // check data pool
107- if ( end + self . config . buffer > totalcount ) {
108- // preload data
106+ // required collection
107+ if ( end + self . config . buffer > loadedcount ) {
109108 self . _nrp . emit ( [ self . config . name , uuid ] . join ( ':' ) , {
109+ uuid : uuid ,
110110 key : key ,
111111 topic : self . toTopic ( uuid ) ,
112112 meta : meta
113113 } )
114114 }
115+
116+ cb ( null , items , meta )
115117 } )
116118 } else {
117119 var timeout = null
@@ -131,6 +133,7 @@ Redmoon.prototype.load = function (cb, key, page, limit) {
131133 self . load ( cb , key , page , limit )
132134 } )
133135 self . _nrp . emit ( [ self . config . name , uuid ] . join ( ':' ) , {
136+ uuid : uuid ,
134137 key : key ,
135138 topic : topic
136139 } )
@@ -142,7 +145,6 @@ Redmoon.prototype.load = function (cb, key, page, limit) {
142145
143146Redmoon . prototype . add = function ( cb , moon , meta , items ) {
144147 var uuid = Redmoon . uuid ( moon . key )
145-
146148 var multi = this . _client . multi ( )
147149 . zadd ( this . key . cycle , Redmoon . unix ( ) , uuid )
148150 . hset ( this . key . meta , [ uuid , meta . provider || shortid . generate ( ) ] . join ( ':' ) , JSON . stringify ( meta ) )
@@ -170,6 +172,34 @@ Redmoon.prototype.trigger = function (topic, data) {
170172 this . _nrp . emit ( topic , data || { } )
171173}
172174
175+ // 중복 이벤트 처리를 방지 목적.
176+ Redmoon . prototype . atomic = function ( cb , uuid ) {
177+ // atomic
178+ var self = this
179+ var key = [ this . key . atomic , uuid ] . join ( ':' )
180+
181+ this . _client . get ( key , function ( err , item ) {
182+ if ( err ) {
183+ return console . error ( err )
184+ }
185+
186+ if ( item ) {
187+ // already requested
188+ return true
189+ }
190+
191+ self . _client . multi ( )
192+ . set ( key , uuid )
193+ . expire ( key , self . config . ttl )
194+ . exec ( cb )
195+ } )
196+ }
197+
198+ // 중복 이벤트 처리를 방지 목적.
199+ Redmoon . prototype . release = function ( cb , uuid ) {
200+ this . _client . del ( [ this . key . atomic , uuid ] . join ( ':' ) , cb )
201+ }
202+
173203Redmoon . prototype . end = function ( ) {
174204 if ( this . _client ) {
175205 this . _client . end ( )
0 commit comments