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

The following examples show how to use redis.clients.jedis.Jedis#lpop() . 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: JedisSegmentScoredQueueStore.java    From vscrawler with Apache License 2.0 6 votes vote down vote up
@Override
public boolean addFirst(String queueID, ResourceItem e) {
    if (!lockQueue(queueID)) {
        return false;
    }
    @Cleanup Jedis jedis = jedisPool.getResource();
    try {
        remove(queueID, e.getKey());
        jedis.hset(makeDataKey(queueID), e.getKey(), JSONObject.toJSONString(e));
        String sliceID = jedis.lpop(makeSliceQueueKey(queueID));
        if (isNil(sliceID)) {
            sliceID = "1";
        }
        jedis.lpush(makeSliceQueueKey(queueID), sliceID);
        jedis.lpush(makePoolQueueKey(queueID, sliceID), e.getKey());
    } finally {
        unLockQueue(queueID);
    }
    return true;
}
 
Example 2
Source File: RedisPriorityScheduler.java    From webmagic with Apache License 2.0 6 votes vote down vote up
private String getRequest(Jedis jedis, Task task)
{
    String url;
    Set<String> urls = jedis.zrevrange(getZsetPlusPriorityKey(task), 0, 0);
    if(urls.isEmpty())
    {
        url = jedis.lpop(getQueueNoPriorityKey(task));
        if(StringUtils.isBlank(url))
        {
            urls = jedis.zrevrange(getZsetMinusPriorityKey(task), 0, 0);
            if(!urls.isEmpty())
            {
                url = urls.toArray(new String[0])[0];
                jedis.zrem(getZsetMinusPriorityKey(task), url);
            }
        }
    }
    else
    {
        url = urls.toArray(new String[0])[0];
        jedis.zrem(getZsetPlusPriorityKey(task), url);
    }
    return url;
}
 
Example 3
Source File: RedisScheduler.java    From webmagic with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized Request poll(Task task) {
    Jedis jedis = pool.getResource();
    try {
        String url = jedis.lpop(getQueueKey(task));
        if (url == null) {
            return null;
        }
        String key = ITEM_PREFIX + task.getUUID();
        String field = DigestUtils.shaHex(url);
        byte[] bytes = jedis.hget(key.getBytes(), field.getBytes());
        if (bytes != null) {
            Request o = JSON.parseObject(new String(bytes), Request.class);
            return o;
        }
        Request request = new Request(url);
        return request;
    } finally {
        pool.returnResource(jedis);
    }
}
 
Example 4
Source File: RedisDataSet.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
private String getDataFromConnection(Jedis conn, String key) {
    String line = null;

    try {
        if (redisDataType == RedisDataType.REDIS_DATA_TYPE_LIST) {
            log.debug("Executing lpop against redis list");
            // Get data from list's head
            line = conn.lpop(key);
        } else if (redisDataType.equals(RedisDataType.REDIS_DATA_TYPE_SET)) {
            log.debug("Executing spop against redis set");
            line = conn.spop(key);
        } else {
            log.warn("Unexpected redis datatype: {0}".format(key));
        }
    } catch (JedisDataException jde) {
        log.error("Exception when retrieving data from Redis: " + jde);
    }

    return line;
}
 
Example 5
Source File: RedisClient.java    From apollo with GNU General Public License v2.0 6 votes vote down vote up
public Object lpop(String key, Class<?> cls) throws Exception {
    byte[] data = null;
    Jedis jedis = null;
    try {
        jedis = this.jedisPool.getResource();
        // long begin = System.currentTimeMillis();
        data = jedis.lpop(SafeEncoder.encode(key));
        // long end = System.currentTimeMillis();
        // LOG.info("getValueFromCache spends: " + (end - begin) + " millionseconds.");
    } catch (Exception e) {
        // do jedis.quit() and jedis.disconnect()
        this.jedisPool.returnBrokenResource(jedis);
        throw e;
    } finally {
        if (jedis != null) {
            this.jedisPool.returnResource(jedis);
        }
    }

    return this.jsonDeserialize(data, cls);
}
 
