Basic Tutorials of Redis(6) - List

來源:互聯網
上載者:User

標籤:

  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 11lpush 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 11rpush 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 90linsert 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             //rpush12             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             //linsert22             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             //llen36             Console.WriteLine(string.Format("the length of list-1 is {0}",db.ListLength("list-1")));37             //lindex38             Console.WriteLine(string.Format("the element in the second index is {0}", db.ListGetByIndex("list-1",2)));39             //lset40             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             //lpop48             Console.WriteLine(string.Format("lpop element {0}", db.ListLeftPop("list-1")) );49             //rpop50             Console.WriteLine(string.Format("rpop element {0}", db.ListRightPop("list-1")));51             //lrem52             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

 

Basic Tutorials of Redis(6) - List

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.