Skip to content

Commit

Permalink
JedisFlowClient uses jedis pool (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
qdaxb authored Dec 13, 2023
1 parent 0f95442 commit 299002d
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,8 @@ public interface RedisClient {
Object eval(String script, String shardingKey, List<String> keys, List<String> args);
long incr(String key);
long hset(String key, String field, String value);

Set<String> hkeys(String key);

String hget(String key, String field);
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,18 @@ public Object eval(String script, String shardingKey, List<String> keys, List<St
return choose(shardingKey).eval(script, shardingKey, keys, args);
}

@Override
public Set<String> hkeys(String key) {
return choose(key).hkeys(key);
}

@Override
public String hget(String key, String field) {
return choose(key).hget(key, field);
}

private RedisClient choose(byte[] shardingKey) {
return choose(new String(shardingKey, StandardCharsets.UTF_8));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ public String initBucket(String bucketName, JSONObject fieldToValues) {
int reserveTimeInSecond = getReserveTimeInSecond(bucketName);

JedisFlowClient jedisFlowClient = getJedisClient(bucketName);
try (Pipeline pipeline = jedisFlowClient.pipelined()) {
jedisFlowClient.pipelined().accept(pipeline -> {
pipeline.hset(bucketName, "expire", String.valueOf(currentTimeSecond + reserveTimeInSecond));
if (MapUtils.isNotEmpty(fieldToValues)) {
fieldToValues.forEach((field, value) -> pipeline.hset(bucketName, field, value.toString()));
}
pipeline.expire(bucketName, reserveTimeInSecond);
pipeline.sync();
}
});
return bucketName;
}

Expand All @@ -67,10 +67,10 @@ public void store(String bucketName, JSONObject fieldToValues) {
}

JedisFlowClient jedisFlowClient = getJedisClient(bucketName);
try (Pipeline pipeline = jedisFlowClient.pipelined()) {
jedisFlowClient.pipelined().accept(pipeline -> {
fieldToValues.forEach((field, value) -> pipeline.hset(bucketName, field, value.toString()));
pipeline.sync();
}
});
}

public Map<String, String> load(String bucketName, boolean hGetAll, List<String> fieldNames, String fieldPrefix) {
Expand Down Expand Up @@ -105,9 +105,7 @@ public boolean remove(String bucketName) {
public boolean remove(String bucketName, List<String> fieldNames) {
JedisFlowClient jedisFlowClient = getJedisClient(bucketName);
if (CollectionUtils.isNotEmpty(fieldNames)) {
try (Pipeline pipeline = jedisFlowClient.pipelined()) {
fieldNames.forEach(fieldName -> pipeline.hdel(bucketName, fieldName));
}
jedisFlowClient.pipelined().accept(pipeline -> fieldNames.forEach(fieldName -> pipeline.hdel(bucketName, fieldName)));
}
return true;
}
Expand Down
Loading

0 comments on commit 299002d

Please sign in to comment.