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. 动手做第一个Chrome插件

    Chrome插件是令人惊讶的简单,一旦你弄懂它的工作和实现原理.它是由一部分HTML,一部分Js,然后混合了一个叫做manifest.json的Json文件组合而成的整体.这意味着你可以使用你最擅长的 ...

  2. VSCode添加Sciter脚本Tiscript高亮支持

    Sciter中的Tiscript脚本不是标准的Javascript,是对Javascript的扩展.所以在常用的编辑器和IDE上对于高亮的支持很不好. 不过在Sciter论坛中找到了在VSCode上的 ...

  3. margin折叠-从子元素margin-top影响父元素引出的问题

    正在做一个手机端电商项目,顶部导航栈的布局是一个div包含一个子div,如果给在正常文档流中的子div一个垂直margin-top,神奇的现象出现了,两父子元素的边距没变,但父div跟着一起往下走了! ...

  4. 【JS基础】循环

    for 循环的语法: for (语句 1; 语句 2; 语句 3) { 被执行的代码块 } 语句 1 在循环(代码块)开始前执行 语句 2 定义运行循环(代码块)的条件 语句 3 在循环(代码块)已被 ...

  5. BPM流程中心解决方案分享

    一.需求分析 在过去办公自动化的浪潮中,很多企业已经实施了OA流程,但随着客户的发展和对流程管理的越来越重视, 客户对流程应用需求越来越深 入,您可能面临以下需求: 1.流程功能不能满足需求,包括流程 ...

  6. android手机登录时遇到“QQ安全登录发现病毒”解决

    android手机作为开源系统非常容易感染病毒,有时候我们会经常遇到手机QQ登录时检测到app被感染,一般情况是由手机感染病毒所引起的,安装腾讯管家后只能检测病毒和卸载感染病毒的软件,不能清除病毒.解 ...

  7. iOS开发--ChildViewController实现订单页的切换

    先不说废话, 上效果图, 代码量也不大, 也不上传github骗星星了, 你们复制粘贴下代码, 就可以轻而易举的弄出一个小demo. 这个代码的实现并不复杂, 甚至于说非常简单, 就是逻辑有点小绕, ...

  8. Oracle补全日志(Supplemental logging)

    Oracle补全日志(Supplemental logging)特性因其作用的不同可分为以下几种:最小(Minimal),支持所有字段(all),支持主键(primary key),支持唯一键(uni ...

  9. .JavaWeb文件上传和FileUpload组件使用

    .JavaWeb文件上传 1.自定义上传 文件上传时的表单设计要符合文件提交的方式: 1.提交方式:post 2.表单中有文件上传的表单项:<input type="file" ...

  10. C#中实现并发的几种方法的性能测试

    C#中实现并发的几种方法的性能测试 0x00 起因 去年写的一个程序因为需要在局域网发送消息支持一些命令和简单数据的传输,所以写了一个C/S的通信模块.当时的做法很简单,服务端等待链接,有用户接入后开 ...