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数据库完全在内存中,使用磁盘仅用 ...
随机推荐
- OpenCASCADE Expression Interpreter by Flex & Bison
OpenCASCADE Expression Interpreter by Flex & Bison eryar@163.com Abstract. OpenCASCADE provide d ...
- php报错 ----> Call to undefined function imagecreatetruecolor()
刚才在写验证码的时候,发现报错,然后排查分析了一下,原来是所用的php版本(PHP/5.3.13)没有开启此扩展功能. 进入php.ini 找到extension=php_gd2.dll ,将其前面的 ...
- 神技!微信小程序(应用号)抢先入门教程(附最新案例DEMO-豆瓣电影)持续更新
微信小程序 Demo(豆瓣电影) 由于时间的关系,没有办法写一个完整的说明,后续配合一些视频资料,请持续关注 官方文档:https://mp.weixin.qq.com/debug/wxadoc/de ...
- cocos2dx调用浏览器打开网址
安卓端cocos2dx/platform/android路径下CCApplication.h: virtual void openURL(const char* pszUrl); CCApplicat ...
- Oracle第一步
Oracle 启动数据库 Startup [NOMOUNT|MOUNT|OPEN|FORCE] [restrict] [pfile=filename] 启动实例,加载数据库,启动数据库 oRACLE关 ...
- AutoMapper(四)
返回总目录 自定义值解析 虽然AutoMapper覆盖了相当一部分目标成员的映射场景,但是还有 1-5%的目标值需要解析处理一下.很多时候,自定义的值解析是可以放在领域层的领域逻辑.然而,如果该逻辑只 ...
- 从零开始,DIY一个jQuery(2)
在上篇文章我们简单实现了一个 jQuery 的基础结构,不过为了顺应潮流,这次咱把它改为模块化的写法,此举得以有效提升项目的可维护性,因此在后续也将以模块化形式进行持续开发. 模块化开发和编译需要用上 ...
- MySQL8:连接查询
连接查询 连接是关系型数据库模型的主要特点. 连接查询是关系型数据库中最主要的查询,主要包括内连接.外连接等通过联结运算符可以实现多个表查询. 在关系型数据库管理系统中,表建立时各种数据之间的关系不必 ...
- 使用Maven+Nexus+Jenkins+Svn+Tomcat+Sonar搭建持续集成环境(一)
前言 但凡一个略有规模的项目都需要一个持续集成环境的支撑,为什么需要持续集成环境,我们来看一个例子.假如一个项目,由A.B两位程序员来协作开发,A负责前端模块,B负责后端模块,前端依赖后端.A ...
- 模拟实现Spring中的注解装配
本文原创,地址为http://www.cnblogs.com/fengzheng/p/5037359.html 在Spring中,XML文件中的bean配置是实现Spring IOC的核心配置文件,在 ...