Jedisをredis-clientとして使用して、Javaでredisを使用しています。クラスがあります
public class RedisDBManager
Jedisを呼び出してredisでコマンドを実行するメソッドがあります。
RedisDBManager内のサンプル メソッド
public Set<NTuple> zrangeWithScores(String key, int min, int max)
{
JedisSentinelPool localPool = redisSentinelPool;
Jedis redis = null;
try
{
redis = getResource();
Set<Tuple> tupleSet = redis.zrangeWithScores(key, min, max);
Set<NTuple> tuples = new LinkedHashSet<>();
for (Tuple tuple : tupleSet) {
tuples.add(new NTuple(tuple.getElement(), tuple.getScore()));
}
return tuples;
}
catch (JedisConnectionException jex) {
logger.error("Creating new connection since it encountered Jedis Connection Exception: ",jex);
createNewConnectionPool(localPool);
try {
redis = getResource();
Set<Tuple> tupleSet = redis.zrangeWithScores(key, min, max);
Set<NTuple> tuples = new LinkedHashSet<>();
for (Tuple tuple : tupleSet) {
tuples.add(new NTuple(tuple.getElement(), tuple.getScore()));
}
return tuples;
}
catch (Exception e){
logger.error("Exception: ", e);
return null;
}
}
catch (Exception ex) {
logger.error("Exception: ", ex);
return null;
}
finally
{
if (redis != null)
{
returnResource(redis);
}
}
}
ここで、メソッド getResource はJedisSentinelPoolからリソースを返します。
実行するコマンドのリストを受け取り、応答をリストとして返すように、このクラス内にパイプライン メソッドが必要です。外部のメソッドでは、すべての責任を処理するパイプライン メソッドを呼び出す必要があるため、Jedis の構成をRedisDBManagerの外部で使用しないでください。
この質問は、この質問に似ています。さまざまな redis コマンドも使用して、それらの応答を取得したいという点で異なります。
私の現在の不完全なアプローチは、RedisDBManager のすべてのメソッドを変更して、それをスレッド ローカルの Pipeline オブジェクトにパイプライン処理するかどうかを受け入れてから、このパイプライン オブジェクトを同期して応答を返すパイプライン メソッドを用意することです。
何かのようなもの :
public Set<NTuple> zrangeWithScores(String key, int min, int max, boolean pipelined) {
...
try
{
if (pipelined) {
pipeline = getExistingThreadLocalPipelineObject();
pipeline.zrangeWithScores(key, min, max);
} else {
redis = getResource();
...
return tuples;
}
catch (JedisConnectionException jex) {
...
if (pipelined) {
pipeline = getExistingThreadLocalPipelineObject();
pipeline.zrangeWithScores(key, min, max);
} else {
redis = getResource();
...
return tuples;
...
}
public List<MyResponse> syncPipeline() {
pipeline = getExistingThreadLocalPipelineObject();
pipeline.sync();
//process all responses and send
}
より良い、またはより簡単なアプローチはありますか?ありがとう。