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. JS继承之原型继承

     许多OO语言都支持两种继承方式:接口继承和实现继承.接口继承只继承方法签名,而实现继承则继承实际的方法.如前所述,由于函数没有签名,在ECMAScript中无法实现接口继承.ECMAScript只支 ...

  2. 脑洞大开之采用HTML5+SignalR2.0(.Net)实现原生Web视频

    目录 对SignalR不了解的人可以直接移步下面的目录 SignalR系列目录 前言 - -,我又来了,今天废话不多说,我们直接来实现Web视频聊天. 采用的技术如下: HTML5 WebRTC Si ...

  3. JDBC简介

    jdbc连接数据库的四个对象 DriverManager  驱动类   DriverManager.registerDriver(new com.mysql.jdbc.Driver());不建议使用 ...

  4. 如何解决流程开发中SheetRadioButtonList页面取值问题

    分享一个常见的取值问题. 应用场景: SheetRadioButtonList控件,点击其中一项执行事件操作.如果是页面加载的情况下,值就无法取到. 具体原因如下: 我给SheetRadioButto ...

  5. iOS 10 跳转系统设置

    苦心人天不负, 为了项目终于把 iOS 10 跳转系统设置的方法给搞定了, 很欣慰. http://www.cnblogs.com/lurenq/p/6189580.html iOS 10 跳转系统设 ...

  6. RunLoop 总结:RunLoop的应用场景(一)

    参考资料 好的书籍都是值得反复看的,那好的文章,好的资料也值得我们反复看.我们在不同的阶段来相同的文章或资料或书籍都能有不同的收获,那它就是好文章,好书籍,好资料.关于iOS 中的RunLoop资料非 ...

  7. WebStorm 2016 最新版激活(activation code方式)

    WebStorm 2016 最新版激活(activation code方式) WebStorm activation code WebStorm 最新版本激活方式: 今天下载最新版本的WebStorm ...

  8. myrocks复制中断问题排查

    背景 mysql可以支持多种不同的存储引擎,innodb由于其高效的读写性能,并且支持事务特性,使得它成为mysql存储引擎的代名词,使用非常广泛.随着SSD逐渐普及,硬件存储成本越来越高,面向写优化 ...

  9. centos6.3与jexus5.4.4配置支持php(wordpress)

    centos6.3与jexus5.4.4配置支持php,并搭建自己的wordpress博客,供那些在Linux平台下想让 php和asp.net一起跑的初学者参考. 1.搭建webserver 首先准 ...

  10. mono3.2.3+Jexus5.5+openSuSE13.1的asp.net

    读书的时候,我似乎有系统地学习过asp.net,但是基本已经还掉了...工作之后有做过一个内部用的网站,但也没有正式使用,的确只能算是个课程设计型的东西,不能做产品.后来工作需求是做Win8下的APP ...