Redis在压力测试下挂钩(发布 - 订阅) - 负载下的性能

时间:2015-06-17 14:38:41

标签: redis hook publish-subscribe jedis subscribe

根据suggested solusion并在example之后,我正在尝试在收到另一个密钥已过期的通知后立即删除密钥。

问题是在压力测试下,在重载时设置600K新密钥并设置其中一半,到期时间为2秒,我得到以下异常:

Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Unknown reply: t

问题是写这样的听众的最佳做法是什么? (线程池?如果是这样,在什么上下文中实现它?)

Jedis版本:2.7.2

Redis版本:2.8.19

到目前为止我的代码:

订阅者类:

public class Subscriber {

    public static void main(String[] args) {
        JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");

        Jedis jedis = pool.getResource();
        jedis.psubscribe(new KeyExpiredListener(), "__key*__:*");

    }

}

听众课程:

public class KeyExpiredListener extends JedisPubSub {

    private String generalKeyTimeoutPrefix = "TO_";

    @Override
    public void onPMessage(String pattern, String channel, String message) {
        String originalKey = null;
        try {
            if(channel.endsWith("__keyevent@0__:expired") && message.startsWith(generalKeyTimeoutPrefix)) {
                originalKey = message.substring(generalKeyTimeoutPrefix.length());
                del(originalKey);
            }
        } catch(Exception e) {
            logger.error("..", e);
        }
    }

    private void del(String key) {
        Jedis jedis = new Jedis("localhost");
        jedis.connect();
        try {
            jedis.del(key);
        } catch (Exception e) {
            logger.error("...");
        } finally {
            jedis.disconnect();
        }
    }
}

密钥生成器:

public class TestJedis {
    public static void main(String[] args) throws InterruptedException {
        JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");
        Jedis jedis = pool.getResource();
        String prefixForlKeys = "token_";
        String prefixForTimeoutKeys = "TO_";
        for (int i = 0; i < 300000; i++) {
            String key = prefixForlKeys + i;
            String timeoutKey = prefixForTimeoutKeys + key;
            jedis.set(key, "some_data");
            jedis.set(timeoutKey, "");
            jedis.expire(timeoutKey, 2);
        }
      System.out.println("Finished to create the keys");  
    }
}

1 个答案:

答案 0 :(得分:1)

问题在于你的del()方法的实现:它不使用连接池,不重用连接,所以它最终占用所有可用的本地端口。尝试使用这样的东西:

private void del(String key) {
    Jedis jedis = pool.getResource();
    jedis.connect();
    try {
        jedis.del(key);
    } catch (Exception e) {
        e.printStackTrace(  );
    } finally {
        jedis.close();
    }
}

而不是为每个过期的密钥打开/关闭连接。