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 has a clear mind of them.
There is commands we can use the 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 to the key named List-1,then add element and the. Here ' re the commands and result.
Lpush list-1lpush list-1
The code demonstrated above push the element from left to the right. As all we know,linkedlist have Anonther
The to push the elements. Rpush is the another. For example,i push the same elements to the key named List-2.
Taking the following code and result.
Rpush list-2rpush list-2
By using those-commands to store the data,we don ' t know the difference between them apparently. But when
You select all of the elements,you would find out something. Using Lrange can make us know the elements in the list.
Lrange list-10 -1
Lrange list-20 -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 before in list-1, and insert after 12.You would get the following result.
Linsert list-1linsert list-1
Sometimes we want to know what many elements in the list? Just as the length of a string. We Use the 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-12
We also can modify the elements by their index.
LSet list-12
The next time I'll show you what to remove the elements from the list. There is three commands can help
us to remove elements.
Lpop 'll remove the leftmost element from the list. And the client would return the removed element.
Lpop list-1
Rpop 'll remove the rightmost element from the list. And the client would return the removed element as well.
Rpop list-1
Lrem would remove the count occurrences of elements equal to value. If count > 0,it would remove elements moving
From head to tail. If Count < 0,it would remove elements moving from tail to head. If count = 0,it would remove all elements
equal to value.
Lrem list-12
The following code demonastrates the basic usage of the List in Stackexchange.redis.
1 //Lpush2Db. Listleftpush ("list-1", One);3 varList_1 =Newredisvalue[2] { A, -};4Db. Listleftpush ("list-1", list_1);5Console.WriteLine ("After Lpush:");6 foreach(varIteminchDb. ListRange ("list-1"))7 {8Console.Write (item+" ");9 }TenConsole.WriteLine (""); One //Rpush ADb. Listrightpush ("list-2", One); - varList_2 =Newredisvalue[2] { A, - }; -Db. Listrightpush ("list-2", list_1); theConsole.WriteLine ("After Rpush:"); - foreach(varIteminchDb. ListRange ("list-2")) - { -Console.Write (item +" "); + } -Console.WriteLine (""); + //Linsert ADb. Listinsertbefore ("list-1", A, -); atConsole.WriteLine ("After Linsert before:"); - foreach(varIteminchDb. ListRange ("list-1")) - { -Console.Write (item +" "); - } -Db. Listinsertafter ("list-1", A, the); inConsole.WriteLine ("\nafter Linsert:"); - foreach(varIteminchDb. ListRange ("list-1")) to { +Console.Write (item +" "); - } theConsole.WriteLine (""); * //Llen $Console.WriteLine (string. Format ("The length of list-1 is {0}"Db. Listlength ("list-1")));Panax Notoginseng //lindex -Console.WriteLine (string. Format ("The element in the second index is {0}"Db. Listgetbyindex ("list-1",2))); the //LSet +Db. Listsetbyindex ("list-1",2, the); AConsole.WriteLine ("After LSet-from index 2:"); the foreach(varIteminchDb. ListRange ("list-1")) + { -Console.Write (item +" "); $ } $Console.WriteLine (""); - //Lpop -Console.WriteLine (string. Format ("lpop element {0}"Db. Listleftpop ("list-1")) ); the //Rpop -Console.WriteLine (string. Format ("rpop element {0}"Db. Listrightpop ("list-1")));Wuyi //Lrem theDb. Listremove ("list-1", the,2); -Console.WriteLine ("After remove:"); Wu foreach(varIteminchDb. ListRange ("list-1")) - { AboutConsole.Write (item +" "); $}
When you debug the codes,the results is as follow.
The next post of this series are the basic opreation of publish and subscribe in redis.thanks for your reading
Basic Tutorials of Redis (6)-List