加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Basic Tutorials of Redis(6) - List

发布时间:2020-12-16 04:46:59 所属栏目:安全 来源:网络整理
导读: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 o

  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- -

?  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- -

?  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- -

lrange list- -

?  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- before - after

?  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-

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

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

  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-

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

rpop list-

?  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-

?
  The following code demonastrates the basic usage of List in StackExchange.Redis.
db.ListLeftPush(, list_1 = RedisValue[] { , db.ListLeftPush( Console.WriteLine( ( item db.ListRange( Console.Write(item+ Console.WriteLine( db.ListRightPush(, list_2 = RedisValue[] { , db.ListRightPush( Console.WriteLine( ( item db.ListRange( Console.Write(item + Console.WriteLine( db.ListInsertBefore(,, Console.WriteLine( ( item db.ListRange( Console.Write(item + db.ListInsertAfter(, Console.WriteLine( ( item db.ListRange( Console.Write(item + Console.WriteLine( Console.WriteLine(.Format(,db.ListLength( Console.WriteLine(.Format(,db.ListGetByIndex(, db.ListSetByIndex(,, Console.WriteLine( ( item db.ListRange( Console.Write(item + Console.WriteLine( Console.WriteLine(.Format(,db.ListLeftPop( Console.WriteLine(.Format(,db.ListRightPop( db.ListRemove(,, Console.WriteLine( ( item db.ListRange( Console.Write(item + }
  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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读