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#?As for me,
the first time I saw the Hash,I considered is as the HashTable.Actually,Hash can identify with HashTable,the same as
DataRow.A row data of table can Regular as a Hash's data.The below picture may help you to card the point.

Hash to the database?And how can we get the Hash from the database.Command hset,hmset,hget,hmget can help us to
solve those two question.Now I use hset to add a key named user-1 with a filed named name , and the value of the filed is
hset user-1 name catcher hget user-1 name
hmset user-1 age 18 gender male hmget user-1 name age gender
the Hash.And it will return a integer,meaning there are 3 fileds in the user-1.
hlen user-1
hgetall command,this command will return all of the fileds and the values of this key.
hgetall user-1
gender filed from the user-1.
hdel user-1 gender
job.As you can see,I judge wheather gender and name exists in the user-1.
hexists user-1 gender hexists user-1 name
values of the hash.At this situation,some people may use hgetall to finish the requirements,but I don't suggest to do
more than the request.So I will use the hkeys to get all the fileds of the hash,and use the hvals to get all the values of
the hash.
hkeys user-1 hvals user-1
How about increase a filed's value like the string do.As for me ,both of the increased command and decreased command
are the same.Because of their regular usage.For example,I increase the age of the user-1 by 2, and you will get the result like
the below image.
hincr user-1 age 2
1 //hset hget hmset hmget 2 db.HashSet("user-1", "name", "catcher"); 3 var user_1 = new HashEntry[2] { new HashEntry("age",18),new HashEntry("gender","male") }; 4 db.HashSet("user-1", user_1); 5 6 Console.WriteLine("the name of user-1 is {0}",db.HashGet("user-1","name")); 7 var user_1_fileds = new RedisValue[] { "name","age","gender" }; 8 var user_1_values = db.HashGet("user-1", user_1_fileds); 9 foreach (var item in user_1_values) 10 { 11 Console.WriteLine(item); 12 } 13 14 //hlen 15 Console.WriteLine(string.Format("the number of filed in user-1 is {0}",db.HashLength("user-1"))); 16 17 //hgetall 18 var all = db.HashGetAll("user-1"); 19 foreach (var item in all) 20 { 21 Console.WriteLine(string.Format("the {0} of user-1 is {1}",item.Name,item.Value)); 22 } 23 24 //hdel 25 db.HashDelete("user-1", "gender"); 26 var all_after_del = db.HashGetAll("user-1"); 27 foreach (var item in all_after_del) 28 { 29 Console.WriteLine(string.Format("the {0} of user-1 is {1}", item.Name, item.Value)); 30 } 31 32 //hexists 33 Console.WriteLine(string.Format("gender {0} in the user-1", db.HashExists("user-1", "gender")?"is":"isn't")); 34 Console.WriteLine(string.Format("gender {0} in the user-1", db.HashExists("user-1", "name") ? "is" : "isn't")); 35 36 //hkeys 37 var keys = db.HashKeys("user-1"); 38 Console.WriteLine("the keys of user-1 are as follow:"); 39 foreach (var item in keys) 40 { 41 Console.WriteLine(item); 42 } 43 44 //hvals 45 var values = db.HashValues("user-1"); 46 Console.WriteLine("the values of user-1 are as follow:"); 47 foreach (var item in values) 48 { 49 Console.WriteLine(item); 50 } 51 52 //hincrby 53 Console.WriteLine(string.Format("after Increase user-1's age by 2,the age of user-1 is {0}",db.HashIncrement("user-1","age",2)));

【推荐】国内首个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的设计模式综述