Compare commits
No commits in common. "master" and "68663787bbe1cca6b6e76200098678794f292a1c" have entirely different histories.
master
...
68663787bb
141
redisclient.js
141
redisclient.js
@ -3,133 +3,20 @@ const Dragonfly = global.Dragonfly;
|
||||
|
||||
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()
|
||||
{
|
||||
this.client = Redis.createClient({
|
||||
"url": `redis://${SessConf.config.host}:${SessConf.config.port}/${SessConf.config.database}`
|
||||
});
|
||||
if( err ) throw err;
|
||||
Dragonfly.Info( "[Session] Database connected. Ready." );
|
||||
});
|
||||
|
||||
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();
|
||||
module.exports = Client;
|
||||
|
57
session.js
57
session.js
@ -3,11 +3,12 @@
|
||||
const cl = global.botanLoader;
|
||||
const Dragonfly = global.Dragonfly;
|
||||
|
||||
const util = require( "util" );
|
||||
const EventEmitter = require( "events" ).EventEmitter;
|
||||
|
||||
const Client = cl.load( "botansx.modular.redisclient" );
|
||||
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
|
||||
{
|
||||
@ -39,15 +40,13 @@ class Session extends EventEmitter
|
||||
this.ready = false;
|
||||
this.exists = false;
|
||||
|
||||
Client.HGETALL( this.id, function( obj, err )
|
||||
Client.HGETALL( this.id, function( err, obj )
|
||||
{
|
||||
if( err ) throw err;
|
||||
|
||||
if( _self.exists = !!Object.keys(obj).length )
|
||||
if( _self.exists = !!obj )
|
||||
{
|
||||
_self.__sess = {};
|
||||
for( var i in obj )
|
||||
_self.__sess[i] = obj[i];
|
||||
_self.__sess = obj;
|
||||
}
|
||||
// Auto reset the session id if no match
|
||||
else if( !noIdReset )
|
||||
@ -60,8 +59,8 @@ class Session extends EventEmitter
|
||||
} );
|
||||
|
||||
this.__sess = {};
|
||||
this.__emitOk = ( m, err ) => {
|
||||
_self.__emit( err, "set", new SessionEventArgs( m ) );
|
||||
this.__emitOk = ( e, m ) => {
|
||||
_self.__emit( e, "set", new SessionEventArgs( m ) );
|
||||
};
|
||||
}
|
||||
|
||||
@ -79,13 +78,11 @@ class Session extends EventEmitter
|
||||
this.__sess[ "lifespan" ] = ttl;
|
||||
|
||||
var _self = this;
|
||||
Client.compatExec(
|
||||
Client.multi()
|
||||
.HSET( this.id, "spawn", new Date().getTime() )
|
||||
.HSET( this.id, "lifespan", ttl )
|
||||
.EXPIRE( this.id, ttl )
|
||||
, handler || this.__emitOk
|
||||
);
|
||||
Client.multi()
|
||||
.HSET( this.id, "spawn", new Date() )
|
||||
.HSET( this.id, "lifespan", ttl )
|
||||
.EXPIRE( this.id, ttl )
|
||||
.exec( handler || this.__emitOk );
|
||||
}
|
||||
|
||||
destroy( handler )
|
||||
@ -94,17 +91,10 @@ class Session extends EventEmitter
|
||||
Client.DEL( this.id, handler || this.__emitOk );
|
||||
}
|
||||
|
||||
set( dict, handler )
|
||||
set( name, val )
|
||||
{
|
||||
var chain = Client.multi();
|
||||
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 );
|
||||
this.__sess[ name ] = val;
|
||||
Client.HSET( this.id, name, val, this.__emitOk );
|
||||
}
|
||||
|
||||
get( name )
|
||||
@ -118,7 +108,7 @@ class Session extends EventEmitter
|
||||
var _self = this;
|
||||
Client.HGET(
|
||||
this.id, name
|
||||
, function( rep, err )
|
||||
, function( err, rep )
|
||||
{
|
||||
_self.__emitOk( err );
|
||||
_self.__sess[ name ] = rep;
|
||||
@ -145,12 +135,15 @@ class Session extends EventEmitter
|
||||
|
||||
this.__sess[ "lifespan" ] = ttl;
|
||||
|
||||
Client.compatExec(
|
||||
Client.multi()
|
||||
.HSET( this.id, "lifespan", ttl )
|
||||
.EXPIRE( this.id, ttl )
|
||||
, this.__emitOk
|
||||
);
|
||||
Client.multi()
|
||||
.HSET( this.id, "lifespan", ttl )
|
||||
.EXPIRE( this.id, ttl, this.__emitOk )
|
||||
.exec( this.__emitOk );
|
||||
}
|
||||
|
||||
get busy()
|
||||
{
|
||||
return 0 < ( Client.command_queue.length + Client.offline_queue.length );
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user