using Tora.share with persist enabled

Introduction

Applications in Tora application server can share data with persist function.

Tora.Persist allows you to 'nake' the objects from their prototype,
keeping only the class name and its data (recursively), and instance
them back on read. It should be faster than serializer which requires
the whole data to be converted to string, and actually allows direct
mutation of shared values. (to be used with tora.Share for shared memory)

Simple test example of using Tora.share

shared data:

//Tora only supports user defined class with rtti as shared data 
//if persist funcition is enabled.
@:rtti
class MyClass {
      public var content : Array<Int>;
      public function new() {
            this.content = [1,2,3];
       }
}

first application code:

class App {
   
    static var share    = new tora.Share<MyClass>( "test", function() {
        return new MyClass();
    }, MyClass);
   
    static function main() {
        var a:MyClass    = share.get( true );
        a.content.push(5);
        Sys.println("App "+a.content);
        share.set(a); //have to set if persist is enabled, otherwise the change will be lost.
        share.commit();       
    }
}

second application code:

class App2 {
   
    static var share    = new tora.Share<MyClass>( "test", function() {
        return new MyClass();
    }, MyClass);
   
   
    static function main() {
        var a:MyClass    = share.get( true );
        a.content.push(10);
        Sys.println("App2 share"+a.content);
        share.set(a); //have to set if persist is enabled, otherwise the change will be lost.
        share.commit();               
    }
}

Compile with:

haxe -neko app.n -main App -lib tora
haxe -neko app2.n -main App2 -lib tora

Testing

You can test Tora with nginx http server with fastcgi mode, more details on Sam's blog.

put app.n and app2.n into your nginx html path, my path is /nginx-1.4.3/html.

and add the following config info into http server section of the nginx.config file, my path is /nginx-1.4.3/conf.

        location /app {
          fastcgi_pass localhost:6666;
          fastcgi_param SCRIPT_FILENAME $document_root/app.n;
          fastcgi_param REQUEST_METHOD $request_method;
          fastcgi_param QUERY_STRING q=$uri&$query_string;
          fastcgi_param CONTENT_TYPE $content_type;
          fastcgi_param CONTENT_LENGTH $content_length;
          fastcgi_param REMOTE_ADDR $remote_addr;
          fastcgi_param REMOTE_PORT $remote_port;
          fastcgi_param SERVER_ADDR $server_addr;
          fastcgi_param SERVER_PORT $server_port;
          fastcgi_param SERVER_NAME $server_name;
        }
        
        location /app2 {
          fastcgi_pass localhost:6666;
          fastcgi_param SCRIPT_FILENAME $document_root/app2.n;
          fastcgi_param REQUEST_METHOD $request_method;
          fastcgi_param QUERY_STRING q=$uri&$query_string;
          fastcgi_param CONTENT_TYPE $content_type;
          fastcgi_param CONTENT_LENGTH $content_length;
          fastcgi_param REMOTE_ADDR $remote_addr;
          fastcgi_param REMOTE_PORT $remote_port;
          fastcgi_param SERVER_ADDR $server_addr;
          fastcgi_param SERVER_PORT $server_port;
          fastcgi_param SERVER_NAME $server_name;
        }

Start the tora server in one terminal with:

neko tora.n -fcgi

Start the ngnix in another terminal with:

nginx

the first application output should be after several times refresh /localhost/app in your browser:

App [1,2,3,5,5,5]

the second application output should be after several times refresh /localhost/app2 in your browser:

App2 share[1,2,3,5,5,5,10,10,10,10]

Reference

Further reading on Tora can be found at Michal Romecki blog.
neko-cache-module-tora-share
chat-example-tora-queue-comet-style-protocol
haxite-writing-entire-website-using-haxe

version #19867, modified 2013-12-20 02:54:10 by gsun