zoukankan      html  css  js  c++  java
  • Basic Tutorials of Redis(6)

      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("
    after 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

     

  • 相关阅读:
    vue3学习笔记
    学习笔记-Python-爬虫5-Selenium + PhantomJS
    学习笔记-Python-爬虫4-数据提取-正则、XML、XPath、BeautifulSoup4
    学习笔记-Python-爬虫7-Scrapy、分布式爬虫
    学习笔记-Python-爬虫6-验证码
    学习笔记-Python-爬虫3-requests
    学习笔记-Python-爬虫2-SSL、js加密、ajax
    学习笔记-Python-爬虫1-urllib、chardet
    Java多线程同步问题
    jQuery限制文本框只能输入正整数
  • 原文地址:https://www.cnblogs.com/catcher1994/p/5858772.html
Copyright © 2011-2022 走看看