Compare commits

..

12 Commits

2 changed files with 159 additions and 39 deletions

View File

@ -3,20 +3,133 @@ const Dragonfly = global.Dragonfly;
const Redis = require( "redis" ); const Redis = require( "redis" );
const SessConf = cl.load( "config.sx.modular.session" ); const SessConf = cl.load( "config.all" ).sx.modular.session;
var Client = Redis.createClient( class ClientCompat
SessConf.config.port
, SessConf.config.host
, SessConf.config.options
);
Client.addListener( "error", ( e ) => { throw e; } );
Client.select( SessConf.config.database, function( err, message )
{ {
if( err ) throw err; constructor()
Dragonfly.Info( "[Session] Database connected. Ready." ); {
}); this.client = Redis.createClient({
"url": `redis://${SessConf.config.host}:${SessConf.config.port}/${SessConf.config.database}`
});
module.exports = Client; this.connecting = false;
}
_nop() { }
async _connect()
{
if( this.client.isReady || this.connecting )
return;
this.connecting = true;
await this.client.connect();
this.connecting = false;
}
async GET( key, handler )
{
handler = handler || this._nop;
try
{
await this._connect();
handler( await this.client.GET( key ) );
}
catch( error )
{
handler( undefined, error );
}
}
async HGET( key, field, handler )
{
handler = handler || this._nop;
try
{
await this._connect();
handler( await this.client.HGET( key, field ) );
}
catch( error )
{
handler( undefined, error );
}
}
async HGETALL( key, handler )
{
handler = handler || this._nop;
try
{
await this._connect();
handler( await this.client.HGETALL( key ) );
}
catch( error )
{
handler( undefined, error );
}
}
async DEL( key, handler )
{
handler = handler || this._nop;
try
{
await this._connect();
handler( await this.client.DEL( key ) );
}
catch( error )
{
handler( undefined, error );
}
}
async HDEL( key, field, handler )
{
handler = handler || this._nop;
try
{
await this._connect();
handler( await this.client.HDEL( key, field ) );
}
catch( error )
{
handler( undefined, error );
}
}
async TTL( key, handler )
{
handler = handler || this._nop;
try
{
await this._connect();
handler( await this.client.TTL( key ) );
}
catch( error )
{
handler( undefined, error );
}
}
async compatExec( multi, handler )
{
handler = handler || this._nop;
try
{
await this._connect();
handler( await multi.exec() );
}
catch( error )
{
handler( undefined, error );
}
}
multi()
{
return this.client.multi();
}
}
module.exports = new ClientCompat();

View File

@ -3,12 +3,11 @@
const cl = global.botanLoader; const cl = global.botanLoader;
const Dragonfly = global.Dragonfly; const Dragonfly = global.Dragonfly;
const util = require( "util" );
const EventEmitter = require( "events" ).EventEmitter; const EventEmitter = require( "events" ).EventEmitter;
const Client = cl.load( "botansx.modular.redisclient" ); const Client = cl.load( "botansx.modular.redisclient" );
const rand = cl.load( "botansx.utils.random" ); const rand = cl.load( "botansx.utils.random" );
const SessConf = cl.load( "config.sx.modular.session" ); const SessConf = cl.load( "config.all" ).sx.modular.session;
class SessionEventArgs class SessionEventArgs
{ {
@ -40,13 +39,15 @@ class Session extends EventEmitter
this.ready = false; this.ready = false;
this.exists = false; this.exists = false;
Client.HGETALL( this.id, function( err, obj ) Client.HGETALL( this.id, function( obj, err )
{ {
if( err ) throw err; if( err ) throw err;
if( _self.exists = !!obj ) if( _self.exists = !!Object.keys(obj).length )
{ {
_self.__sess = obj; _self.__sess = {};
for( var i in obj )
_self.__sess[i] = obj[i];
} }
// Auto reset the session id if no match // Auto reset the session id if no match
else if( !noIdReset ) else if( !noIdReset )
@ -59,8 +60,8 @@ class Session extends EventEmitter
} ); } );
this.__sess = {}; this.__sess = {};
this.__emitOk = ( e, m ) => { this.__emitOk = ( m, err ) => {
_self.__emit( e, "set", new SessionEventArgs( m ) ); _self.__emit( err, "set", new SessionEventArgs( m ) );
}; };
} }
@ -78,11 +79,13 @@ class Session extends EventEmitter
this.__sess[ "lifespan" ] = ttl; this.__sess[ "lifespan" ] = ttl;
var _self = this; var _self = this;
Client.multi() Client.compatExec(
.HSET( this.id, "spawn", new Date() ) Client.multi()
.HSET( this.id, "lifespan", ttl ) .HSET( this.id, "spawn", new Date().getTime() )
.EXPIRE( this.id, ttl ) .HSET( this.id, "lifespan", ttl )
.exec( handler || this.__emitOk ); .EXPIRE( this.id, ttl )
, handler || this.__emitOk
);
} }
destroy( handler ) destroy( handler )
@ -91,10 +94,17 @@ class Session extends EventEmitter
Client.DEL( this.id, handler || this.__emitOk ); Client.DEL( this.id, handler || this.__emitOk );
} }
set( name, val ) set( dict, handler )
{ {
this.__sess[ name ] = val; var chain = Client.multi();
Client.HSET( this.id, name, val, this.__emitOk ); for( let k in dict )
{
var v = dict[k];
this.__sess[ k ] = v;
chain = chain.HSET( this.id, k, v );
}
Client.compatExec( chain, handler || this.__emitOk );
} }
get( name ) get( name )
@ -108,7 +118,7 @@ class Session extends EventEmitter
var _self = this; var _self = this;
Client.HGET( Client.HGET(
this.id, name this.id, name
, function( err, rep ) , function( rep, err )
{ {
_self.__emitOk( err ); _self.__emitOk( err );
_self.__sess[ name ] = rep; _self.__sess[ name ] = rep;
@ -135,15 +145,12 @@ class Session extends EventEmitter
this.__sess[ "lifespan" ] = ttl; this.__sess[ "lifespan" ] = ttl;
Client.multi() Client.compatExec(
.HSET( this.id, "lifespan", ttl ) Client.multi()
.EXPIRE( this.id, ttl, this.__emitOk ) .HSET( this.id, "lifespan", ttl )
.exec( this.__emitOk ); .EXPIRE( this.id, ttl )
} , this.__emitOk
);
get busy()
{
return 0 < ( Client.command_queue.length + Client.offline_queue.length );
} }
} }