好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

redis 获取 list 中的所有元素操作

一种方法是用 lrange( key, 0, -1 )。这种方法不会影响 redis list 中的数据。

?

1

list<string> list = jedis.lrange( key, 0 , - 1 );

另一种方法是用 while + lpop 。这种方法会将 redis list 中的数据都弹出来,redis list 就变成空的了。

?

1

2

3

4

5

6

7

list<string> list = new arraylist<>();

string st = jedis.lpop( key );

while ( st != null ) {

   list.add( st );

  

   st = jedis.lpop( key );

}

这两种方法获得的 list<string> list 中的元素的顺序是一样的。

补充:redis列表类型list如何一次返回多个值并删除这些值

redis的列表类型list是一个常用的数据类型,但是这个类型并不支持一次性返回多个值并删除这些已经返回的值。

其实我们可以通过redis的事务,来完成这个一次性返回多个值并删除这些已经返回的值的需求。

redis中的事务就是一组命令的集合,这些命令要么全部执行,要么全都不执行。redis事务的原理就是一次性将命令都发给服务端,

当服务接收到exec命令之后,按照事务中命令的顺序依次执行事务中的命令。exec命令的返回值就是事务中依次执行的命令返回值的集合,返回值的顺序和命令的执行顺序相同。如果在发送exec命令前,客户端和服务端失去连接,这时redis会清空这个事务队列。

介绍完这些原理,我们再来看看如何完成一次性返回多个值并删除这些已经返回的值的需求。

我们这里要利用两个列表类型的命令:lrange和ltrim

lrange key start end // 从左边依次返回key的[start,end] 的所有值,注意返回结果包含两端的值。

ltrim key start end //删除指定索引之外的所有元素,注意删除之后保留的元素包含两端的start和end索引值。

我们这里举例测试:

我们构造了一个名为yujie_list的列表类型数据结构,从左边依次压入:0 1 2 3 4 5 6 7 8 9

最后从左到右依次列出列表中的所有元素如上图所示。

接下来我们测试lrange和ltrim命令如下图:

我们使用lrange yujie_list 0 3命令,从左到右依次列出从索引0到索引3的元素,注意包含了索引0 值为9和索引3值为6的元素。

我们使用ltrim yujie_list 4 -1命令,删除索引4到最右端之外的所有元素,注意删除的元素不包含索引4职位5的元素。

好了原理讲完了,接下来我们上代码:

redisutil是一个工具类,用于连接redis服务端。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

/**

  * 连接redis服务的工具类

  * @author yujie.wang3

  *

  */

public final class redisutil { 

   //redis服务器ip

   private static string addr = "10.4.36.93" ;

  

   //redis的端口号

   private static int port = 6379 ; 

  

   //可用连接实例的最大数目,默认值为8;

   //如果赋值为-1,则表示不限制;如果pool已经分配了maxactive个jedis实例,则此时pool的状态为exhausted(耗尽)。

   private static int max_active = 100 ;

  

   //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。

   private static int max_idle = 20 ;

  

   //等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出jedisconnectionexception;

   private static int max_wait = 10000 ;

  

   private static int timeout = 10000 ;

  

   //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;

   private static boolean test_on_borrow = true ;

  

   private static jedispool jedispool = null ;

  

   /**

    * 初始化redis连接池

    */

   static {

     try {

       jedispoolconfig config = new jedispoolconfig();

       config.setmaxactive(max_active);

       config.setmaxidle(max_idle);

       config.setmaxwait(max_wait);

       config.settestonborrow(test_on_borrow);

       jedispool = new jedispool(config, addr, port);

     } catch (exception e) {

       e.printstacktrace();

     }

   }

  

   /**

    * 获取jedis实例

    * @return

    */

   public synchronized static jedis getjedis() {

     try {

       if (jedispool != null ) {

         jedis resource = jedispool.getresource();

         return resource;

       } else {

         return null ;

       }

     } catch (exception e) {

       e.printstacktrace();

       return null ;

     }

   }

  

   /**

    * 释放jedis资源

    * @param jedis

    */

   public static void returnresource( final jedis jedis) {

     if (jedis != null ) {

       jedispool.returnresource(jedis);

     }

   }

}

测试类如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

/**

  * 一次返回多个列表值并删除返回值测试类

  * @author yujie.wang

  *

  */

public class redistest {

 

  public static void main(string[] args) {

  string key = "yujie_test_list" ;

  initlist(key, 9 );

  printlist(key, "原始列表数据" );

  list<string> listresult = getlistmultvalueafterdel(key, 0 , 3 );

  system.out.println( "一次返回并删除数据:" +listresult.tostring());

  printlist(key, "删除之后列表数据" );

  }

 

  public static void initlist(string key, int maxvalue){

  jedis client = redisutil.getjedis();

  for ( int i = 0 ;i <= maxvalue; i++){

   client.lpush(key, string.valueof(i));

  }

  system.out.println( "初始化列表:" + key + "完毕" );

  }

 

  public static void printlist(string key,string message){

  jedis client = redisutil.getjedis();

  list<string> list = client.lrange(key, 0 , - 1 );

  system.out.println(message+ " : " + list.tostring());

  }

 

  @suppresswarnings ( "unchecked" )

  public static list<string> getlistmultvalueafterdel(string key, int start, int end){

  list<object> list = null ;

  list<string> liststr = new arraylist<string>();

  try {

   jedis jedis = redisutil.getjedis();

   transaction ts = jedis.multi();

   ts.lrange(key, start, end);

   ts.ltrim(key, end+ 1 , - 1 );

   list = ts.exec();

   redisutil.returnresource(jedis);

  } catch (exception e) {

   // todo: handle exception

   system.out.println(e);

  }

  if (list != null && !list.isempty()){

   try {

   //获得命令lrange(key, start, end)的返回结果

   liststr = (arraylist<string>)list.get( 0 );

   } catch (exception e) {

   // todo: handle exception

   system.out.println(e);

   } 

  } else {

   return collections.emptylist();

  }

  return liststr;

  }

 

}

输出结果:

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/beguile/article/details/82047982

查看更多关于redis 获取 list 中的所有元素操作的详细内容...

  阅读:42次