ext-modular/redisclient.js

136 lines
2.1 KiB
JavaScript

const cl = global.botanLoader;
const Dragonfly = global.Dragonfly;
const Redis = require( "redis" );
const SessConf = cl.load( "config.all" ).sx.modular.session;
class ClientCompat
{
constructor()
{
this.client = Redis.createClient({
"url": `redis://${SessConf.config.host}:${SessConf.config.port}/${SessConf.config.database}`
});
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();