Basic Tutorials of Redis -Hash
Posted Catcher Wong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Basic Tutorials of Redis -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.
![](https://image.cha138.com/20210611/f5ea298f614b4557baf0cd2b371a07ee.jpg)
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)));
以上是关于Basic Tutorials of Redis -Hash的主要内容,如果未能解决你的问题,请参考以下文章
Basic Tutorials of Redis -Hash
Basic Tutorials of Redis - Install And Configure Redis
Basic Tutorials of Redis - Sorted Set