File tree 5 files changed +107
-0
lines changed
5 files changed +107
-0
lines changed Original file line number Diff line number Diff line change
1
+ exports . install = function ( framework ) {
2
+ framework . route ( '/' , view_homepage , [ '#session' ] ) ;
3
+ } ;
4
+
5
+ function view_homepage ( ) {
6
+ var self = this ;
7
+
8
+ if ( self . session . counter === undefined )
9
+ self . session . counter = 0 ;
10
+ else
11
+ self . session . counter ++ ;
12
+
13
+ process . send ( 'Response framework ID: ' + framework . id + ' (' + self . session . counter + ')' ) ;
14
+ self . view ( 'homepage' ) ;
15
+ }
Original file line number Diff line number Diff line change
1
+ var redis = require ( 'redis' ) ;
2
+
3
+ // Install session module
4
+ INSTALL ( 'module' , 'https://modules.totaljs.com/session/v1.00/session.js' ) ;
5
+
6
+ // Configure session module with REDIS
7
+ framework . on ( 'install' , function ( type , name ) {
8
+
9
+ if ( type !== 'module' )
10
+ return ;
11
+
12
+ if ( name !== 'session' )
13
+ return ;
14
+
15
+ var session = MODULE ( 'session' ) . instance ;
16
+
17
+ session . onRead = function ( id , callback ) {
18
+ var client = redis . createClient ( ) ;
19
+ client . get ( 'session_' + id , function ( err , reply ) {
20
+ client . quit ( ) ;
21
+ callback ( err ? { } : reply === null ? { } : JSON . parse ( reply . toString ( ) ) ) ;
22
+ } ) ;
23
+ } ;
24
+
25
+ session . onWrite = function ( id , value ) {
26
+ var client = redis . createClient ( ) ;
27
+ client . set ( 'session_' + id , JSON . stringify ( value ) ) ;
28
+ client . quit ( ) ;
29
+ } ;
30
+ } ) ;
Original file line number Diff line number Diff line change
1
+ var http = require ( 'http' ) ;
2
+ var cluster = require ( 'cluster' ) ;
3
+ var os = require ( 'os' ) ;
4
+
5
+ var debug = true ;
6
+
7
+ if ( ! cluster . isMaster ) {
8
+
9
+ // This code will be executed according the number of CPU
10
+ // This code will be using: single process RAM * numCPUs
11
+ var framework = require ( 'total.js' ) ;
12
+
13
+ // Set framework ID
14
+ framework . on ( 'message' , function ( message ) {
15
+ if ( message . type === 'id' )
16
+ framework . id = message . id ;
17
+ } ) ;
18
+
19
+ framework . http ( 'debug' ) ;
20
+ return ;
21
+ }
22
+
23
+ var numCPUs = os . cpus ( ) . length ;
24
+
25
+ for ( var i = 0 ; i < numCPUs ; i ++ ) {
26
+
27
+ // Run framework
28
+ var fork = cluster . fork ( ) ;
29
+
30
+ fork . on ( 'message' , onMessage ) ;
31
+
32
+ // Send ID
33
+ fork . send ( { type : 'id' , id : i } ) ;
34
+ }
35
+
36
+ console . log ( 'Cluster is running.' ) ;
37
+
38
+ function onMessage ( message ) {
39
+ console . log ( 'Message ->' , message ) ;
40
+ }
41
+
42
+ // Use a terminal for testing:
43
+ // $ siege -b -r 10 http://127.0.0.1:8000/
Original file line number Diff line number Diff line change
1
+ ```
2
+ $ npm install redis
3
+ ```
Original file line number Diff line number Diff line change
1
+ @{layout('')}
2
+
3
+ <!DOCTYPE html>
4
+ < html >
5
+ < head >
6
+ < title > node session + redis + cluster</ title >
7
+ < meta charset ="utf-8 " />
8
+ < meta http-equiv ="X-UA-Compatible " content ="IE=10 " />
9
+ < meta name ="format-detection " content ="telephone=no "/>
10
+ < meta name ="viewport " content ="width=1024, user-scalable=yes " />
11
+ < meta name ="robots " content ="all,follow " />
12
+ </ head >
13
+ < body >
14
+ Counter: @{session.counter}
15
+ </ body >
16
+ </ html >
You can’t perform that action at this time.
0 commit comments