Basic Tutorials of Redis(3) -Hash
2016-09-04 08:53
393 查看
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.
The hmset and hmget can handle multi k/v.
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.
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.
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.
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.
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.
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.
After showing the native commands,we should turn to the usage of StackExchange.Redis.
When you debug the codes,the results are as follow.
The next post of this series is the basic opreation of Set in Redis.
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 catcher hget user-1 name
The hmset and hmget can handle multi k/v.
hmset user-1 age 18 gender male hmget 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-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
After showing the native commands,we should turn to the usage of StackExchange.Redis.
//hset hget hmset hmget db.HashSet("user-1", "name", "catcher"); var user_1 = new HashEntry[2] { new HashEntry("age",18),new HashEntry("gender","male") }; db.HashSet("user-1", user_1); Console.WriteLine("the name of user-1 is {0}",db.HashGet("user-1","name")); var user_1_fileds = new RedisValue[] { "name","age","gender" }; var user_1_values = db.HashGet("user-1", user_1_fileds); foreach (var item in user_1_values) { Console.WriteLine(item); } //hlen Console.WriteLine(string.Format("the number of filed in user-1 is {0}",db.HashLength("user-1"))); //hgetall var all = db.HashGetAll("user-1"); foreach (var item in all) { Console.WriteLine(string.Format("the {0} of user-1 is {1}",item.Name,item.Value)); } //hdel db.HashDelete("user-1", "gender"); var all_after_del = db.HashGetAll("user-1"); foreach (var item in all_after_del) { Console.WriteLine(string.Format("the {0} of user-1 is {1}", item.Name, item.Value)); } //hexists Console.WriteLine(string.Format("gender {0} in the user-1", db.HashExists("user-1", "gender")?"is":"isn't")); Console.WriteLine(string.Format("gender {0} in the user-1", db.HashExists("user-1", "name") ? "is" : "isn't")); //hkeys var keys = db.HashKeys("user-1"); Console.WriteLine("the keys of user-1 are as follow:"); foreach (var item in keys) { Console.WriteLine(item); } //hvals var values = db.HashValues("user-1"); Console.WriteLine("the values of user-1 are as follow:"); foreach (var item in values) { Console.WriteLine(item); } //hincrby 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(5) - Sorted Set
- Basic Tutorials of Redis(7) -Publish and Subscribe
- Basic Tutorials of Redis(4) -Set
- Basic Tutorials of Redis(8) -Transaction
- Basic Tutorials of Redis(9) -First Edition RedisHelper
- Basic Tutorials of Redis(6) - List
- Basic Tutorials of Redis(1) - Install And Configure Redis
- Basic Tutorials of Redis(2) - String
- Redis 哈希(Hash)
- Spring Data Redis(Repositories-Object to Hash Mapping)
- Hashcode Of A String In Java
- redis中的hash实现
- redis for lack of backlog
- GDI+ 使用指南(basic guiding of GDI plus )
- Redis常用命令-Hash
- Redis:WRONGTYPE Operation against a key holding the wrong kind of value
- redis中hash一致性
- The step of installation of JDK and basic of JAVA(Day 01)
- Redis 源码分析(二) 一个 rehash 也不阻塞的哈希表
- Redis--Hash类型命令操作