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

The following examples show how to use redis.clients.jedis.Jedis#lrange() . 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: JedisSingleDemo.java    From Redis-4.x-Cookbook with MIT License 8 votes vote down vote up
public static void main(String[] args) {
    //Connecting to localhost Redis server
    Jedis jedis = new Jedis("localhost");

    //String operations
    String restaurant = "Extreme Pizza";
    jedis.set(restaurant, "300 Broadway, New York, NY");
    jedis.append(restaurant, " 10011");
    String address = jedis.get("Extreme Pizza");
    System.out.printf("Address for %s is %s\n", restaurant, address);

    //List operations
    String listKey = "favorite_restaurants";
    jedis.lpush(listKey, "PF Chang's", "Olive Garden");
    jedis.rpush(listKey, "Outback Steakhouse", "Red Lobster");
    List<String> favoriteRestaurants = jedis.lrange(listKey, 0, -1);
    System.out.printf("Favorite Restaurants: %s\n", favoriteRestaurants);

    System.exit(0);
}
 
Example 2
Source File: JedisUtils.java    From fw-spring-cloud with Apache License 2.0 6 votes vote down vote up
/**
 * <p>
 * 通过key获取list指定下标位置的value
 * </p>
 * <p>
 * 如果start 为 0 end 为 -1 则返回全部的list中的value
 * </p>
 *
 * @param key
 * @param start
 * @param end
 * @return
 */
public List<String> lrange(String key, long start, long end, int indexdb) {
    Jedis jedis = null;
    List<String> res = null;
    try {
        jedis = jedisPool.getResource();
        jedis.select(indexdb);
        res = jedis.lrange(key, start, end);
    } catch (Exception e) {

        log.error(e.getMessage());
    } finally {
        returnResource(jedisPool, jedis);
    }
    return res;
}
 
Example 3
Source File: JedisUtils.java    From easyweb with Apache License 2.0 6 votes vote down vote up
/**
 * 获取List缓存
 * @param key 键
 * @return 值
 */
public static List<String> getList(String key) {
	List<String> value = null;
	Jedis jedis = null;
	try {
		jedis = getResource();
		if (jedis.exists(key)) {
			value = jedis.lrange(key, 0, -1);
			logger.debug("getList {} = {}", key, value);
		}
	} catch (Exception e) {
		logger.warn("getList {} = {}", key, value, e);
	} finally {
		returnResource(jedis);
	}
	return value;
}
 
Example 4
Source File: JedisUtil.java    From newblog with Apache License 2.0 6 votes vote down vote up
public List<String> lrange(String key) {
    Jedis jedis = null;
    try {
        jedis = getJedis();
        if (jedis == null) {
            logger.error("get jedis fail");
        }
        List<String> list = jedis.lrange(key, 0, 50);
        return list;
    } catch (JedisConnectionException e) {
        if (jedis != null) {
            jedis.close();
        }
    } finally {
        returnJedisResource(jedis);
    }
    return null;
}
 
Example 5
Source File: RedisUtil.java    From shop with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 获取列表中所有数据,ruguo
 *
 * @param key
 * @return
 */
public List<T> lall(String key, Class clazz) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        // 0表示第一个元素,-1表示最后一个元素
        List<String> list = jedis.lrange(key, 0, -1);
        List<T> res = new ArrayList<T>();
        if (list == null || list.size() == 0) {
            return res;
        }
        for (String str : list) {
            res.add((T) JSON.parseObject(str, clazz));
        }
        return res;
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }
}
 
Example 6
Source File: JedisUtils.java    From easyweb with Apache License 2.0 6 votes vote down vote up
/**
 * 获取List缓存
 * @param key 键
 * @return 值
 */
public static List<Object> getObjectList(String key) {
	List<Object> value = null;
	Jedis jedis = null;
	try {
		jedis = getResource();
		if (jedis.exists(getBytesKey(key))) {
			List<byte[]> list = jedis.lrange(getBytesKey(key), 0, -1);
			value = Lists.newArrayList();
			for (byte[] bs : list){
				value.add(toObject(bs));
			}
			logger.debug("getObjectList {} = {}", key, value);
		}
	} catch (Exception e) {
		logger.warn("getObjectList {} = {}", key, value, e);
	} finally {
		returnResource(jedis);
	}
	return value;
}
 
