redis.clients.jedis.ShardedJedis Java Examples

The following examples show how to use redis.clients.jedis.ShardedJedis. 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: RedisClientImpl.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Override
public <T> Map<T, Double> zrangeByScoreWithScores(final String key, final String min, final String max, final int offset, final int count,
        final TypeReference<T> type) {
    Assert.hasText(key);
    Assert.hasText(min);
    Assert.hasText(max);
    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        final Set<Tuple> values = jedis.zrangeByScoreWithScores(key, min, max, offset, count);
        if (!CollectionUtils.isEmpty(values)) {
            final Map<T, Double> newValues = Maps.newHashMap();
            for (Tuple value : values) {
                newValues.put(parseObject(value.getElement(), type), value.getScore());
            }

            return newValues;
        }

        return Collections.emptyMap();
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}
 
Example #2
Source File: RedisClientImpl.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Override
public long decr(final String key) {
    Assert.hasText(key);
    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        final Long val = jedis.decr(key);
        if (val == null) {
            return 0;
        }

        return val.longValue();
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}
 
Example #3
Source File: RedisExample.java    From java-platform with Apache License 2.0 6 votes vote down vote up
public void testShardNormal() {// 13.619秒
	JedisShardInfo jedis = new JedisShardInfo("120.25.241.144", 6379);
	jedis.setPassword("b840fc02d52404542994");

	List<JedisShardInfo> shards = Arrays.asList(jedis);
	ShardedJedis sharding = new ShardedJedis(shards);

	long start = System.currentTimeMillis();
	for (int i = 0; i < 1000; i++) {
		sharding.set("n" + i, "n" + i);
		System.out.println(i);
	}
	long end = System.currentTimeMillis();
	System.out.println("共花费:" + (end - start) / 1000.0 + "秒");

	sharding.disconnect();
	try {
		Closeables.close(sharding, true);
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
Example #4
Source File: TestShardedRedis.java    From craft-atom with MIT License 6 votes vote down vote up
@Test
public void testHashSharded() {
	for (int i = 0; i < 10000; i++) {
		String key = "test-" + i;
		String value = "value-" + i;
		shardedRedis.set(key, key, value);
		ShardedJedis sj = shardedPool.getResource();
		try {
			String v = sj.get(key);
			Assert.assertEquals(value, v);
		} finally {
			sj.close();
		}
	}
	
	System.out.println(String.format("[CRAFT-ATOM-REDIS] (^_^)  <%s>  Case -> test hash sharded. ", CaseCounter.incr(1)));
}
 
Example #5
Source File: RedisCacheClient.java    From AsuraFramework with Apache License 2.0 6 votes vote down vote up
/**
 * 从string中删除对象
 */
@Override
public void del(String key) {
	ShardedJedis redis = null;
	try {
		redis = pool.getResource();
		key = getKeyAll(key);
		redis.del(key);
	} catch (RuntimeException e) { 
        if(redis != null ) {
	        pool.returnBrokenResource(redis);
	    }
        logger.error("redis hdel(String key, String... fields):", e);
	} finally{ 
         if(redis != null ) {
            pool.returnResource(redis);
         }
	}
	
}
 
Example #6
Source File: ShardedJedisPoolStream.java    From RedisDirectory with Apache License 2.0 6 votes vote down vote up
/**
 * Use transactions to add index file and then delete the old one
 *
 * @param fileLengthKey the key using for hash file length
 * @param fileDataKey   the key using for hash file data
 * @param oldField      the old hash field
 * @param newField      the new hash field
 * @param values        the data values of the old hash field
 * @param fileLength    the data length of the old hash field
 */
@Override
public void rename(String fileLengthKey, String fileDataKey, String oldField, String newField, List<byte[]> values, long
        fileLength) {
    ShardedJedis shardedJedis = getShardedJedis();
    ShardedJedisPipeline pipelined = shardedJedis.pipelined();
    //add new file length
    pipelined.hset(fileLengthKey.getBytes(), newField.getBytes(), Longs.toByteArray(fileLength));
    //add new file content
    Long blockSize = getBlockSize(fileLength);
    for (int i = 0; i < blockSize; i++) {
        pipelined.hset(fileDataKey.getBytes(), getBlockName(newField, i), compressFilter(values.get(i)));
    }
    pipelined.sync();
    shardedJedis.close();
    values.clear();
    deleteFile(fileLengthKey, fileDataKey, oldField, blockSize);
}
 
Example #7
Source File: ShardedJedisPoolStream.java    From RedisDirectory with Apache License 2.0 6 votes vote down vote up
@Override
public void saveFile(String fileLengthKey, String fileDataKey, String fileName, List<byte[]> values, long fileLength) {
    ShardedJedis shardedJedis = getShardedJedis();
    ShardedJedisPipeline pipelined = shardedJedis.pipelined();
    pipelined.hset(fileLengthKey.getBytes(), fileName.getBytes(), Longs.toByteArray(fileLength));
    Long blockSize = getBlockSize(fileLength);
    for (int i = 0; i < blockSize; i++) {
        pipelined.hset(fileDataKey.getBytes(), getBlockName(fileName, i), compressFilter(values.get(i)));
        if (i % Constants.SYNC_COUNT == 0) {
            pipelined.sync();
            pipelined = shardedJedis.pipelined();
        }
    }
    pipelined.sync();
    shardedJedis.close();
    values.clear();
}
 
Example #8
Source File: JedisShardProvider.java    From azeroth with Apache License 2.0 6 votes vote down vote up
public ShardedJedis get() throws JedisException {
    ShardedJedis jedis = context.get();
    if (jedis != null) { return jedis; }
    try {
        jedis = jedisPool.getResource();
    } catch (JedisException e) {
        if (jedis != null) {
            jedis.close();
        }
        throw e;
    }
    context.set(jedis);
    if (logger.isTraceEnabled()) {
        logger.trace(">>get a redis conn[{}]", jedis.toString());
    }
    return jedis;
}
 
Example #9
Source File: RedisClientImpl.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Override
public Map<String, String> hmget(final String key, final String... fields) {
    Assert.hasText(key);
    Assert.notEmpty(fields);

    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        final List<String> values = jedis.hmget(key, fields);
        final Map<String, String> valuesMap = Maps.newHashMap();
        for (int idx = 0; idx < values.size(); idx++) {
            final String value = values.get(idx);
            if (value != null) {
                valuesMap.put(fields[idx], value);
            }
        }

        return valuesMap;
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}
 
Example #10
Source File: RedisClient.java    From Redis_Learning with Apache License 2.0 6 votes vote down vote up
/**
 * ��ʼ����Ƭ��
 */
private void initialShardedPool() {
	// �ػ�������
	JedisPoolConfig config = new JedisPoolConfig();
	config.setMaxActive(20);
	config.setMaxIdle(5);
	config.setMaxWait(1000l);
	config.setTestOnBorrow(false);

	List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
	JedisShardInfo infoA = new JedisShardInfo(ADDR, PORT);
	infoA.setPassword("redis");
	shards.add(infoA);
	// ���Dz��� ��ʱ���������ķ������������ӷ���������
	// JedisShardInfo infoB = new JedisShardInfo(SUB_ADDR, PORT2);
	// infoB.setPassword("redis");
	// shards.add(infoB);
	// shards = Arrays.asList(infoA,infoB);
	shardedJedisPool = new ShardedJedisPool(config, shards,
			Hashing.MURMUR_HASH, ShardedJedis.DEFAULT_KEY_TAG_PATTERN);
}
 
Example #11
Source File: ShardedJedisPoolStream.java    From RedisDirectory with Apache License 2.0 6 votes vote down vote up
/**
 * Use transactions to add index file and then delete the old one
 *
 * @param fileLengthKey the key using for hash file length
 * @param fileDataKey   the key using for hash file data
 * @param oldField      the old hash field
 * @param newField      the new hash field
 * @param values        the data values of the old hash field
 * @param fileLength    the data length of the old hash field
 */
@Override
public void rename(String fileLengthKey, String fileDataKey, String oldField, String newField, List<byte[]> values, long
        fileLength) {
    ShardedJedis shardedJedis = getShardedJedis();
    ShardedJedisPipeline pipelined = shardedJedis.pipelined();
    //add new file length
    pipelined.hset(fileLengthKey.getBytes(), newField.getBytes(), Longs.toByteArray(fileLength));
    //add new file content
    Long blockSize = getBlockSize(fileLength);
    for (int i = 0; i < blockSize; i++) {
        pipelined.hset(fileDataKey.getBytes(), getBlockName(newField, i), compressFilter(values.get(i)));
    }
    pipelined.sync();
    shardedJedis.close();
    values.clear();
    deleteFile(fileLengthKey, fileDataKey, oldField, blockSize);
}
 
Example #12
Source File: RedisClientTemplate.java    From spring-boot-seed with MIT License 6 votes vote down vote up
/**
 * 设置单个值
 *
 * @param key   key
 * @param value value
 * @return result
 */
public String set(String key, String value) {
    String result = null;

    ShardedJedis shardedJedis = getRedisClient();
    if (shardedJedis == null) {
        return null;
    }

    try {
        result = shardedJedis.set(key, value);
    } catch (Exception e) {
        log.error("RedisClientTemplate set error !", e);
    } finally {
        shardedJedis.close();
    }
    return result;
}
 
Example #13
Source File: RedisClientTemplate.java    From spring-boot-seed with MIT License 6 votes vote down vote up
/**
 * 检查某个值是否存在
 *
 * @param key key
 * @return 是否存在某值
 */
public boolean exists(String key) {
    boolean result = false;
    ShardedJedis shardedJedis = getRedisClient();
    if (shardedJedis == null) {
        return false;
    }

    try {
        result = shardedJedis.exists(key);
    } catch (Exception e) {
        log.error("RedisClientTemplate exists error !", e);
    } finally {
        shardedJedis.close();
    }
    return result;
}
 
Example #14
Source File: RedisTemplate.java    From jigsaw-payment with Apache License 2.0 6 votes vote down vote up
/**
 * 全局扫描hset
 *
 * @param match field匹配模式
 */
public List<Map.Entry<String, String>> scanHSet(String domain, String match) {
    try (ShardedJedis shardedJedis = shardedJedisPool.getResource()) {
        int cursor = 0;

        ScanParams scanParams = new ScanParams();
        scanParams.match(match);
        Jedis jedis = shardedJedis.getShard(domain);
        ScanResult<Map.Entry<String, String>> scanResult;
        List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>();
        do {
            scanResult = jedis.hscan(domain, String.valueOf(cursor), scanParams);
            list.addAll(scanResult.getResult());
            cursor = Integer.parseInt(scanResult.getStringCursor());
        } while (cursor > 0);
        return list;
    }
}
 
Example #15
Source File: RedisShardedPoolUtil.java    From mmall20180107 with Apache License 2.0 6 votes vote down vote up
public static String set(String key,String value){

        ShardedJedis jedis = null;
        String result = null;

        try {
            jedis = RedisShardedPool.getJedis();
            result = jedis.set(key,value);
        } catch (Exception e) {
            log.error("set key:{} value:{} error",key,value,e);
            RedisShardedPool.returnBrokenResource(jedis);
            e.printStackTrace();
        }


        RedisShardedPool.returnResource(jedis);

        return result;

    }
 
Example #16
Source File: RedisClientImpl.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Override
public long decrBy(final String key, final long value) {
    Assert.hasText(key);
    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        final Long val = jedis.decrBy(key, value);
        if (val == null) {
            return 0;
        }

        return val.longValue();
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}
 
Example #17
Source File: RedisClientTemplate.java    From spring-boot-seed with MIT License 6 votes vote down vote up
/**
 * list右侧插入(队尾)
 *
 * @param key    key
 * @param string value
 * @return result
 */
public Long rpush(String key, String string) {
    Long result = null;
    ShardedJedis shardedJedis = getRedisClient();
    if (shardedJedis == null) {
        return null;
    }
    try {
        result = shardedJedis.rpush(key, string);

    } catch (Exception e) {
        log.error(e.getMessage(), e);
    } finally {
        shardedJedis.close();
    }
    return result;
}
 
Example #18
Source File: RedisClientImpl.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Override
public <T> Map<T, Double> zrevrangeByScoreWithScores(final String key, final double max, final double min, final int offset, final int count,
        final TypeReference<T> type) {
    Assert.hasText(key);
    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        final Set<Tuple> values = jedis.zrevrangeByScoreWithScores(key, max, min, offset, count);
        if (!CollectionUtils.isEmpty(values)) {
            final Map<T, Double> newValues = Maps.newHashMap();
            for (Tuple value : values) {
                newValues.put(parseObject(value.getElement(), type), value.getScore());
            }

            return newValues;
        }

        return Collections.emptyMap();
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}
 
Example #19
Source File: RedisClientImpl.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Override
public <T> Map<T, Double> zrevrangeWithScores(final String key, final long start, final long end, final TypeReference<T> type) {
    Assert.hasText(key);
    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        final Set<Tuple> values = jedis.zrevrangeWithScores(key, start, end);
        if (!CollectionUtils.isEmpty(values)) {
            final Map<T, Double> newValues = Maps.newHashMap();
            for (Tuple value : values) {
                newValues.put(parseObject(value.getElement(), type), value.getScore());
            }

            return newValues;
        }

        return Collections.emptyMap();
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}
 
Example #20
Source File: RedisClientTemplate.java    From spring-boot-seed with MIT License 6 votes vote down vote up
/**
 * 删除某个值
 *
 * @param key key
 * @return result
 */
public Long del(String key) {
    Long result = null;
    ShardedJedis shardedJedis = getRedisClient();
    if (shardedJedis == null) {
        return null;
    }

    try {
        result = shardedJedis.del(key);

    } catch (Exception e) {
        log.error("RedisClientTemplate del error !", e);
    } finally {
        shardedJedis.close();
    }
    return result;
}
 
Example #21
Source File: RedisServiceImpl.java    From AsuraFramework with Apache License 2.0 6 votes vote down vote up
@Override
public long increment(final String key, final int expire, final Callback<Long> callback) {
	checkParameters(key, callback);
	final ShardedJedis jedis = getJedis();

	long incr = jedis.incr(key);
	jedis.expire(key, expire);

	try {
		incr = callback.callback(incr);
	} catch (final Exception e) {
		throw new IncrementRedisException("method: increment, key: " + key + ", incr: " + incr, e);
	}

	returnResource(jedis);
	return incr;
}
 
Example #22
Source File: RedisClientImpl.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Override
public List<String> lrangeltrim(final String key, final int count) {
    Assert.hasText(key);
    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        final ShardedJedisPipeline pipeline = jedis.pipelined();
        final Response<List<String>> values = pipeline.lrange(key, 0, count);
        pipeline.ltrim(key, count + 1, -1);
        pipeline.sync();
        return values.get();
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}
 
Example #23
Source File: RedisClientTemplate.java    From spring-boot-seed with MIT License 6 votes vote down vote up
/**
 * 获取多个值
 *
 * @param keys key集合
 * @return result
 */
public Map<String, String> get(Collection<String> keys) {
    Map<String, String> result = new HashMap<>(keys.size());
    ShardedJedis shardedJedis = getRedisClient();
    if (shardedJedis == null) {
        return null;
    }

    try {
        for (String key : keys) {
            String value = shardedJedis.get(key);
            result.put(key, value);
        }
    } catch (Exception e) {
        log.error("RedisClientTemplate get error !", e);
    } finally {
        shardedJedis.close();
    }
    return result;
}
 
Example #24
Source File: RedisClientImpl.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
@Override
public Set<String> zrangeByLex(final String key, final String min, final String max, final int offset, final int count) {
    Assert.hasText(key);
    Assert.hasText(min);
    Assert.hasText(max);
    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        return jedis.zrangeByLex(key, min, max, offset, count);
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}
 
Example #25
Source File: RedisClientImpl.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
@Override
public long del(final String... keys) {
    if (keys.length == 0) {
        return 0;
    }

    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        final ShardedJedisPipeline pipeline = jedis.pipelined();
        final List<Response<Long>> responses = new ArrayList<>();
        for (String key : keys) {
            responses.add(pipeline.del(key));
        }

        pipeline.sync();

        final AtomicLong dels = new AtomicLong(0);
        if (!CollectionUtils.isEmpty(responses)) {
            responses.forEach(res -> dels.addAndGet(res.get()));
        }

        return dels.get();
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}
 
Example #26
Source File: RedisShardedPoolUtil.java    From mmall-kay-Java with Apache License 2.0 5 votes vote down vote up
public static Long del(String key){
    ShardedJedis jedis = null;
    Long result=null;
    try {
        jedis = RedisShardedPool.getJedis();
        result = jedis.del(key);
    } catch (Exception e) {
        log.error("del key:{} error",key,e);
        RedisShardedPool.returnBrokenResource(jedis);
        return result;
    }
    RedisShardedPool.returnResource(jedis);
    return result;
}
 
Example #27
Source File: RedisClientImpl.java    From nano-framework with Apache License 2.0 5 votes vote down vote up
@Override
public Set<String> spop(final String key, final int count) {
    Assert.hasText(key);
    ShardedJedis jedis = null;
    try {
        jedis = POOL.getJedis(config.getRedisType());
        return jedis.spop(key, count);
    } catch (final Throwable e) {
        throw new RedisClientException(e.getMessage(), e);
    } finally {
        POOL.close(jedis);
    }
}
 
Example #28
Source File: JedisCacheService.java    From howsun-javaee-framework with Apache License 2.0 5 votes vote down vote up
private String setObject(String key, Object value, int seconds) {
	ShardedJedis jedis = null;

	if(value instanceof String){
		return setStringValue(key, value.toString(), seconds);
	}

	try {
		jedis = cacheFactory.getResource();

		byte[] keyBytes = key.getBytes("UTF-8");
		byte result[] = serializer.serialize(value);
		if(seconds > -1){
			return jedis.setex(keyBytes, seconds, result);
		}else{
			return jedis.set(keyBytes, result);
		}
	}
	catch (Exception e) {
		log.error("获取缓存失败:", e);
	}
	finally{
		if(jedis != null){
			try {cacheFactory.returnResource(jedis);}
			catch (Exception e2) {
				log.error("不能释放缓存操纵对象:",e2);
			}
		}
	}
	return null;
}
 
Example #29
Source File: RedisShardedPoolUtil.java    From mmall-kay-Java with Apache License 2.0 5 votes vote down vote up
public static String get(String key){
    ShardedJedis jedis = null;
    String result=null;
    try {
        jedis = RedisShardedPool.getJedis();
        result = jedis.get(key);
    } catch (Exception e) {
        log.error("get key:{} error",key,e);
        RedisShardedPool.returnBrokenResource(jedis);
        return result;
    }
    RedisShardedPool.returnResource(jedis);
    return result;
}
 
Example #30
Source File: RedisTemplate.java    From jigsaw-payment with Apache License 2.0 5 votes vote down vote up
/**
 * 添加到List
 */
public boolean addList(String key, String... value) {
    try (ShardedJedis shardedJedis = shardedJedisPool.getResource()) {
        shardedJedis.lpush(key, value);
        return true;
    }
}