Java Code Examples for redis.clients.jedis.Jedis#zadd()

The following examples show how to use redis.clients.jedis.Jedis#zadd() . 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: JedisUtil.java    From scaffold-cloud with MIT License 6 votes vote down vote up
/**
 * 设置sorted set缓存
 *
 * @param key          键
 * @param value        值
 * @param cacheSeconds 超时时间,0为不超时 @Param score 值
 */
public static Long zadd(String key, String value, double score, int cacheSeconds) {
    Long result = null;
    Jedis jedis = null;
    try {
        jedis = getResource();
        result = jedis.zadd(key, score, value);
        if (cacheSeconds != 0) {
            jedis.expire(key, cacheSeconds);
        }
    } catch (Exception e) {
        logger.warn("zadd {} = {}", key, value, e);
    } finally {
        close(jedis);
    }
    return result;
}
 
Example 2
Source File: JedisUtil.java    From scaffold-cloud with MIT License 6 votes vote down vote up
/**
 * 设置sorted set缓存
 *
 * @param key          键
 * @param members      成员 值 分数
 * @param cacheSeconds 超时时间,0为不超时 @Param score 值
 */
public static Long zadd(String key, Map<String, Double> members, int cacheSeconds) {
    Long result = null;
    Jedis jedis = null;
    try {
        jedis = getResource();
        result = jedis.zadd(key, members);
        if (cacheSeconds != 0) {
            jedis.expire(key, cacheSeconds);
        }
    } catch (Exception e) {
        logger.warn("zadd {} = {}", key, members, e);
    } finally {
        close(jedis);
    }
    return result;
}
 
Example 3
Source File: RedisSockjsClient.java    From slime with MIT License 6 votes vote down vote up
public static void pingDaemonServer() {
	Jedis jedis = null;
	try {
		jedis = JedisConnectionPool.getJedisConnection();
		jedis.select(10);
		JsonObject status = new JsonObject();
		status.put("status", true);
		status.put("time", System.currentTimeMillis());
		jedis.hset(RedisKeyStore.CLIENT.getMaster(), serverKey, status.toString());
		jedis.zadd(RedisKeyStore.CLIENT + ":STATUS", 0, serverKey);
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (jedis != null)
			JedisConnectionPool.close(jedis);
	}
}
 
Example 4
Source File: RedisHttpClient.java    From slime with MIT License 6 votes vote down vote up
public static void pingDaemonServer() {
	Jedis jedis = null;
	try {
		jedis = JedisConnectionPool.getJedisConnection();
		jedis.select(10);
		JsonObject status = new JsonObject();
		status.put("status", true);
		status.put("time", System.currentTimeMillis());
		jedis.hset(RedisKeyStore.HTTP.getMaster(), serverKey, status.toString());
		jedis.zadd(RedisKeyStore.HTTP + ":STATUS", 0, serverKey);
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		if (jedis != null)
			JedisConnectionPool.close(jedis);
	}
}
 
Example 5
Source File: JedisUtil.java    From newblog with Apache License 2.0 6 votes vote down vote up
public void zadd(String key, double score, String member) {
    Jedis jedis = null;
    try {
        jedis = getJedis();
        if (jedis == null) {
            logger.error("get jedis fail");
        }
        jedis.zadd(key, score, member);
    } catch (JedisConnectionException e) {
        if (jedis != null) {
            jedis.close();
        }
    } finally {
        returnJedisResource(jedis);
    }
}
 
Example 6
Source File: RedisContainer.java    From bahir-flink with Apache License 2.0 6 votes vote down vote up
@Override
public void zadd(final String key, final String score, final String element) {
    Jedis jedis = null;
    try {
        jedis = getInstance();
        jedis.zadd(key, Double.valueOf(score), element);
    } catch (Exception e) {
        if (LOG.isErrorEnabled()) {
            LOG.error("Cannot send Redis message with command ZADD to set {} error message {}",
                key, e.getMessage());
        }
        throw e;
    } finally {
        releaseInstance(jedis);
    }
}
 
Example 7
Source File: RedisPriorityScheduler.java    From webmagic with Apache License 2.0 6 votes vote down vote up
@Override
protected void pushWhenNoDuplicate(Request request, Task task)
{
    Jedis jedis = pool.getResource();
    try
    {
        if(request.getPriority() > 0)
            jedis.zadd(getZsetPlusPriorityKey(task), request.getPriority(), request.getUrl());
        else if(request.getPriority() < 0)
            jedis.zadd(getZsetMinusPriorityKey(task), request.getPriority(), request.getUrl());
        else
            jedis.lpush(getQueueNoPriorityKey(task), request.getUrl());

        setExtrasInItem(jedis, request, task);
    }
    finally
    {
        pool.returnResource(jedis);
    }
}
 
Example 8
Source File: JedisBlockingQueueStore.java    From vscrawler with Apache License 2.0 5 votes vote down vote up
@Override
public void zadd(String queueID, ResourceItem resourceItem) {
    if (!lockQueue(queueID)) {
        throw new RuntimeException("failed to acquire resource queue for queueID: " + queueID);
    }
    Jedis jedis = jedisPool.getResource();
    try {
        jedis.zadd(makePoolQueueKey(queueID), resourceItem.getValidTimeStamp(), resourceItem.getKey());
        jedis.hset(makeDataKey(queueID), resourceItem.getKey(), JSONObject.toJSONString(resourceItem));
    } finally {
        IOUtils.closeQuietly(jedis);
        unLockQueue(queueID);
    }
}
 
