其它特性

操作节点

Redisson NodesGroup 对象提供了对 Redis 节点的一些控制:

  1. NodesGroup nodesGroup = redisson.getNodesGroup();
  2. nodesGroup.addConnectionListener(new ConnectionListener() {
  3. public void onConnect(InetSocketAddress addr) {
  4. // Redis server connected
  5. }
  6. public void onDisconnect(InetSocketAddress addr) {
  7. // Redis server disconnected
  8. }
  9. });

允许给单个或所有 Redis 服务器发送 ping 请求:

  1. NodesGroup nodesGroup = redisson.getNodesGroup();
  2. Collection<Node> allNodes = nodesGroup.getNodes();
  3. for (Node n : allNodes) {
  4. n.ping();
  5. }
  6. // or
  7. nodesGroup.pingAll();

执行批量命令

通过 RBatch 对象可以将多个命令汇总到一个网络调用中一次性发送并执行。 通过这个对象你可以一组命令的执行时间。 在 Redis 中这种方式称为 Pipeling

  1. RBatch batch = redisson.createBatch();
  2. batch.getMap("test").fastPutAsync("1", "2");
  3. batch.getMap("test").fastPutAsync("2", "3");
  4. batch.getMap("test").putAsync("2", "5");
  5. batch.getAtomicLongAsync("counter").incrementAndGetAsync();
  6. batch.getAtomicLongAsync("counter").incrementAndGetAsync();
  7. List<?> res = batch.execute();

脚本

  1. redisson.getBucket("foo").set("bar");
  2. String r = redisson.getScript().eval(Mode.READ_ONLY,
  3. "return redis.call('get', 'foo')", RScript.ReturnType.VALUE);
  4. // do the same using cache
  5. RScript s = redisson.getScript();
  6. // load script into cache to all redis master instances
  7. String res = s.scriptLoad("return redis.call('get', 'foo')");
  8. // res == 282297a0228f48cd3fc6a55de6316f31422f5d17
  9. // call script by sha digest
  10. Future<Object> r1 = redisson.getScript().evalShaAsync(Mode.READ_ONLY,
  11. "282297a0228f48cd3fc6a55de6316f31422f5d17",
  12. RScript.ReturnType.VALUE, Collections.emptyList());

Spring Cache 集成

Redisson 完全支持 Spring Cache 抽象. 每个 Cache 实例具有两个重要参数: ttlmaxIdleTime, 且在它们没有定义或者等于 0 时会永久存储。 配置示例:

  1. @Configuration
  2. @ComponentScan
  3. @EnableCaching
  4. public static class Application {
  5. @Bean(destroyMethod="shutdown")
  6. RedissonClient redisson() throws IOException {
  7. Config config = new Config();
  8. config.useClusterServers()
  9. .addNodeAddress("127.0.0.1:7004", "127.0.0.1:7001");
  10. return Redisson.create(config);
  11. }
  12. @Bean
  13. CacheManager cacheManager(RedissonClient redissonClient) {
  14. Map<String, CacheConfig> config = new HashMap<String, CacheConfig>();
  15. config.put("testMap", new CacheConfig(24*60*1000, 12*60*1000));
  16. return new RedissonSpringCacheManager(redissonClient, config);
  17. }
  18. }

Cache 配置也可以从 JSON 或 YAML 配置文件中读取:

  1. @Configuration
  2. @ComponentScan
  3. @EnableCaching
  4. public static class Application {
  5. @Bean(destroyMethod="shutdown")
  6. RedissonClient redisson(@Value("classpath:/redisson.json") Resource configFile) throws IOException {
  7. Config config = Config.fromJSON(configFile.getInputStream());
  8. return Redisson.create(config);
  9. }
  10. @Bean
  11. CacheManager cacheManager(RedissonClient redissonClient) throws IOException {
  12. return new RedissonSpringCacheManager(redissonClient, "classpath:/cache-config.json");
  13. }
  14. }

底层 Redis 客户端

Redisson 使用高性能异步且无锁的 Redis 客户端。 它支持异步和同步模式。 如果 Redisson 尚不支持,你可以通过它自行执行命令。 当然,在使用底层客户端之前,你可能想在 Redis命令映射 查询一下。

  1. RedisClient client = new RedisClient("localhost", 6379);
  2. RedisConnection conn = client.connect();
  3. //or
  4. Future<RedisConnection> connFuture = client.connectAsync();
  5. conn.sync(StringCodec.INSTANCE, RedisCommands.SET, "test", 0);
  6. conn.async(StringCodec.INSTANCE, RedisCommands.GET, "test");
  7. conn.sync(RedisCommands.PING);
  8. conn.close()
  9. // or
  10. conn.closeAsync()
  11. client.shutdown();
  12. // or
  13. client.shutdownAsync();