Basic Tutorials of Redis(6) - List
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-

lindex list-

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-

//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 + " ");
}

Basic Tutorials of Redis(6) - List的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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#? ...
- 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 ...
- 【转】Redis入门
Redis是一个开源,先进的key-value存储,并用于构建高性能,可扩展的Web应用程序的完美解决方案. Redis从它的许多竞争继承来的三个主要特点: Redis数据库完全在内存中,使用磁盘仅用 ...
随机推荐
- WPF 有用博客地址
增加智能感知的RichTextBox扩展控件(WPF) WPF自定义控件与样式(3)-TextBox & RichTextBox & PasswordBox样式.水印.Label标签. ...
- 关于Vue.js 2.0 的 Vuex 2.0,你需要更新的知识库
应用结构 实际上,Vuex 在怎么组织你的代码结构上面没有任何限制,相反,它强制规定了一系列高级的原则: 应用级的状态集中放在 store 中. 改变状态的唯一方式是提交mutations,这是个同步 ...
- [C#] C# 知识回顾 - 表达式树 Expression Trees
C# 知识回顾 - 表达式树 Expression Trees 目录 简介 Lambda 表达式创建表达式树 API 创建表达式树 解析表达式树 表达式树的永久性 编译表达式树 执行表达式树 修改表达 ...
- C# 生成验证码图片时消除锯齿
引言 基于生成图片实现了一个手机号转图片的需求. 内容也很简单,直接用手机号生成一个png图片.就是为了背景透明以便其他地方调用. 有无锯齿主要依靠一句代码:g.TextRenderingHint= ...
- 深入Java虚拟机--判断对象存活状态
程序计数器,虚拟机栈和本地方法栈 首先我们先来看下垃圾回收中不会管理到的内存区域,在Java虚拟机的运行时数据区我们可以看到,程序计数器,虚拟机栈,本地方法栈这三个地方是比较特别的.这个三个部分的特点 ...
- ASP.NET Core 中文文档 第四章 MVC(4.4)依赖注入和控制器
原文: Dependency Injection and Controllers 作者: Steve Smith 翻译: 刘浩杨 校对: 孟帅洋(书缘) ASP.NET Core MVC 控制器应通过 ...
- iOS controller解耦探究实现——第一次写博客
大学时曾经做过android的开发,目前的工作是iOS的开发.之前自己记录东西都是通过自己比较喜欢的笔记类的应用记录下了.直到前段时一个哥们拉着我注册了一个博客.现在终于想明白了,博客这个东西受众会稍 ...
- C# 对象实例化 用json保存 泛型类 可以很方便的保存程序设置
用于永久化对象,什么程序都行,依赖NewtonSoft.用于json序列化和反序列化. using Newtonsoft.Json; using System; using System.Collec ...
- ecshop验证码
<?php //仿制ecshop验证码(四位大写字母和数字.背景) //处理码值(四位大写字母和数字组成) //所有的可能的字符集合 $chars = 'ABCDEFGHIJKLMNOPQRST ...
- 网站里加入QQ在线客服
1.开启"QQ在线状态"服务 http://jingyan.baidu.com/article/b24f6c823425a586bfe5da1f.html http://www. ...