Javascript 如何在nodejs中编码获取池的处理释放?
我想在java中实现像AOP这样的通用函数。例如,我有一个redis池,acquire processing release是这样的:Javascript 如何在nodejs中编码获取池的处理释放?,javascript,node.js,Javascript,Node.js,我想在java中实现像AOP这样的通用函数。例如,我有一个redis池,acquire processing release是这样的: public class JedisInterceptor implements MethodInterceptor{ @Inject private Session<Jedis> session; @Override public Object invoke(MethodInvocation invocation) throws Throwab
public class JedisInterceptor implements MethodInterceptor{
@Inject private Session<Jedis> session;
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
if(!session.isClosed()) return invocation.proceed();
try {
//acquire connection from pool
session.start();
return invocation.proceed();
} catch (Throwable e) {
throw e;
} finally {
if(!session.isClosed()) {
//always release connection no matter there's exception or not.
session.close();
}
}
}
然后我可以在类似bellowed的地方使用这个函数,并且不需要担心连接处理
var value = Utils.redis(function(redis){
return redis.get('something')
});
npm上的
redis
模块会自动进行管道传输,因此不需要使用池。您有什么用例需要节点中的redis池?@mscdex自动流水线非常好!我想使用一种常用的方法来管理redis连接周期,然后我可以在任何地方使用这种方法。当然,我可以用另一种方法丢弃游泳池。我只想管理连接周期。
var value = Utils.redis(function(redis){
return redis.get('something')
});