Example 7
Source File: UpmsSessionDao.java    From zheng with MIT License 6 votes vote down vote up
/**
 * 获取会话列表
 * @param offset
 * @param limit
 * @return
 */
public Map getActiveSessions(int offset, int limit) {
    Map sessions = new HashMap();
    Jedis jedis = RedisUtil.getJedis();
    // 获取在线会话总数
    long total = jedis.llen(ZHENG_UPMS_SERVER_SESSION_IDS);
    // 获取当前页会话详情
    List<String> ids = jedis.lrange(ZHENG_UPMS_SERVER_SESSION_IDS, offset, (offset + limit - 1));
    List<Session> rows = new ArrayList<>();
    for (String id : ids) {
        String session = RedisUtil.get(ZHENG_UPMS_SHIRO_SESSION_ID + "_" + id);
        // 过滤redis过期session
        if (null == session) {
            RedisUtil.lrem(ZHENG_UPMS_SERVER_SESSION_IDS, 1, id);
            total = total - 1;
            continue;
        }
         rows.add(SerializableUtil.deserialize(session));
    }
    jedis.close();
    sessions.put("total", total);
    sessions.put("rows", rows);
    return sessions;
}
 
Example 8
Source File: TestRedis.java    From ProjectStudy with MIT License 6 votes vote down vote up
@Test
public void TestList() {
    //连接本地的 Redis 服务
    Jedis jedis = new Jedis("localhost");
    System.out.println("连接成功");
    //存储数据到列表中
    jedis.lpush("site-list", "Runoob");
    jedis.lpush("site-list", "Google");
    jedis.lpush("site-list", "Taobao");
    // 获取存储的数据并输出
    List<String> list = jedis.lrange("site-list", 0, 2);
    for (int i = 0; i < list.size(); i++) {
        System.out.println("列表项为: " + list.get(i));
    }
    List<String> list2 = jedis.lrange("list", 0, 2);
    for (int i = 0; i < list2.size(); i++) {
        System.out.println("列表项为: " + list2.get(i));
    }
}
 
Example 9
Source File: RedisTaskResultStore.java    From helix with Apache License 2.0 5 votes vote down vote up
@Override
public List<String> lrange(String key, long start, long end) throws Exception {
  Jedis jedis = _jedisPool.getResource();
  try {
    return jedis.lrange(key, start, end);
  } finally {
    _jedisPool.returnResource(jedis);
  }
}
 
Example 10
Source File: JedisUtil.java    From scaffold-cloud with MIT License 5 votes vote down vote up
/**
 * 返回列表 key 中指定区间内的元素,区间以偏移量 start 和 stop 指定。
 *
 * @param key
 * @return
 */
public static List<String> getladd(String key) {
    Jedis jedis = null;
    List<String> list = null;
    try {
        jedis = getResource();
        list = jedis.lrange(key, 0, -1);
    } catch (Exception e) {
        logger.warn("lrange{}", key, e);
    } finally {
        close(jedis);
    }
    return list;
}
 
Example 11
Source File: RedisClient.java    From Mykit with Apache License 2.0 5 votes vote down vote up
public List<String> lrange(String key, int start, int length) {
	Jedis client = jedisPool.getResource();
	try {
		return client.lrange(key, start, length);
	} finally {
		// 向连接池“归还”资源
		jedisPool.returnResourceObject(client);
	}
}
 
Example 12
Source File: JedisSegmentScoredQueueStore.java    From vscrawler with Apache License 2.0 5 votes vote down vote up
private List<String> sliceQueue(String queueID) {
    if (!lockQueue(queueID)) {
        throw new IllegalStateException("error for acquire queue lock for queue :" + queueID);
    }
    @Cleanup Jedis jedis = jedisPool.getResource();
    try {
        return jedis.lrange(makeSliceQueueKey(queueID), 0, -1);
    } finally {
        unLockQueue(queueID);
    }
}
 
