其他特性
7.1. 操作节点
Redisson 节点组对象提供了一些对Redis节点的控制。
NodesGroup nodesGroup = redisson.getNodesGroup();
nodesGroup.addConnectionListener(new ConnectionListener() {
public void onConnect(InetSocketAddress addr) {
// Redis server connected
}
public void onDisconnect(InetSocketAddress addr) {
// Redis server disconnected
}
});
可以ping单独节点或者是所有其他节点。
NodesGroup nodesGroup = redisson.getNodesGroup();
Collection<Node> allNodes = nodesGroup.getNodes();
for (Node n : allNodes) {
n.ping();
}
// or
nodesGroup.pingAll();
7.2. 批量执行命令
多个命令可以在一次执行中被一起发送通过RBatch
对象作为一次网络请求。通过使用这个对象你能够减少命令组的执行时间。在Redis中这个方法是通过调用Pipelining.
RBatch batch = redisson.createBatch();
batch.getMap("test").fastPutAsync("1", "2");
batch.getMap("test").fastPutAsync("2", "3");
batch.getMap("test").putAsync("2", "5");
batch.getAtomicLongAsync("counter").incrementAndGetAsync();
batch.getAtomicLongAsync("counter").incrementAndGetAsync();
List<?> res = batch.execute();
7.3. 脚步
redisson.getBucket("foo").set("bar");
String r = redisson.getScript().eval(Mode.READ_ONLY,
"return redis.call('get', 'foo')", RScript.ReturnType.VALUE);
// do the same using cache
RScript s = redisson.getScript();
// load script into cache to all redis master instances
String res = s.scriptLoad("return redis.call('get', 'foo')");
// res == 282297a0228f48cd3fc6a55de6316f31422f5d17
// call script by sha digest
Future<Object> r1 = redisson.getScript().evalShaAsync(Mode.READ_ONLY,
"282297a0228f48cd3fc6a55de6316f31422f5d17",
RScript.ReturnType.VALUE, Collections.emptyList());
7.4. Spring cache 集成
Redisson 完全支持 Spring Cache Abstraction。每一个缓存示例都有两个重要的参数: ttl
和 maxIdleTime
,如果没有定义或者是使用0表示这个存储数据是无限的。
配置示例:
@Configuration
@ComponentScan
@EnableCaching
public static class Application {
@Bean(destroyMethod="shutdown")
RedissonClient redisson() throws IOException {
Config config = new Config();
config.useClusterServers()
.addNodeAddress("127.0.0.1:7004", "127.0.0.1:7001");
return Redisson.create(config);
}
@Bean
CacheManager cacheManager(RedissonClient redissonClient) {
Map<String, CacheConfig> config = new HashMap<String, CacheConfig>();
config.put("testMap", new CacheConfig(24*60*1000, 12*60*1000));
return new RedissonSpringCacheManager(redissonClient, config);
}
}
缓存配置可以从JSON或者是YAML格式的配置文件中读取:
@Configuration
@ComponentScan
@EnableCaching
public static class Application {
@Bean(destroyMethod="shutdown")
RedissonClient redisson(@Value("classpath:/redisson.json") Resource configFile) throws IOException {
Config config = Config.fromJSON(configFile.getInputStream());
return Redisson.create(config);
}
@Bean
CacheManager cacheManager(RedissonClient redissonClient) throws IOException {
return new RedissonSpringCacheManager(redissonClient, "classpath:/cache-config.json");
}
}
7.5. 底层的Redis客户端
Redisson 使用的是高效的异步,无锁实现的 Redis 客户端。 它支持同步和异步两种模式。此外你可能想执行一个命令还没有被Redisson支持的。此外你可以通过在Redis 命令映射列表中找到你需要的命令在你使用底层的客户端的时候。
RedisClient client = new RedisClient("localhost", 6379);
RedisConnection conn = client.connect();
//or
Future<RedisConnection> connFuture = client.connectAsync();
conn.sync(StringCodec.INSTANCE, RedisCommands.SET, "test", 0);
conn.async(StringCodec.INSTANCE, RedisCommands.GET, "test");
conn.sync(RedisCommands.PING);
conn.close()
// or
conn.closeAsync()
client.shutdown();
// or
client.shutdownAsync();