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.

  1. lpush list-
  2. 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.

  1. rpush list-
  2. 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.

  1. lrange list- -

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

  1. linsert list- before
  2. 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.

  1. llen list-

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

  We also can modify the elements by their index.
  1. 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.

  1. lpop list-

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

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

  1. lrem list-
 
  The following code demonastrates the basic usage of List in StackExchange.Redis.
  1. //lpush
  2. db.ListLeftPush("list-1", );
  3. var list_1 = new RedisValue[] { ,};
  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", );
  13. var list_2 = new RedisValue[] { , };
  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",,);
  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", , );
  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. //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",)));
  39. //lset
  40. db.ListSetByIndex("list-1", , );
  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", ,);
  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的更多相关文章

  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. jQuery学习之路(4)- 动画

    ▓▓▓▓▓▓ 大致介绍 通过jQuery中基本的动画方法,能够轻松地为网页添加非常精彩的视觉效果,给用户一种全新的体验 ▓▓▓▓▓▓ jQuery中的动画 ▓▓▓▓▓▓ show()和hide()方法 ...

  2. python+uwsgi导致redis无法长链接引起性能下降问题记录

    今天在部署python代码到预生产环境时,web站老是出现redis链接未初始化,无法连接到服务的提示,比对了一下开发环境与测试环境代码,完全一致,然后就是查看各种日志,排查了半天也没有查明是什么原因 ...

  3. [.NET] 怎样使用 async & await 一步步将同步代码转换为异步编程

    怎样使用 async & await 一步步将同步代码转换为异步编程 [博主]反骨仔 [出处]http://www.cnblogs.com/liqingwen/p/6079707.html  ...

  4. iOS开发之Alamofire源码深度解析

    今天博客中的Alamofire源码的版本是以现在最新的3.4版本为例.上篇博客系统的对NSURLSession相关的东西进行了详细的解析,详情请看<详解NSURLSession>,为了就是 ...

  5. C#如何在PDF文件添加图片印章

    文档中添加印章可以起一定的作用,比如,防止文件随意被使用,或者确保文档内容的安全性和权威性.C#添加图片印章其实也有很多实现方法,这里我使用的是免费的第三方软件Free Spire.PDF,向大家阐述 ...

  6. Collection集合

    一些关于集合内部算法可以查阅这篇文章<容器类总结>. (Abstract+) Collection 子类:List,Queue,Set 增: add(E):boolean addAll(C ...

  7. .net core和angular2之前端篇—1

    2016-10-20更新 今天的这篇文章还是一篇"Hello World",只不过开发环境有所改变--Visual Studio Code+Angular2+Webapck,也算是 ...

  8. Linux设备管理(五)_写自己的sysfs接口

    我们在Linux设备管理(一)_kobject, kset,ktype分析一文中介绍了kobject的相关知识,在Linux设备管理(二)_从cdev_add说起和Linux设备管理(三)_总线设备的 ...

  9. ola.hallengren的SQL Server维护脚本

    ola.hallengren的SQL Server维护脚本 下载地址 http://files.cnblogs.com/files/lyhabc/ola.hallengrenMaintenanceSo ...

  10. 增强版字典DictionaryEx

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