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-1 11 lpush list-1 12 13
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-2 11 rpush list-2 12 13
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-1 0 -1
lrange list-2 0 -1
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-1 before 12 90 linsert list-1 after 12 80
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-1
lindex list-1 2
lset list-1 2 66
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-1
rpop will remove the rightmost element from the list.And the client will return the removed element as well.
rpop list-1

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-1 2 66

1 //lpush 2 db.ListLeftPush("list-1", 11); 3 var list_1 = new RedisValue[2] { 12,13}; 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", 11); 13 var list_2 = new RedisValue[2] { 12, 13 }; 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",12,90); 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", 12, 80); 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",2))); 39 //lset 40 db.ListSetByIndex("list-1", 2, 66); 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", 66,2); 53 Console.WriteLine("after remove:"); 54 foreach (var item in db.ListRange("list-1")) 55 { 56 Console.Write(item + " "); 57 }

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述