Example 6
Source File: JedisScoredQueueStore.java    From vscrawler with Apache License 2.0 6 votes vote down vote up
@Override
public ResourceItem pop(String queueID) {
    if (!lockQueue(queueID)) {
        return null;
    }
    Jedis jedis = jedisPool.getResource();
    try {
        String firstResourceKey = jedis.lpop(makePoolQueueKey(queueID));
        if (isNil(firstResourceKey)) {
            return null;
        }
        String dataJson = jedis.hget(makeDataKey(queueID), firstResourceKey);
        if (isNil(dataJson)) {
            throw new IllegalStateException("this is no meta data for key queue :" + queueID + " ,for resourceKey :" + firstResourceKey);
        }
        jedis.hdel(makeDataKey(queueID), firstResourceKey);
        return JSONObject.toJavaObject(JSON.parseObject(dataJson), ResourceItem.class);
    } finally {
        IOUtils.closeQuietly(jedis);
        unLockQueue(queueID);
    }
}
 
Example 7
Source File: DoTestJedisHookProxy.java    From uavstack with Apache License 2.0 6 votes vote down vote up
private static void foo() {

        System.out.println("TEST Jedis ======================================================");
        Jedis jedis = new Jedis(ip, port);
        jedis.set("foo", "bar");

        jedis.get("foo");

        jedis.lpush("lll", "a");
        jedis.lpush("lll", "b");
        jedis.lpush("lll", "c");
        jedis.lpop("lll");
        jedis.lpop("lll");
        jedis.lpop("lll");

        jedis.hset("mmm", "abc", "123");
        jedis.hset("mmm", "def", "456");
        jedis.hgetAll("mmm");

        jedis.close();
    }
 
Example 8
Source File: DoTestJedisHookProxy.java    From uavstack with Apache License 2.0 5 votes vote down vote up
private static void foo2() {

        System.out.println("TEST JedisPool ======================================================");
        JedisPoolConfig cfg = new JedisPoolConfig();
        cfg.setMaxTotal(5);
        cfg.setMaxIdle(1);
        cfg.setMaxWaitMillis(10000L);

        JedisPool jp = new JedisPool(cfg, ip, port);
        Jedis jedis = jp.getResource();

        jedis.set("foo", "bar");
        // jedis.close();
        jedis = jp.getResource();

        jedis.get("foo");
        // jedis.close();
        jedis = jp.getResource();

        jedis.lpush("lll", "a");
        jedis.lpush("lll", "b");
        jedis.lpush("lll", "c");
        jedis.lpop("lll");
        jedis.lpop("lll");
        jedis.lpop("lll");
        // jedis.close();
        jedis = jp.getResource();

        jedis.hset("mmm", "abc", "123");
        jedis.hset("mmm", "def", "456");
        jedis.hgetAll("mmm");

        jp.close();
    }
 
Example 9
Source File: JedisUtils.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * 通过key从list的头部删除一个value,并返回该value
 * </p>
 *
 * @param key
 * @return
 */
synchronized public String lpop(String key) {
    Jedis jedis = null;
    String res = null;
    try {
        jedis = jedisPool.getResource();
        res = jedis.lpop(key);
    } catch (Exception e) {

        log.error(e.getMessage());
    } finally {
        returnResource(jedisPool, jedis);
    }
    return res;
}
 
Example 10
Source File: RedisServiceImpl.java    From ace-cache with Apache License 2.0 5 votes vote down vote up
@Override
synchronized public String lpop(String key) {
    Jedis jedis = null;
    String res = null;
    try {
        jedis = pool.getResource();
        res = jedis.lpop(key);
    } catch (Exception e) {

        LOGGER.error(e.getMessage());
    } finally {
        returnResource(pool, jedis);
    }
    return res;
}
 
Example 11
Source File: JedisUtil.java    From BigData with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 将List中的第一条记录移出List
 * 
 * @param byte[] key
 * @return 移出的记录
 * */
public byte[] lpop(byte[] key) {
	Jedis jedis = getJedis();
	byte[] value = jedis.lpop(key);
	returnJedis(jedis);
	return value;
}
 
Example 12
Source File: RedisScheduler.java    From ScriptSpider with Apache License 2.0 5 votes vote down vote up
/**
 * poll种子
 *
 * @return UrlSeed
 */
@Override
public UrlSeed poll() {
    Gson gson = new Gson();
    Jedis jedis = null;
    String urlSeedJson = null;
    try {
        jedis = jedisPool.getResource();
        //依次取三个队列,优先级高的被先取!
        urlSeedJson = jedis.lpop(Prefix_Queue_high);
        if (urlSeedJson != null) {
            return gson.fromJson(urlSeedJson, UrlSeed.class);
        }
        urlSeedJson = jedis.lpop(Prefix_Queue_default);
        if (urlSeedJson != null) {
            return gson.fromJson(urlSeedJson, UrlSeed.class);
        }
        urlSeedJson = jedis.lpop(Prefix_Queue_low);
        if (urlSeedJson != null) {
            return gson.fromJson(urlSeedJson, UrlSeed.class);
        }
    } catch (Exception e) {
        logger.warn("连接获取失败!", e);
    } finally {
        if (null != jedis) {
            jedis.close();
        }
    }
    return null;
}
 
