Py学习  »  Redis

如何设置redis请求的节点超时

bravinator932421 • 5 年前 • 1782 次点击  

我编写了一个简单的服务,使用redis将数据存储在内存中,或者从磁盘中提取数据,然后存储在内存中。我现在正试图控制一些罕见的情况,在这些情况下,获取redis是很慢的。我见过一个例子( https://gist.github.com/stockholmux/3a4b2d1480f27df8be67#file-timelimitedredis-js )这似乎解决了这个问题,但我在实施过程中遇到了困难。

相关的实施是:

/**
 * returns a function that acts like the Redis command indicated by cmd except that it will time out after a given number of milliseconds
 * 
 * @param {string} cmd The redis commmand to execute ('get','hset','sort', etc.)
 * @param {integer} timeLimit The number of milliseconds to wait until returning an error to the callback.
 * 
 */
function timeLimited(cmd, timeLimit) {
  return function() {
    var
      argsAsArr = Array.prototype.slice.call(arguments),
      cb        = argsAsArr.pop(),
      timeoutHandler;

    timeoutHandler = setTimeout(function(){
      cb(new Error('Redis timed out'));
      cb = function() {};
    }, timeLimit);

    argsAsArr.push(function(err, values){
      clearTimeout(timeoutHandler);
      cb(err,values);
    });

    client[cmd].apply(client,argsAsArr);
  };
}

但是,我不知道如何实现这一点,因为客户端从未定义,redis键/值也从未传入。有人能解释一下如何实施这个例子吗?我一直在寻找更多的信息或一个有效的例子,但至今没有任何运气。谢谢您。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/47820
 
1782 次点击  
文章 [ 1 ]  |  最新文章 5 年前