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. C# 正则表达式大全

    文章导读 正则表达式的本质是使用一系列特殊字符模式,来表示某一类字符串.正则表达式无疑是处理文本最有力的工具,而.NET提供的Regex类实现了验证正则表达式的方法.Regex 类表示不可变(只读)的 ...

  2. Android数据存储之Android 6.0运行时权限下文件存储的思考

    前言: 在我们做App开发的过程中基本上都会用到文件存储,所以文件存储对于我们来说是相当熟悉了,不过自从Android 6.0发布之后,基于运行时权限机制访问外置sdcard是需要动态申请权限,所以以 ...

  3. [C#] 走进 LINQ 的世界

    走进 LINQ 的世界 序 在此之前曾发表过三篇关于 LINQ 的随笔: 进阶:<LINQ 标准查询操作概述>(强烈推荐) 技巧:<Linq To Objects - 如何操作字符串 ...

  4. 算法与数据结构(十七) 基数排序(Swift 3.0版)

    前面几篇博客我们已经陆陆续续的为大家介绍了7种排序方式,今天博客的主题依然与排序算法相关.今天这篇博客就来聊聊基数排序,基数排序算法是不稳定的排序算法,在排序数字较小的情况下,基数排序算法的效率还是比 ...

  5. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

  6. 如何在ASP.Net创建各种3D图表

    我们都知道,图表在ASP.NET技术中是一种特别受欢迎而又很重要的工具.图表是表示数据的图形,一般含有X和Y两个坐标轴.我们可以用折线,柱状,块状来表示数据.通过图表控件,我们即能表示数据又能比较各种 ...

  7. 现代3D图形编程学习-基础简介(3)-什么是opengl (译)

    本书系列 现代3D图形编程学习 OpenGL是什么 在我们编写openGL程序之前,我们首先需要知道什么是OpenGL. 将OpenGL作为一个API OpenGL 通常被认为是应用程序接口(API) ...

  8. Mysql - 查询之关联查询

    查询这块是重中之重, 关系到系统反应时间. 项目做到后期, 都是要做性能测试和性能优化的, 优化的时候, 数据库这块是一个大头. sql格式: select 列名/* from 表名 where 条件 ...

  9. MongoDB学习笔记二—Shell操作

    数据类型 MongoDB在保留JSON基本键/值对特性的基础上,添加了其他一些数据类型. null null用于表示空值或者不存在的字段:{“x”:null} 布尔型 布尔类型有两个值true和fal ...

  10. HTML5 标签 details 展开 搜索

    details有一个新增加的子标签--summary,当鼠标点击summary标签中的内容文字时,details标签中的其他所有元素将会展开或收缩. 默认状态为 收缩状态 设置为展开状态为 <d ...