Example 13
Source File: RedisTest.java    From java-study with Apache License 2.0 5 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args) {
	// 连接到本地的 redis服务
	Jedis jedis = new Jedis("127.0.0.1");
	System.out.println("连接成功");
	// 查看服务是否运行
	System.out.println("服务正在运行: " + jedis.ping());

	// 存储到列表中
	jedis.lpush("list", "redis");
	jedis.lpush("list", "java");
	jedis.lpush("list", "mysql");
	// 获取存储的数据并输出
	List<String> list = jedis.lrange("list", 0, 2);
	for (int i = 0, j = list.size(); i < j; i++) {
		System.out.println("list的输出结果:" + list.get(i));
	}

	// 设置 redis 字符串数据
	jedis.set("rst", "redisStringTest");
	// 获取存储的数据并输出
	System.out.println("redis 存储的字符串为: " + jedis.get("rst"));

	// 存储数据
	jedis.sadd("setTest1", "abc");
	jedis.sadd("setTest1", "abcd");
	jedis.sadd("setTest1", "abcde");
	// 获取数据并输出
	Set<String> keys = jedis.keys("*");
	// Set<String> keys=jedis.smembers("setTest1");
	// 定义迭代器输出
	Iterator<String> it = keys.iterator();
	while (it.hasNext()) {
		String key = it.next();
		System.out.println(key);
	}

}
 
Example 14
Source File: RedisServiceImpl.java    From blog_demos with Apache License 2.0 5 votes vote down vote up
@Override
public List<String> listGetAll(String key) {
    List<String> list = null;

    Jedis jedis = borrowJedis();

    if(null!=jedis){
        list = jedis.lrange(key, 0, -1);
        returnJedis(jedis);
    }

    return null==list ? new ArrayList() : list;
}
 
Example 15
Source File: Main.java    From JavaBase with MIT License 4 votes vote down vote up
private void transferList(String key, Jedis originJedis, Jedis targetJedis) {
  List<String> data = originJedis.lrange(key, 0, -1);
  String[] temps = new String[data.size()];
  String[] results = data.toArray(temps);
  targetJedis.lpush(key, results);
}
 
Example 16
Source File: DefaultRedis.java    From craft-atom with MIT License 4 votes vote down vote up
private List<String> lrange0(Jedis j, String key, long start, long stop) {
	return j.lrange(key, start, stop);
}
 
Example 17
Source File: JedisUtil.java    From Project with Apache License 2.0 3 votes vote down vote up
/**
 * 获取指定范围的记录,可以做为分页使用
 * 
 * @param  key
 * @param    start
 * @param   end
 * @return List
 */
public List<String> lrange(String key, long start, long end) {
	Jedis sjedis = getJedis();
	List<String> list = sjedis.lrange(key, start, end);
	sjedis.close();
	return list;
}
 
Example 18
Source File: JedisUtil.java    From Project with Apache License 2.0 3 votes vote down vote up
/**
 * 获取指定范围的记录,可以做为分页使用
 * 
 * @param  key
 * @param start
 * @param  end 如果为负数,则尾部开始计算
 * @return List
 */
public List<byte[]> lrange(byte[] key, int start, int end) {
	Jedis sjedis = getJedis();
	List<byte[]> list = sjedis.lrange(key, start, end);
	sjedis.close();
	return list;
}
 
Example 19
Source File: JedisUtil.java    From BigData with GNU General Public License v3.0 3 votes vote down vote up
/**
 * 获取指定范围的记录,可以做为分页使用
 * 
 * @param byte[] key
 * @param int start
 * @param int end 如果为负数,则尾部开始计算
 * @return List
 * */
public List<byte[]> lrange(byte[] key, int start, int end) {
	// ShardedJedis sjedis = getShardedJedis();
	Jedis sjedis = getJedis();
	List<byte[]> list = sjedis.lrange(key, start, end);
	returnJedis(sjedis);
	return list;
}
 
Example 20
Source File: RedisClient.java    From springboot-learn with MIT License 2 votes vote down vote up
/**
 * 列表获取
 *
 * @param key
 * @param start
 * @param end
 * @return
 */
public List<String> lRange(String key, long start, long end) {
    Jedis jedis = jedisPool.getResource();
    return jedis.lrange(key, start, end);
}