Basic Tutorials of Redis(6) - List

  Redis's List is different from C#'s List,but similar with C#'s LinkedList.Sometimes I confuse with them.I expect

that you won't mix them and have a clear mind of them.

  There are 17 commands we can use in List.

  

   Push and pop are the base opreation of the linkediist,either as Redis's List.When we want to store the

list, lpush and rpush can help us to save the data.Both of them can save one or more values to the key.Now

I add element 11 to the key named list-1,then add element 12 and 13 to this key.Here're the commands and result.

lpush list-1 11
lpush list-1 12 13

   The code demonstrated above push the element from left to the right.As all we know,linkedlist has anonther

way to push the elements.rpush is the another way.For example,I push the same elements to the key named list-2.

Taking the following code and result.

rpush list-2 11
rpush list-2 12 13

   By using those two commands to store the data,we don't know the Difference between them apparently.But when

you select all of the elements,you will find out something.Using lrange can make us know the elements in the list.

lrange list-1 0 -1

lrange list-2 0 -1 

   The next picture explain the result clearly.You can combine the linkedlist's feature to think about the result.

  We also can insert an element before or after another element.Redis provides a command for us.For an instance,

I insert 90 before 12 on list-1 ,and insert 80 after 12.You will get the following result.

linsert list-1 before 12 90
linsert list-1 after 12 80

   Sometimes we may want to know how many elements in the list?Just as the length of a string.We use llen to

get the length of the list.

llen list-1 

  We also can get the elements by their own index in the list.
lindex list-1 2

  We also can modify the elements by their index.
lset list-1 2 66

  The next time I will show you how to remove the elements from the list.There are three commands can help

us to remove elements.

  lpop will remove the leftmost element from the list.And the client will return the removed element.

lpop list-1

   rpop will remove the rightmost element from the list.And the client will return the removed element as well.

rpop list-1

   lrem will remove the count occurrences of elements equal to value.If count > 0,it will remove elements moving

from head to tail.If count < 0,it will remove elements moving from tail to head.If count = 0,it will remove all elements

equal to value.

lrem list-1 2 66
 
  The following code demonastrates the basic usage of List in StackExchange.Redis.
 1              //lpush
 2             db.ListLeftPush("list-1", 11);
 3             var list_1 = new RedisValue[2] { 12,13};
 4             db.ListLeftPush("list-1", list_1);
 5             Console.WriteLine("after lpush:");
 6             foreach (var item in db.ListRange("list-1"))
 7             {
 8                 Console.Write(item+" ");
 9             }
10             Console.WriteLine("");
11             //rpush
12             db.ListRightPush("list-2", 11);
13             var list_2 = new RedisValue[2] { 12, 13 };
14             db.ListRightPush("list-2", list_1);
15             Console.WriteLine("after rpush:");
16             foreach (var item in db.ListRange("list-2"))
17             {
18                 Console.Write(item + " ");
19             }
20             Console.WriteLine("");
21             //linsert
22             db.ListInsertBefore("list-1",12,90);
23             Console.WriteLine("after linsert 90 before 12:");
24             foreach (var item in db.ListRange("list-1"))
25             {
26                 Console.Write(item + " ");
27             }
28             db.ListInsertAfter("list-1", 12, 80);
29             Console.WriteLine("\nafter linsert 80 after 12:");
30             foreach (var item in db.ListRange("list-1"))
31             {
32                 Console.Write(item + " ");
33             }
34             Console.WriteLine("");
35             //llen
36             Console.WriteLine(string.Format("the length of list-1 is {0}",db.ListLength("list-1")));
37             //lindex
38             Console.WriteLine(string.Format("the element in the second index is {0}", db.ListGetByIndex("list-1",2)));
39             //lset
40             db.ListSetByIndex("list-1", 2, 66);
41             Console.WriteLine("after lset 66 from index 2:");
42             foreach (var item in db.ListRange("list-1"))
43             {
44                 Console.Write(item + " ");
45             }
46             Console.WriteLine("");
47             //lpop
48             Console.WriteLine(string.Format("lpop element {0}", db.ListLeftPop("list-1")) );
49             //rpop
50             Console.WriteLine(string.Format("rpop element {0}", db.ListRightPop("list-1")));
51             //lrem
52             db.ListRemove("list-1", 66,2);
53             Console.WriteLine("after remove:");
54             foreach (var item in db.ListRange("list-1"))
55             {
56                 Console.Write(item + " ");
57             }
  When you debug the codes,the results are as follow.

  

  The next post of this series is the basic opreation of publish and subscribe in Redis.Thanks for your reading

 

转载于:https://www.cnblogs.com/catcher1994/p/5858772.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值