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- name catcher
hget user- name

  The hmset and hmget can handle multi k/v.
hmset user- age  gender male
hmget user- 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-

  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-

  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- 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- gender
hexists user- 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-
hvals 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.  
        //hset hget hmset hmget
db.HashSet("user-1", "name", "catcher");
var user_1 = new HashEntry[] { new HashEntry("age",),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",)));
  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的更多相关文章

  1. Basic Tutorials of Redis(9) -First Edition RedisHelper

    After learning the basic opreation of Redis,we should take some time to summarize the usage. And I w ...

  2. Basic Tutorials of Redis(2) - String

    This post is mainly about how to use the commands to handle the Strings of Redis.And I will show you ...

  3. Basic Tutorials of Redis(8) -Transaction

    Data play an important part in our project,how can we ensure correctness of the data and prevent the ...

  4. Basic Tutorials of Redis(4) -Set

    This post will introduce you to some usages of Set in Redis.The Set is a unordered set,it means that ...

  5. Basic Tutorials of Redis(7) -Publish and Subscribe

    This post is mainly about the publishment and subscription in Redis.I think you may subscribe some o ...

  6. Basic Tutorials of Redis(6) - List

    Redis's List is different from C#'s List,but similar with C#'s LinkedList.Sometimes I confuse with t ...

  7. Basic Tutorials of Redis(5) - Sorted Set

    The last post is mainly about the unsorted set,in this post I will show you the sorted set playing a ...

  8. Basic Tutorials of Redis(1) - Install And Configure Redis

    Nowaday, Redis became more and more popular , many projects use it in the cache module and the store ...

  9. redis的hash操作在集中式session中的应用

    在集群部署时,为了高可用性的目的,往往把session进行共享,共享分为两种:session复制和集中式管理. redis在session集中式管理中可以起到比较大的作用. 制约session集中式共 ...

随机推荐

  1. 客户端的验证插件validator

    简单,智能,令人愉悦的表单验证~~~ 官方文档:http://www.niceue.com/validator/ <!DOCTYPE html> <html> <head ...

  2. 理解 .NET Platform Standard

    相关博文:ASP.NET 5 Target framework dnx451 and dnxcore50 .NET Platform Standard:https://github.com/dotne ...

  3. jquery屏幕滚动计算事件总结

    获取浏览器显示区域(可视区域)的高度 : $(window).height(); 获取浏览器显示区域(可视区域)的宽度 : $(window).width(); 获取页面的文档高度: $(docume ...

  4. python 数据类型 ---文件一

    1.文件的操作流程: 打开(open), 操作(read,write), 关闭(close) 下面分别用三种方式打开文件,r,w,a 模式 . "a"模式将不会覆盖原来的文件内容, ...

  5. HTML5游戏源码 飞翔的字母 可自定义内容

    相信大家都玩过飞翔的小鸟吧,当然,可能已经有很多人因为这个游戏砸了不少手机.吼吼. 废话不多说,回到主题,源码如下. 博客园上传空间大小有限制,没法上传了,需要打包源码的朋友们请留言邮箱地址.当然还有 ...

  6. Configure a bridged network interface for KVM using RHEL 5.4 or later?

    environment Red Hat Enterprise Linux 5.4 or later Red Hat Enterprise Linux 6.0 or later KVM virtual ...

  7. 自定义ConfigSection

      CCustom configuration section with intelisense

  8. 《CPU的工作过程》

    本文转载自inter官方网址:https://software.intel.com/zh-cn/articles/book-Processor-Architecture_CPU_work_proces ...

  9. 端盘子的服务生到月薪一万五的IT精英,你能相信吗

    一直以来,我都觉得自己不是一个有故事的人. 以前的我,是个乖宝宝,对父母言听计从,特别内向,甚至一度感觉到自卑.不上学之后,我干过送货员,去工地除泥搬砖,当过油漆工,去过工厂,还去饭店当过端盘子的服务 ...

  10. 微软将向Linux用户提供SQL Server程序

    微软公司(Microsoft Corp., MSFT)将向Linux操作系统的用户提供旗下一项最赚钱的产品,这是该公司几年前无法想像的举措.这家软件巨头周一表示,将向免费的Linux Server提供 ...