代码拉取完成,页面将自动刷新
Jedipus is a Redis 3.2+ Java 8 client that manages client object pools and command execution.
final RedisClientExecutor rce = RedisClientExecutor.startBuilding()
.createPooled(() -> Node.create("localhost", 6379));
// SET 42 107.6
rce.accept(client -> client.sendCmd(Cmds.SET, "42", "107.6"));
// GET 42
final String temp = rce.apply(client -> client.sendCmd(Cmds.GET, "42"));
if (temp.equals("107.6")) {
System.out.println("Showers' ready, don't forget your towel.");
}
// ...
rce.close();
Consumer<RedisClient>
and Function<RedisClient, R>
lambdas.CLIENT REPLY ON|OFF|SKIP
.RedisClient
pool.Node
-> ClientPool<RedisClient>
factories.LoadBalancedPools
factories. By default, a round robin strategy is used.RedisConnectionException
retry delays per cluster node. By default, an exponential back-off delay is used.Read modes control how pools to master and slave nodes are managed.
ReadMode.MASTER
or ReadMode.MIXED
. When no slave pools are available the master pool is used.ReadMode.MASTER
or ReadMode.SLAVES
. When overriding with ReadMode.SLAVES
and no slave pools are available the master pool is used.UnhandledAskNodeException
is thrown. The reason for this is that even if all of the keys point to the same slot Redis requires a new ASKING request in front of each command. It is cleaner to let the user handle recovery rather than injecting ASKING requests internally. See this integration test for an example of how to recover. MOVE redirects are supported within pipelines.repositories {
jcenter()
}
dependencies {
compile 'com.fabahaba:jedipus:+'
}
Note: The examples auto close the
RedisClusterExecutor
but you probably want it to be a long lived object.
try (final RedisClusterExecutor rce =
RedisClusterExecutor.startBuilding(Node.create("localhost", 7000)).create()) {
final String skey = "skey";
final FutureLongReply numMembers = rce.applyPipeline(skey, pipeline -> {
// Fire-And-Forget: skip() issues a pipelined CLIENT REPLY SKIP
pipeline.skip().sendCmd(Cmds.SADD, skey, "member");
// Optional primitive return types.
final FutureLongReply futureReply = pipeline.sendCmd(Cmds.SCARD.prim(), skey);
pipeline.sync();
// Check reply to leverage library error handling.
return futureReply.checkReply();
});
// This long was never auto boxed.
final long primitiveNumMembers = numMembers.getAsLong();
System.out.format("'%s' has %d members.%n", skey, primitiveNumMembers);
}
try (final RedisClusterExecutor rce =
RedisClusterExecutor.startBuilding(Node.create("localhost", 7000))
.withReadMode(ReadMode.MIXED_SLAVES).create()) {
// Hash tagged pipelined transaction.
final String hashTag = CRC16.createNameSpacedHashTag("HT");
final int slot = CRC16.getSlot(hashTag);
final String fooKey = hashTag + "foo";
// Implicit multi applied.
final Object[] sortedBars = rce.applyPipelinedTransaction(ReadMode.MASTER, slot, pipeline -> {
pipeline.sendCmd(Cmds.ZADD, fooKey, "NX", "-1", "barowitch");
// New key will still be hashtag pinned to the same slot/node.
pipeline.sendCmd(Cmds.ZADD, fooKey + "a", "XX", "-2", "barowitch");
// Handle different ZADD return types with flexible command design.
pipeline.sendCmd(Cmds.ZADD_INCR, fooKey + "b", "XX", "INCR", "-1", "barowitch");
// Utilities to avoid extra array creation.
pipeline.sendCmd(Cmds.ZADD, ZAddParams.fillNX(new byte[][] {RESP.toBytes(fooKey), null,
RESP.toBytes(.37), RESP.toBytes("barinsky")}));
pipeline.sendCmd(Cmds.ZADD, fooKey, "42", "barikoviev");
final FutureReply<Object[]> barsReply =
pipeline.sendCmd(Cmds.ZRANGE_WITHSCORES, fooKey, "0", "-1", "WITHSCORES");
// Note: Pipelines and transactions (multi) are merely started by the the library.
// 'exec' and 'sync' must be called by the user.
pipeline.execSync();
// Note: Replys must be captured within this lambda closure in order to properly
// leverage error handling.
return barsReply.get();
});
// '{HT}:foo': [barowitch (-1.0), barinsky (0.37), barikoviev (42.0)]
System.out.format("%n'%s':", fooKey);
for (int i = 0; i < sortedBars.length;) {
System.out.format(" %s (%s)", RESP.toString(sortedBars[i++]),
RESP.toDouble(sortedBars[i++]));
}
// Optional primitive return types; no auto boxing!
final long numRemoved =
rce.applyPrim(ReadMode.MASTER, slot, client -> client.sendCmd(Cmds.DEL.prim(), fooKey));
System.out.format("%nRemoved %d keys.%n", numRemoved);
}
try (final RedisClusterExecutor rce =
RedisClusterExecutor.startBuilding(Node.create("localhost", 7000))
.withReadMode(ReadMode.MIXED_SLAVES).create()) {
// Ping-Pong all masters.
rce.acceptAllMasters(
master -> System.out.format("%s from %s%n", master.sendCmd(Cmds.PING), master.getNode()));
// Ping-Pong all slaves concurrently.
rce.applyAllSlaves(
slave -> String.format("%s from %s", slave.sendCmd(Cmds.PING, "Howdy"), slave.getNode()),
1, ForkJoinPool.commonPool()).stream().map(CompletableFuture::join)
.forEach(System.out::println);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。