redis之jedis

redis之jedis

Jedis Test

本地localhost:6397, 直接new Jedis(),父类Connection, 构造函数默认host和port 为localhost + 6397

Screen Shot 2020-09-21 at 2.26.37 PM

public static void jedisTest() {
Jedis jedis = new Jedis();

// String
jedis.set("testkkey1", "testVal1");
String cacheResponse = jedis.get("testkkey1");

System.out.println("Cache Response: " + cacheResponse);

// list
jedis.lpush("tasks", "task1");
jedis.lpush("tasks", "task2");

String tasks = jedis.rpop("tasks");
System.out.println("rpop: " + tasks);
tasks = jedis.rpop("tasks");
System.out.println("rpop: " + tasks);

// set
jedis.sadd("set", "setv1");
jedis.sadd("set", "setv2");
jedis.sadd("set", "setv3");

Set<String> s = jedis.smembers("set");
System.out.println("set values: " + String.join("," , s));

// hash
jedis.hset("userinfo", "subk1", "subv2");
jedis.hset("userinfo", "subk2", "subv2");

Map<String, String> userinfo = jedis.hgetAll("userinfo");
for (Map.Entry<String, String> entry : userinfo.entrySet()) {
System.out.println("entry: " + entry.getKey() + "-" + entry.getValue());
}

// sorted set
Map<String, Double> scores = new HashMap<>();
scores.put("player1", 3000.0);
scores.put("player2", 1000.0);
scores.put("player3", 5000.0);

scores.entrySet().forEach(playerScore -> {
jedis.zadd("ranking", playerScore.getValue(), playerScore.getKey());
});
Set<String> ranking = jedis.zrevrange("ranking", 0, 1);
System.out.println("Player with the highest score : " + ranking.iterator().next());
long rank = jedis.zrevrank("ranking", "player1");
// note: rank is zero based
System.out.println("The rank of player1 is " + rank);

}

JedisPoolTest

参数介绍,参考JedisPool optimization

maxTotal The maximum number of connections that are supported by the pool.

maxIdle The maximum number of idle connections in the pool.

minIdle The minimum number of idle connections in the pool.

blockWhenExhausted Specifies whether the client must wait when the resource pool is exhausted. The following maxWaitMillis parameter takes effect only when this parameter is set to true.

testOnBorrow Specifies whether connections will be validated by using the PING command before they rre borrowed from the pool. Invalid connections will be removed from the pool.

testOnReturn Specifies whether connections will be validated using the PING command before they are returned to the pool. Invalid connections will be removed from the pool.

通过jedisPoll.getResource()来获取connection来执行redis 操作

public static void jedisPoolTest() {

JedisPool jedisPool = new JedisPool(getPoolConfig(), "localhost", 6379);

Jedis jedis;
try {
jedis = jedisPool.getResource();
String cacheResponse = jedis.get("testkkey1");
System.out.println("cacheResponse: " + cacheResponse);
} catch (Exception e) {
e.printStackTrace();
}

}

private static JedisPoolConfig getPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(100);
jedisPoolConfig.setMaxIdle(100);
jedisPoolConfig.setMinIdle(10);
jedisPoolConfig.setTestOnBorrow(true);
jedisPoolConfig.setTestOnReturn(true);
jedisPoolConfig.setTestWhileIdle(true);
jedisPoolConfig.setMinEvictableIdleTimeMillis(Duration.ofSeconds(60).toMillis());
jedisPoolConfig.setTimeBetweenEvictionRunsMillis(Duration.ofSeconds(30).toMillis());
jedisPoolConfig.setNumTestsPerEvictionRun(3);
jedisPoolConfig.setBlockWhenExhausted(true);
return jedisPoolConfig;
}