Example 9
Source File: RedisActionHistory.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
@Override
public void addAction(String clientName, long userId, long itemId)
		throws APIException {
	JedisPool pool = poolManager.get(clientName);
	if (pool != null)
	{
		Jedis jedis = null;
		try 
		{
			long now = System.currentTimeMillis()/1000;
			String key = MemCacheKeys.getActionHistory(clientName, userId);
			jedis = pool.getResource();
			jedis.zadd(key, now, ""+itemId);
			//zremrangebyscore needed
		}
		finally
		{
			if (jedis != null)
			{
				jedis.close();
			}
		}
	}
	else
	{
		logger.error("No redis pool found for "+clientName);
	}
	
}
 
Example 10
Source File: RedisActionHistory.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
@Override
public void addFullAction(String clientName, Action a) throws APIException {

	JedisPool pool = poolManager.get(clientName);
	if (pool != null)
	{
		Jedis jedis = null;
		try 
		{
			ActionLogEntry al = new ActionLogEntry(clientName,a);
			String val = mapper.writeValueAsString(al);
			String key = MemCacheKeys.getActionFullHistory(clientName, a.getUserId());
			jedis = pool.getResource();
			jedis.zadd(key, a.getDate().getTime()/1000, val);
			//zremrangebyscore needed
		}
		catch (JsonProcessingException e) 
		{
			logger.error("Failed to convert action to json ",e);
		}
		finally
		{
			if (jedis != null)
			{
				jedis.close();
			}
		}
	}
	else
	{
		logger.error("No redis pool found for "+clientName);
	}
}
 
Example 11
Source File: StringUtil.java    From BigData with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 遍历Map,将Map中的商品依次推进Redis sorted set
 * 
 * @param jedis
 * @param currentTime
 * @param hashMap
 */
public static void map2Redis(long currentTime, Map<String, String> hashMap) {
	Jedis jedis = new Jedis("192.168.1.104", 6379);
	// 遍历Map
	Set<String> keySet = hashMap.keySet();
	Iterator<String> iterator = keySet.iterator();

	while (iterator.hasNext()) {
		String key = iterator.next();// 获得商品码
		String value = hashMap.get(key);// 获得字符串类型的总金额
		jedis.zadd("mx@" + currentTime, Double.parseDouble(value), key);
	}

	jedis.close();
}
 
Example 12
Source File: RedisServiceImpl.java    From ace-cache with Apache License 2.0 5 votes vote down vote up
@Override
public Long zadd(String key, double score, String member) {
    Jedis jedis = null;
    Long res = null;
    try {
        jedis = pool.getResource();
        res = jedis.zadd(key, score, member);
    } catch (Exception e) {

        LOGGER.error(e.getMessage());
    } finally {
        returnResource(pool, jedis);
    }
    return res;
}
 
Example 13
Source File: JedisClientPool.java    From paas with Apache License 2.0 5 votes vote down vote up
@Override
public Long zadd(String key, Double score, String member) {
    Jedis jedis = jedisPool.getResource();
    Long result = jedis.zadd(key, score, member);
    jedis.close();
    return result;
}
 
Example 14
Source File: ZSetOperUtil.java    From springboot-learn with MIT License 5 votes vote down vote up
public static void oper(Jedis jedis) {

        jedis.zadd("zset", 3, "3");
        jedis.zadd("zset", 4, "5");
        jedis.zadd("zset", 5, "5");
        jedis.zadd("zset", 6, "6");
        Set<String> zset = jedis.zrange("zset", 0, -1);
        System.out.println("zset:" + zset);
        System.out.println("zset type:" + jedis.type("zset"));

    }
 
Example 15
Source File: Main.java    From JavaBase with MIT License 4 votes vote down vote up
private void transferZSet(String key, Jedis originJedis, Jedis targetJedis) {
  Set<Tuple> tuples = originJedis.zrangeWithScores(key, 0, -1);
  Map<String, Double> collect = tuples.stream()
      .collect(Collectors.toMap(Tuple::getElement, Tuple::getScore));
  targetJedis.zadd(key, collect);
}
 
Example 16
Source File: JedisUtil.java    From BigData with GNU General Public License v3.0 4 votes vote down vote up
public long zadd(String key, Map<String, Double> scoreMembers) {
	Jedis jedis = getJedis();
	long s = jedis.zadd(key, scoreMembers);
	returnJedis(jedis);
	return s;
}
 
Example 17
Source File: DefaultRedis.java    From craft-atom with MIT License 4 votes vote down vote up
private Long zadd0(Jedis j, String key, Map<String, Double> scoremembers) {
	return j.zadd(key, scoremembers);
}
 
Example 18
Source File: JedisUtil.java    From BigData with GNU General Public License v3.0 3 votes vote down vote up
/**
 * 向集合中增加一条记录,如果这个值已存在,这个值对应的权重将被置为新的权重
 * 
 * @param String
 *            key
 * @param double score 权重
 * @param String
 *            member 要加入的值,
 * @return 状态码 1成功,0已存在member的值
 * */
public long zadd(String key, double score, String member) {
	Jedis jedis = getJedis();
	long s = jedis.zadd(key, score, member);
	returnJedis(jedis);
	return s;
}
 
Example 19
Source File: RedisClient.java    From springboot-learn with MIT License 2 votes vote down vote up
/**
 * 有序集合添加
 *
 * @param key
 * @param score
 * @param member
 */
public void zAdd(String key, double score, String member) {
    Jedis jedis = jedisPool.getResource();
    jedis.zadd(key, score, member);
}
 
Example 20
Source File: RedisClient.java    From springboot-learn with MIT License 2 votes vote down vote up
/**
 * 有序集合添加
 *
 * @param key
 * @param score
 * @param member
 */
public void zAdd(String key, double score, String member) {
    Jedis jedis = jedisPool.getResource();
    jedis.zadd(key, score, member);
}