Example 13
Source File: RandomRedisRepositoryImpl.java    From ispider with Apache License 2.0 5 votes vote down vote up
/**
 * 从队列中获取url,目前的策略是:
 *      1.先从高优先级url队列中获取
 *      2.再从低优先级url队列中获取
 *  对应我们的实际场景,应该是先解析完列表url再解析商品url
 *  但是需要注意的是,在分布式多线程的环境下,肯定是不能完全保证的,因为在某个时刻高优先级url队列中
 *  的url消耗完了,但实际上程序还在解析下一个高优先级url,此时,其它线程去获取高优先级队列url肯定获取不到
 *  这时就会去获取低优先级队列中的url,在实际考虑分析时,这点尤其需要注意
 * @return
 */
@Override
public String poll() {
    // 从set中随机获取一个顶级域名
    Jedis jedis = JedisUtil.getJedis();
    String randomDomain = jedis.srandmember(SpiderConstants.SPIDER_WEBSITE_DOMAINS_KEY);    // jd.com
    String key = randomDomain + SpiderConstants.SPIDER_DOMAIN_HIGHER_SUFFIX;                // jd.com.higher
    String url = jedis.lpop(key);
    if(url == null) {   // 如果为null,则从低优先级中获取
        key = randomDomain + SpiderConstants.SPIDER_DOMAIN_LOWER_SUFFIX;    // jd.com.lower
        url = jedis.lpop(key);
    }
    JedisUtil.returnJedis(jedis);
    return url;
}
 
Example 14
Source File: JedisUtil.java    From Project with Apache License 2.0 5 votes vote down vote up
/**
 * 将List中的第一条记录移出List
 * 
 * @param  key
 * @return 移出的记录
 */
public byte[] lpop(byte[] key) {
	Jedis jedis = getJedis();
	byte[] value = jedis.lpop(key);
	jedis.close();
	return value;
}
 
Example 15
Source File: JedisSegmentScoredQueueStore.java    From vscrawler with Apache License 2.0 4 votes vote down vote up
@Override
public ResourceItem pop(String queueID) {
    if (!lockQueue(queueID)) {
        return null;
    }
    @Cleanup Jedis jedis = jedisPool.getResource();
    try {
        String sliceQueueKey = makeSliceQueueKey(queueID);
        String firstResourceKey;
        String firstSliceID = null;
        while (true) {
            String sliceID = jedis.lpop(sliceQueueKey);
            if (isNil(sliceID)) {
                // empty queue
                return null;
            }
            if (firstSliceID == null) {
                firstSliceID = sliceID;
            } else if (firstSliceID.equals(sliceID)) {
                // 这个条件下,数据紊乱了?
                jedis.lpush(sliceQueueKey, firstSliceID);
                return null;
            }
            firstResourceKey = jedis.lpop(makePoolQueueKey(queueID, sliceID));
            if (!isNil(firstResourceKey)) {
                jedis.lpush(sliceQueueKey, sliceID);
                break;
            }
            jedis.rpush(sliceQueueKey, sliceID);
        }

        String dataJson = jedis.hget(makeDataKey(queueID), firstResourceKey);
        if (isNil(dataJson)) {
            throw new IllegalStateException(
                    "this is no meta data for key queue :" + queueID + " ,for resourceKey :" + firstResourceKey);
        }
        jedis.hdel(makeDataKey(queueID), firstResourceKey);
        return JSONObject.toJavaObject(JSON.parseObject(dataJson), ResourceItem.class);
    } finally {
        unLockQueue(queueID);
    }

}
 
Example 16
Source File: DefaultRedis.java    From craft-atom with MIT License 4 votes vote down vote up
private String lpop0(Jedis j, String key) {
	return j.lpop(key);
}
 
Example 17
Source File: ListOperUtil.java    From springboot-learn with MIT License 3 votes vote down vote up
public static void oper(Jedis jedis) {

        Long lpush = jedis.lpush("list", "a", "b", "c");
        String list = jedis.lpop("list");
        System.out.println("lpush:" + lpush + "---" + list + "---type:" + jedis.type("list"));

    }