Esempio n. 1
0
 /**
  * 获取List
  *
  * @param key
  * @return
  */
 public List<String> getList(String key) {
   ShardedJedis shardedJedis = null;
   try {
     shardedJedis = slaveShardedJedisPool.getResource();
     return shardedJedis.lrange(key, 0, -1);
   } catch (Exception ex) {
     logger.error("getList error.", ex);
   } finally {
     returnResource(shardedJedis);
   }
   return null;
 }
Esempio n. 2
0
  private void ListOperate() {
    {
      System.out.println("======================list==========================");
      // 清空数据
      System.out.println("清空库中所有数据:" + jedis.flushDB());

      System.out.println("=============增=============");
      shardedJedis.lpush("stringlists", "vector");
      shardedJedis.lpush("stringlists", "ArrayList");
      shardedJedis.lpush("stringlists", "vector");
      shardedJedis.lpush("stringlists", "vector");
      shardedJedis.lpush("stringlists", "LinkedList");
      shardedJedis.lpush("stringlists", "MapList");
      shardedJedis.lpush("stringlists", "SerialList");
      shardedJedis.lpush("stringlists", "HashList");
      shardedJedis.lpush("numberlists", "3");
      shardedJedis.lpush("numberlists", "1");
      shardedJedis.lpush("numberlists", "5");
      shardedJedis.lpush("numberlists", "2");
      System.out.println("所有元素-stringlists:" + shardedJedis.lrange("stringlists", 0, -1));
      System.out.println("所有元素-numberlists:" + shardedJedis.lrange("numberlists", 0, -1));

      System.out.println("=============删=============");
      // 删除列表指定的值 ,第二个参数为删除的个数(有重复时),后add进去的值先被删,类似于出栈
      System.out.println("成功删除指定元素个数-stringlists:" + shardedJedis.lrem("stringlists", 2, "vector"));
      System.out.println("删除指定元素之后-stringlists:" + shardedJedis.lrange("stringlists", 0, -1));
      // 删除区间以外的数据
      System.out.println("删除下标0-3区间之外的元素:" + shardedJedis.ltrim("stringlists", 0, 3));
      System.out.println("删除指定区间之外元素后-stringlists:" + shardedJedis.lrange("stringlists", 0, -1));
      // 列表元素出栈
      System.out.println("出栈元素:" + shardedJedis.lpop("stringlists"));
      System.out.println("元素出栈后-stringlists:" + shardedJedis.lrange("stringlists", 0, -1));

      System.out.println("=============改=============");
      // 修改列表中指定下标的值
      shardedJedis.lset("stringlists", 0, "hello list!");
      System.out.println("下标为0的值修改后-stringlists:" + shardedJedis.lrange("stringlists", 0, -1));
      System.out.println("=============查=============");
      // 数组长度
      System.out.println("长度-stringlists:" + shardedJedis.llen("stringlists"));
      System.out.println("长度-numberlists:" + shardedJedis.llen("numberlists"));
      // 排序
      /*
       * list中存字符串时必须指定参数为alpha,如果不使用SortingParams,而是直接使用sort("list"),
       * 会出现"ERR One or more scores can't be converted into double"
       */
      SortingParams sortingParameters = new SortingParams();
      sortingParameters.alpha();
      sortingParameters.limit(0, 3);
      System.out.println(
          "返回排序后的结果-stringlists:" + shardedJedis.sort("stringlists", sortingParameters));
      System.out.println("返回排序后的结果-numberlists:" + shardedJedis.sort("numberlists"));
      // 子串: start为元素下标,end也为元素下标;-1代表倒数一个元素,-2代表倒数第二个元素
      System.out.println("子串-第二个开始到结束:" + shardedJedis.lrange("stringlists", 1, -1));
      // 获取列表指定下标的值
      System.out.println("获取下标为2的元素:" + shardedJedis.lindex("stringlists", 2) + "\n");
    }
  }
 /**
  * 通过key获取list指定下标位置的value
  *
  * <p>如果start 为 0 end 为 -1 则返回全部的list中的value
  *
  * @param key
  * @param start
  * @param end
  * @return
  */
 public List<String> lrange(String key, long start, long end) {
   ShardedJedis jedis = null;
   List<String> res = null;
   try {
     jedis = pool.getResource();
     res = jedis.lrange(key, start, end);
   } catch (Exception e) {
     pool.returnBrokenResource(jedis);
     e.printStackTrace();
   } finally {
     returnResource(pool, jedis);
   }
   return res;
 }
Esempio n. 4
0
 /**
  * 截取List
  *
  * @param key
  * @param start 起始位置
  * @param end 结束位置
  * @return
  */
 public List<String> rangeList(String key, long start, long end) {
   if (key == null || key.equals("")) {
     return null;
   }
   ShardedJedis shardedJedis = null;
   try {
     shardedJedis = slaveShardedJedisPool.getResource();
     return shardedJedis.lrange(key, start, end);
   } catch (Exception ex) {
     logger.error(
         "rangeList 出错[key=" + key + " start=" + start + " end=" + end + "]" + ex.getMessage(),
         ex);
   } finally {
     returnResource(shardedJedis);
   }
   return null;
 }