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. 利用Oracle RUEI+EM12c进行应用的“端到端”性能诊断

    概述 我们知道,影响一个B/S应用性能的因素,粗略地说,有以下几个大的环节: 1. 客户端环节 2. 网络环节(可能包括WAN和LAN) 3. 应用及中间层环节 4. 数据库层环节 能够对各个环节的问 ...

  2. Android之常见问题集锦Ⅱ

    Android问题集锦Ⅰ:http://www.cnblogs.com/AndroidJotting/p/4608025.html EditText输入内容改变事件监听 _edit.addTextCh ...

  3. Angularjs参考框架地址

    1.Table(Grid)参考地址 https://github.com/samu/angular-table https://github.com/daniel-nagy/md-data-table ...

  4. iOS UITableView 与 UITableViewController

    很多应用都会在界面中使用某种列表控件:用户可以选中.删除或重新排列列表中的项目.这些控件其实都是UITableView 对象,可以用来显示一组对象,例如,用户地址薄中的一组人名.项目地址. UITab ...

  5. BlockingCollection使用

    BlockingCollection是一个线程安全的生产者-消费者集合. 代码 public class BlockingTest { BlockingCollection<int> bc ...

  6. Sass之坑Compass编译报错

    前段时间在使用Compass时遇到了其为难处理的一个坑,现记录到博客希望能帮助到各位. 一.问题: 利用Koala或者是gulp编译提示如下,截图为koala编译提示错误: 二.解决办法 从问题截图上 ...

  7. Android—关于自定义对话框的工具类

    开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...

  8. NOIP模板整理计划

    先占个坑 [update]noip结束了,弃了 一.图论 1.单源最短路 洛谷P3371 (1)spfa 已加SLF优化 #include <iostream> #include < ...

  9. MyEclipse对Maven的安装

    好记性不如烂笔头,记录一下. 操作系统:windows 7 MyEclipse2015 JDK1.7 maven的下载链接,点这里下载apache-maven-3.0.4-bin.tar.gz. 下载 ...

  10. C#(或者说.NET/Mono)能做的那些事

    不做语言之争,只陈述事实: 1.桌面软件与服务 不仅是在Windows上,有了开源的Mono,在Apple Mac和Linux(如:Ubuntu)上也有C#的施展天地.并且还可以通过mkbundle工 ...