Make redis library somewhat compat

This commit is contained in:
斟酌 鵬兄 2023-02-01 06:21:52 +08:00
parent 65ec5b5da8
commit d3e3e95422
2 changed files with 114 additions and 16 deletions

View File

@ -5,11 +5,105 @@ const Redis = require( "redis" );
const SessConf = cl.load( "config.all" ).sx.modular.session; const SessConf = cl.load( "config.all" ).sx.modular.session;
var Client = Redis.createClient({ class ClientCompat
"legacyMode": true {
, "url": `redis://${SessConf.config.host}:${SessConf.config.port}/${SessConf.config.database}` constructor()
}); {
this.client = Redis.createClient({
"url": `redis://${SessConf.config.host}:${SessConf.config.port}/${SessConf.config.database}`
});
}
Client.connect(); async _connect()
{
if( this.client.isReady )
return;
await this.client.connect();
}
module.exports = Client; async HGET( key, field, handler )
{
try
{
await this._connect();
handler( await this.client.HGET( key, field ) );
}
catch( error )
{
handler( undefined, error );
}
}
async HGETALL( key, handler )
{
try
{
await this._connect();
handler( await this.client.HGETALL( key ) );
}
catch( error )
{
handler( undefined, error );
}
}
async DEL( key, handler )
{
try
{
await this._connect();
handler( await this.client.DEL( key ) );
}
catch( error )
{
handler( undefined, error );
}
}
async HDEL( key, field, handler )
{
try
{
await this._connect();
handler( await this.client.HDEL( key, field ) );
}
catch( error )
{
handler( undefined, error );
}
}
async TTL( key, handler )
{
try
{
await this._connect();
handler( await this.client.TTL( key ) );
}
catch( error )
{
handler( undefined, error );
}
}
async compatExec( multi, handler )
{
try
{
await this._connect();
handler( await multi.exec() );
}
catch( error )
{
handler( undefined, error );
}
}
multi()
{
return this.client.multi();
}
}
module.exports = new ClientCompat();

View File

@ -81,11 +81,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 )
@ -104,7 +106,7 @@ class Session extends EventEmitter
chain = chain.HSET( this.id, k, v ); chain = chain.HSET( this.id, k, v );
} }
chain.exec( handler || this.__emitOk ); Client.compatExec( chain, handler || this.__emitOk );
} }
get( name ) get( name )
@ -145,10 +147,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
);
} }
} }