首页 > 代码库 > Basic Tutorials of Redis(3) -Hash

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.

  技术分享 
  
  There are 15 commands you can use in Redis,less than the Strings. 

  技术分享

  Before we use the Hash of Redis, we must hava some exists Hashes in the Redis‘s database.So how can we store the

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

catcher.And I use hget to get the value of name.
hset user-1 name catcherhget user-1 name

技术分享

  The hmset and hmget can handle multi k/v.
hmset user-1 age 18 gender malehmget user-1 name age gender

技术分享

  When you want to learn how many fileds in this Hash,you can use the command hlen to get the number of fileds in 

the Hash.And it will return a integer,meaning there are 3 fileds in the user-1.

hlen user-1

技术分享 

  hget and hmget is a little complex when a hash has 100 fileds or much more.To solve this problem,we can use the

hgetall command,this command will return all of the fileds and the values of this key.

hgetall user-1

技术分享

  If there some fileds you don‘t need anymore,you can delete them by hdel command.For an instance,I delete the

gender filed from the user-1.

hdel user-1 gender

技术分享

  Sometimes,we have to judge wheather a filed existses in the key.At this time we can use the hexists to finish the

job.As you can see,I judge wheather gender and name exists in the user-1.

hexists user-1 gender hexists user-1 name

技术分享

  With the Requirement change,some places many only need the fileds of the hash ,the other place only need the

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-1hvals 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

技术分享

  After showing the native commands,we should turn to the usage of StackExchange.Redis.  
 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             //hlen15             Console.WriteLine(string.Format("the number of filed in user-1 is {0}",db.HashLength("user-1")));16 17             //hgetall18             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             //hdel25             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             //hexists33             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             //hkeys37             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             //hvals45             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             //hincrby53             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)));

 

  When you debug the codes,the results are as follow.

技术分享

 

  The next post of this series is the basic opreation of Set in Redis.

 

Basic Tutorials of Redis(3) -Hash