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;
var Client = Redis.createClient({
"legacyMode": true
, "url": `redis://${SessConf.config.host}:${SessConf.config.port}/${SessConf.config.database}`
});
class ClientCompat
{
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;
var _self = this;
Client.multi()
.HSET( this.id, "spawn", new Date() )
.HSET( this.id, "lifespan", ttl )
.EXPIRE( this.id, ttl )
.exec( handler || this.__emitOk );
Client.compatExec(
Client.multi()
.HSET( this.id, "spawn", new Date().getTime() )
.HSET( this.id, "lifespan", ttl )
.EXPIRE( this.id, ttl )
, handler || this.__emitOk
);
}
destroy( handler )
@ -104,7 +106,7 @@ class Session extends EventEmitter
chain = chain.HSET( this.id, k, v );
}
chain.exec( handler || this.__emitOk );
Client.compatExec( chain, handler || this.__emitOk );
}
get( name )
@ -145,10 +147,12 @@ class Session extends EventEmitter
this.__sess[ "lifespan" ] = ttl;
Client.multi()
.HSET( this.id, "lifespan", ttl )
.EXPIRE( this.id, ttl, this.__emitOk )
.exec( this.__emitOk );
Client.compatExec(
Client.multi()
.HSET( this.id, "lifespan", ttl )
.EXPIRE( this.id, ttl )
, this.__emitOk
);
}
}