Compare commits

..

No commits in common. "master" and "68663787bbe1cca6b6e76200098678794f292a1c" have entirely different histories.

2 changed files with 39 additions and 159 deletions

View File

@ -3,133 +3,20 @@ const Dragonfly = global.Dragonfly;
const Redis = require( "redis" ); const Redis = require( "redis" );
const SessConf = cl.load( "config.all" ).sx.modular.session; const SessConf = cl.load( "config.sx.modular.session" );
class ClientCompat var Client = Redis.createClient(
SessConf.config.port
, SessConf.config.host
, SessConf.config.options
);
Client.addListener( "error", ( e ) => { throw e; } );
Client.select( SessConf.config.database, function( err, message )
{ {
constructor() if( err ) throw err;
{ Dragonfly.Info( "[Session] Database connected. Ready." );
this.client = Redis.createClient({
"url": `redis://${SessConf.config.host}:${SessConf.config.port}/${SessConf.config.database}`
}); });
this.connecting = false; module.exports = Client;
}
_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,11 +3,12 @@
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.all" ).sx.modular.session; const SessConf = cl.load( "config.sx.modular.session" );
class SessionEventArgs class SessionEventArgs
{ {
@ -39,15 +40,13 @@ class Session extends EventEmitter
this.ready = false; this.ready = false;
this.exists = false; this.exists = false;
Client.HGETALL( this.id, function( obj, err ) Client.HGETALL( this.id, function( err, obj )
{ {
if( err ) throw err; if( err ) throw err;
if( _self.exists = !!Object.keys(obj).length ) if( _self.exists = !!obj )
{ {
_self.__sess = {}; _self.__sess = obj;
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 )
@ -60,8 +59,8 @@ class Session extends EventEmitter
} ); } );
this.__sess = {}; this.__sess = {};
this.__emitOk = ( m, err ) => { this.__emitOk = ( e, m ) => {
_self.__emit( err, "set", new SessionEventArgs( m ) ); _self.__emit( e, "set", new SessionEventArgs( m ) );
}; };
} }
@ -79,13 +78,11 @@ class Session extends EventEmitter
this.__sess[ "lifespan" ] = ttl; this.__sess[ "lifespan" ] = ttl;
var _self = this; var _self = this;
Client.compatExec(
Client.multi() Client.multi()
.HSET( this.id, "spawn", new Date().getTime() ) .HSET( this.id, "spawn", new Date() )
.HSET( this.id, "lifespan", ttl ) .HSET( this.id, "lifespan", ttl )
.EXPIRE( this.id, ttl ) .EXPIRE( this.id, ttl )
, handler || this.__emitOk .exec( handler || this.__emitOk );
);
} }
destroy( handler ) destroy( handler )
@ -94,17 +91,10 @@ class Session extends EventEmitter
Client.DEL( this.id, handler || this.__emitOk ); Client.DEL( this.id, handler || this.__emitOk );
} }
set( dict, handler ) set( name, val )
{ {
var chain = Client.multi(); this.__sess[ name ] = val;
for( let k in dict ) Client.HSET( this.id, name, val, this.__emitOk );
{
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 )
@ -118,7 +108,7 @@ class Session extends EventEmitter
var _self = this; var _self = this;
Client.HGET( Client.HGET(
this.id, name this.id, name
, function( rep, err ) , function( err, rep )
{ {
_self.__emitOk( err ); _self.__emitOk( err );
_self.__sess[ name ] = rep; _self.__sess[ name ] = rep;
@ -145,12 +135,15 @@ class Session extends EventEmitter
this.__sess[ "lifespan" ] = ttl; this.__sess[ "lifespan" ] = ttl;
Client.compatExec(
Client.multi() Client.multi()
.HSET( this.id, "lifespan", ttl ) .HSET( this.id, "lifespan", ttl )
.EXPIRE( this.id, ttl ) .EXPIRE( this.id, ttl, this.__emitOk )
, this.__emitOk .exec( this.__emitOk );
); }
get busy()
{
return 0 < ( Client.command_queue.length + Client.offline_queue.length );
} }
} }