微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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 .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-- name

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

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

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

hlen user-

  hget and hmget is a little complex when a hash has 100 fileds or much more.To solve this 

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

hgetall user-

  If there some fileds you don't need anymore,you can delete them by hdel command.For an 

gender filed from the user-1.

hdel user- 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-- name

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

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

   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- age

  After showing the native commands,we should turn to the usage of StackExchange.Redis.
db.HashSet(,, user_1 = HashEntry[] { HashEntry(,), HashEntry(, db.HashSet( Console.WriteLine(,db.HashGet(, user_1_fileds = RedisValue[] { ,, user_1_values = db.HashGet( ( item Console.WriteLine(.Format(,db.HashLength( all = db.HashGetAll( ( item Console.WriteLine(.Format( db.HashDelete(, all_after_del = db.HashGetAll( ( item Console.WriteLine(.Format( Console.WriteLine(.Format(,db.HashExists(,)?: Console.WriteLine(.Format(,) ? : keys = db.HashKeys( Console.WriteLine( ( item values = db.HashValues( Console.WriteLine( ( item Console.WriteLine(.Format(,db.HashIncrement(,)));
  When you debug the codes,the results are as follow.

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