Java Code Examples for redis.clients.jedis.ShardedJedis#getAllShards()

The following examples show how to use redis.clients.jedis.ShardedJedis#getAllShards() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MarsRedisTemplate.java    From Mars-Java with MIT License 6 votes vote down vote up
/**
 * 模糊查询keys
 *
 * @param pattern
 * @return
 */
public TreeSet<String> keys(String pattern) {
    ShardedJedis jedis = null;
    TreeSet<String> allKeys = new TreeSet<>();

    try {
        jedis = getShardedJedis();
        // 获取所有的缓存实例
        Collection<Jedis> jedisList = jedis.getAllShards();
        for (Jedis jedisItem : jedisList) {
            // 获取匹配prefix的所有的key
            allKeys.addAll(jedisItem.keys(pattern));
        }
    } catch (Exception e) {
        logger.error("获取keys失败", e);
    } finally {
        recycleJedis(jedis);
    }

    return allKeys;
}
 
Example 2
Source File: ShardedJedisTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void checkCloseable() {
  List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
  shards.add(new JedisShardInfo(redis1.getHost(), redis1.getPort()));
  shards.add(new JedisShardInfo(redis2.getHost(), redis2.getPort()));
  shards.get(0).setPassword("foobared");
  shards.get(1).setPassword("foobared");

  ShardedJedis jedisShard = new ShardedJedis(shards);
  try {
    jedisShard.set("shard_closeable", "true");
  } finally {
    jedisShard.close();
  }

  for (Jedis jedis : jedisShard.getAllShards()) {
    assertTrue(!jedis.isConnected());
  }
}
 
Example 3
Source File: RedisClientImpl.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Override
public ScanResult<String> scan(final long cursor, final ScanParams params) {
    ShardedJedis shardedJedis = null;
    try {
        shardedJedis = POOL.getJedis(config.getRedisType());
        final Collection<Jedis> allJedis = shardedJedis.getAllShards();
        if (allJedis.size() == 1) {
            final Jedis jedis = allJedis.iterator().next();
            if (params == null) {
                return jedis.scan(String.valueOf(cursor), DEFAULT_SCAN_PARAMS);
            } else {
                return jedis.scan(String.valueOf(cursor), params);
            }
        } else {
            throw new RedisClientException("不支持对多节点Sharded模式进行Scan操作");
        }
    } catch (final Throwable e) {
        if (e instanceof RedisClientException) {
            throw e;
        }

        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(shardedJedis);
    }
}
 
Example 4
Source File: RedisCommands.java    From jea with Apache License 2.0 6 votes vote down vote up
@Override
public Set<String> keys(String pattern) {
	// TODO Auto-generated method stub
	if (commands instanceof Jedis) {
		return ((Jedis)commands).keys(pattern);
	} else if (commands instanceof ShardedJedis) {
		Set<String> set = new HashSet<String>();
		ShardedJedis shardedJedis = (ShardedJedis)commands;
		Collection<Jedis> jediss = shardedJedis.getAllShards();
		for(Jedis jedis : jediss){
			set.addAll(jedis.keys(pattern));
		}
		return set;
	} else {
		return null;
	}
}
 
Example 5
Source File: ShardedCacheFactory.java    From howsun-javaee-framework with Apache License 2.0 5 votes vote down vote up
public boolean validateObject(final Object obj) {
	try {
		ShardedJedis jedis = (ShardedJedis) obj;
		for (Jedis shard : jedis.getAllShards()) {
			if (!shard.ping().equals("PONG")) {
				return false;
			}
		}
		return true;
	} catch (Exception ex) {
		return false;
	}
}