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-
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-
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
linsert list- 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.
              //lpush
db.ListLeftPush("list-1", );
var list_1 = new RedisValue[] { ,};
db.ListLeftPush("list-1", list_1);
Console.WriteLine("after lpush:");
foreach (var item in db.ListRange("list-1"))
{
Console.Write(item+" ");
}
Console.WriteLine("");
//rpush
db.ListRightPush("list-2", );
var list_2 = new RedisValue[] { , };
db.ListRightPush("list-2", list_1);
Console.WriteLine("after rpush:");
foreach (var item in db.ListRange("list-2"))
{
Console.Write(item + " ");
}
Console.WriteLine("");
//linsert
db.ListInsertBefore("list-1",,);
Console.WriteLine("after linsert 90 before 12:");
foreach (var item in db.ListRange("list-1"))
{
Console.Write(item + " ");
}
db.ListInsertAfter("list-1", , );
Console.WriteLine("\nafter linsert 80 after 12:");
foreach (var item in db.ListRange("list-1"))
{
Console.Write(item + " ");
}
Console.WriteLine("");
//llen
Console.WriteLine(string.Format("the length of list-1 is {0}",db.ListLength("list-1")));
//lindex
Console.WriteLine(string.Format("the element in the second index is {0}", db.ListGetByIndex("list-1",)));
//lset
db.ListSetByIndex("list-1", , );
Console.WriteLine("after lset 66 from index 2:");
foreach (var item in db.ListRange("list-1"))
{
Console.Write(item + " ");
}
Console.WriteLine("");
//lpop
Console.WriteLine(string.Format("lpop element {0}", db.ListLeftPop("list-1")) );
//rpop
Console.WriteLine(string.Format("rpop element {0}", db.ListRightPop("list-1")));
//lrem
db.ListRemove("list-1", ,);
Console.WriteLine("after remove:");
foreach (var item in db.ListRange("list-1"))
{
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

Basic Tutorials of Redis(6) - List的更多相关文章

  1. Basic Tutorials of Redis(9) -First Edition RedisHelper

    After learning the basic opreation of Redis,we should take some time to summarize the usage. And I w ...

  2. Basic Tutorials of Redis(2) - String

    This post is mainly about how to use the commands to handle the Strings of Redis.And I will show you ...

  3. Basic Tutorials of Redis(8) -Transaction

    Data play an important part in our project,how can we ensure correctness of the data and prevent the ...

  4. Basic Tutorials of Redis(7) -Publish and Subscribe

    This post is mainly about the publishment and subscription in Redis.I think you may subscribe some o ...

  5. Basic Tutorials of Redis(5) - Sorted Set

    The last post is mainly about the unsorted set,in this post I will show you the sorted set playing a ...

  6. Basic Tutorials of Redis(4) -Set

    This post will introduce you to some usages of Set in Redis.The Set is a unordered set,it means that ...

  7. Basic Tutorials of Redis(3) -Hash

    When you first saw the name of Hash,what do you think?HashSet,HashTable or other data structs of C#? ...

  8. Basic Tutorials of Redis(1) - Install And Configure Redis

    Nowaday, Redis became more and more popular , many projects use it in the cache module and the store ...

  9. 【转】Redis入门

    Redis是一个开源,先进的key-value存储,并用于构建高性能,可扩展的Web应用程序的完美解决方案. Redis从它的许多竞争继承来的三个主要特点: Redis数据库完全在内存中,使用磁盘仅用 ...

随机推荐

  1. 15个关于Chrome的开发必备小技巧[译]

    谷歌Chrome,是当前最流行且被众多web开发人员使用的浏览器.最快六周就更新发布一次以及伴随着它不断强大的开发组件,使得Chrome成为你必备的开发工具.例如,在线编辑CSS,console以及d ...

  2. Python多线程爬虫爬取电影天堂资源

    最近花些时间学习了一下Python,并写了一个多线程的爬虫程序来获取电影天堂上资源的迅雷下载地址,代码已经上传到GitHub上了,需要的同学可以自行下载.刚开始学习python希望可以获得宝贵的意见. ...

  3. Java之多态(二)

    package test05;import test06.Car1;public class DuoTai_Test02 { /**多个对象,一个形态 * Tiger.Lion.Snake → Ani ...

  4. JavaScript中String对象的方法介绍

    1.字符方法 1.1 charAt() 方法,返回字符串中指定位置的字符. var question = "Do you like JavaScript?"; alert(ques ...

  5. iOS 10 跳转系统设置

    苦心人天不负, 为了项目终于把 iOS 10 跳转系统设置的方法给搞定了, 很欣慰. http://www.cnblogs.com/lurenq/p/6189580.html iOS 10 跳转系统设 ...

  6. 《月之猎人 (Moon Hunters)》主角设计

    原文链接 游戏开发人员,你们好! 我是 Kitfox Games 工作室的总监 Tanya,我们的工作室位于加拿大的蒙特利尔,拥有六名员工. 我们 3 月份发布了<月之猎人>游戏的桌面版, ...

  7. [PHP源码阅读]array_slice和array_splice函数

    array_slice和array_splice函数是用在取出数组的一段切片,array_splice还有用新的切片替换原删除切片位置的功能.类似javascript中的Array.prototype ...

  8. Windows forfiles(删除历史文件)

    200 ? "200px" : this.width)!important;} --> 介绍 forfiles是windows自带的一个批量删除命令,对于时间的判断是通过文件 ...

  9. mvc4 自定义HtmlHelper

    好久没写博客了,最近只看博客不写的习惯很不好啊. 好了,最近的项目中大量的用到了表单,很多表单有特殊的编写,但是在该项目中又有很多重复的地方,这个时候若能封装成htmlhelper将大大降低工作量的. ...

  10. 增强版字典DictionaryEx

    代码 public class DictionaryEx<TKey, TValue> : IDictionary<TKey, TValue> { /// <summary ...