@@ -9,20 +9,35 @@ function Redmoon (config) {
99 this . _client = null
1010 this . _nrp = null
1111
12+ this . _timer = null
13+
1214 this . config = {
1315 scope : 'redmoon' ,
1416 name : 'default' ,
1517 host : '127.0.0.1' ,
1618 port : 6379 ,
1719 timeout : 5000 , // 5 sec
18- buffer : 100 ,
19- ttl : 5
20+ buffer : 100 , // buffer count for preloading
21+ ttl : 5 , // max time for atomic
22+ interval : 60 // interval value for garbage collection
2023 }
2124 this . key = {
2225 atomic : [ this . config . scope , 'atomic' ] . join ( ':' ) , // hash
2326 meta : [ this . config . scope , 'meta' ] . join ( ':' ) , // hash
2427 cycle : [ this . config . scope , 'cycle' ] . join ( ':' ) // set (sorted)
2528 }
29+
30+ this . garbage = {
31+ start : function ( offset ) {
32+ this . _timer = setInterval ( function ( ) {
33+ this . truncate ( Redmoon . unix ( ) - offset )
34+ } . bind ( this ) , this . config . interval * 1000 )
35+ } . bind ( this ) ,
36+ stop : function ( ) {
37+ clearInterval ( this . _timer )
38+ this . _timer = null
39+ } . bind ( this )
40+ }
2641}
2742
2843util . inherits ( Redmoon , events . EventEmitter )
@@ -37,6 +52,9 @@ Redmoon.prototype.connect = function (config) {
3752 typeof config . host !== 'undefined' && ( this . config . host = config . host )
3853 typeof config . port !== 'undefined' && ( this . config . port = config . port )
3954 typeof config . timeout !== 'undefined' && ( this . config . timeout = config . timeout )
55+ typeof config . buffer !== 'undefined' && ( this . config . buffer = config . buffer )
56+ typeof config . ttl !== 'undefined' && ( this . config . ttl = config . ttl )
57+ typeof config . interval !== 'undefined' && ( this . config . interval = config . interval )
4058
4159 this . _client = require ( 'redis' ) . createClient ( this . config . port , this . config . host )
4260 this . _nrp = new NRP ( this . config )
@@ -72,14 +90,11 @@ Redmoon.prototype.load = function (cb, key, page, limit) {
7290 limit = limit || 10
7391
7492 // check exist search result on meta
75- this . _client . send_command ( 'HSCAN' , [ this . key . meta , 0 , 'MATCH' , uuid + ':*' , 'COUNT' , 1000 ] , function ( err , metas ) {
93+ this . loadMeta ( function ( err , metas ) {
7694 if ( err ) {
7795 return cb ( err )
7896 }
7997
80- // migrate
81- metas = metas [ 1 ]
82-
8398 if ( metas . length > 0 ) {
8499 var start = ( page - 1 ) * limit
85100 var end = start + limit - 1
@@ -132,12 +147,20 @@ Redmoon.prototype.load = function (cb, key, page, limit) {
132147 self . _nrp . off ( topic )
133148 self . load ( cb , key , page , limit )
134149 } )
135- self . _nrp . emit ( [ self . config . name , uuid ] . join ( ':' ) , {
150+ self . _nrp . emit ( [ self . config . scope , self . config . name , 'request' , uuid ] . join ( ':' ) , {
136151 uuid : uuid ,
137152 key : key ,
138153 topic : topic
139154 } )
140155 }
156+ } , uuid )
157+
158+ return this
159+ }
160+
161+ Redmoon . prototype . loadMeta = function ( cb , uuid , count ) {
162+ this . _client . send_command ( 'HSCAN' , [ this . key . meta , 0 , 'MATCH' , uuid + ':*' , 'COUNT' , count || 1000 ] , function ( err , metas ) {
163+ cb ( err , metas ? metas [ 1 ] : [ ] )
141164 } )
142165
143166 return this
@@ -161,26 +184,29 @@ Redmoon.prototype.add = function (cb, moon, meta, items) {
161184}
162185
163186Redmoon . prototype . subscribe = function ( cb ) {
164- this . _nrp . on ( this . config . name + ':*' , cb )
187+ this . _nrp . on ( [ this . config . scope , this . config . name , 'request' , '*' ] . join ( ':' ) , cb )
188+ return this
165189}
166190
167191Redmoon . prototype . unsubscribe = function ( cb ) {
168- this . _nrp . off ( this . config . name + ':*' , cb )
192+ this . _nrp . off ( [ this . config . scope , this . config . name , 'request' , '*' ] . join ( ':' ) , cb )
193+ return this
169194}
170195
171196Redmoon . prototype . trigger = function ( topic , data ) {
172197 this . _nrp . emit ( topic , data || { } )
198+ return this
173199}
174200
175- // 중복 이벤트 처리를 방지 목적.
176- Redmoon . prototype . atomic = function ( cb , uuid ) {
201+ // atomic processing
202+ Redmoon . prototype . atomic = function ( uuid , process , complete ) {
177203 // atomic
178204 var self = this
179205 var key = [ this . key . atomic , uuid ] . join ( ':' )
180206
181207 this . _client . get ( key , function ( err , item ) {
182208 if ( err ) {
183- return console . error ( err )
209+ return self . emit ( 'error' , err )
184210 }
185211
186212 if ( item ) {
@@ -191,13 +217,19 @@ Redmoon.prototype.atomic = function (cb, uuid) {
191217 self . _client . multi ( )
192218 . set ( key , uuid )
193219 . expire ( key , self . config . ttl )
194- . exec ( cb )
195- } )
196- }
220+ . exec ( function ( err ) {
221+ if ( err ) {
222+ return self . emit ( 'error' , err )
223+ }
197224
198- // 중복 이벤트 처리를 방지 목적.
199- Redmoon . prototype . release = function ( cb , uuid ) {
200- this . _client . del ( [ this . key . atomic , uuid ] . join ( ':' ) , cb )
225+ process ( function ( err , data ) {
226+ self . _client . del ( [ self . key . atomic , uuid ] . join ( ':' ) , function ( ) {
227+ complete ( err , data )
228+ } )
229+ } )
230+ } )
231+ } )
232+ return this
201233}
202234
203235Redmoon . prototype . end = function ( ) {
@@ -210,10 +242,47 @@ Redmoon.prototype.end = function () {
210242 this . _nrp . end ( )
211243 this . _nrp = null
212244 }
245+ return this
246+ }
247+
248+ Redmoon . prototype . truncate = function ( unix ) {
249+ var self = this
250+ unix = unix || Redmoon . unix ( ) - 3600 // default 1 hour
251+
252+ this . _client . zrangebyscore ( this . key . cycle , 0 , unix , function ( err , items ) {
253+ if ( err ) {
254+ return self . emit ( 'error' , err )
255+ }
256+
257+ for ( var i = 0 ; i < items . length ; i ++ ) {
258+ self . loadMeta ( ( function ( uuid ) {
259+ return function ( err , metas ) {
260+ if ( err ) {
261+ return self . emit ( 'error' , err )
262+ }
263+
264+ var multi = self . _client . multi ( )
265+
266+ for ( var j = 0 ; j < metas . length ; j += 2 ) {
267+ multi . hdel ( self . key . meta , metas [ j ] )
268+ }
269+
270+ multi . del ( [ self . config . scope , self . config . name , uuid ] . join ( ':' ) )
271+ multi . zrem ( self . key . cycle , uuid )
272+ multi . exec ( function ( err , replies ) {
273+ if ( err ) {
274+ return self . emit ( 'error' , err )
275+ }
276+ } )
277+ }
278+ } ) ( items [ i ] ) , items [ i ] )
279+ }
280+ } )
281+ return this
213282}
214283
215284Redmoon . prototype . toTopic = function ( uuid ) {
216- return [ shortid . generate ( ) , this . config . name , 'result' , uuid ] . join ( ':' )
285+ return [ this . config . name , 'result' , uuid ] . join ( ':' )
217286}
218287
219288Redmoon . uuid = function ( key ) {
0 commit comments