zoukankan      html  css  js  c++  java
  • 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 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-1 name catcher
    hget user-1 name

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

     

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

      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-1 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-1 gender 
    hexists user-1 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-1
    hvals user-1

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

      After showing the native commands,we should turn to the usage of StackExchange.Redis.  
     1        //hset hget hmset hmget
     2             db.HashSet("user-1", "name", "catcher");
     3             var user_1 = new HashEntry[2] { new HashEntry("age",18),new HashEntry("gender","male") };
     4             db.HashSet("user-1", user_1);
     5 
     6             Console.WriteLine("the name of user-1 is {0}",db.HashGet("user-1","name"));
     7             var user_1_fileds = new RedisValue[] { "name","age","gender" };
     8             var user_1_values = db.HashGet("user-1", user_1_fileds);
     9             foreach (var item in user_1_values)
    10             {
    11                 Console.WriteLine(item);
    12             }
    13 
    14             //hlen
    15             Console.WriteLine(string.Format("the number of filed in user-1 is {0}",db.HashLength("user-1")));
    16 
    17             //hgetall
    18             var all = db.HashGetAll("user-1");
    19             foreach (var item in all)
    20             {
    21                 Console.WriteLine(string.Format("the {0} of user-1 is {1}",item.Name,item.Value));
    22             }
    23 
    24             //hdel
    25             db.HashDelete("user-1", "gender");
    26             var all_after_del = db.HashGetAll("user-1");
    27             foreach (var item in all_after_del)
    28             {
    29                 Console.WriteLine(string.Format("the {0} of user-1 is {1}", item.Name, item.Value));
    30             }
    31 
    32             //hexists
    33             Console.WriteLine(string.Format("gender {0} in the user-1", db.HashExists("user-1", "gender")?"is":"isn't"));
    34             Console.WriteLine(string.Format("gender {0} in the user-1", db.HashExists("user-1", "name") ? "is" : "isn't"));
    35 
    36             //hkeys
    37             var keys = db.HashKeys("user-1");
    38             Console.WriteLine("the keys of user-1 are as follow:");
    39             foreach (var item in keys)
    40             {
    41                 Console.WriteLine(item);
    42             }
    43 
    44             //hvals
    45             var values = db.HashValues("user-1");
    46             Console.WriteLine("the values of user-1 are as follow:");
    47             foreach (var item in values)
    48             {
    49                 Console.WriteLine(item);
    50             }
    51 
    52             //hincrby
    53             Console.WriteLine(string.Format("after Increase user-1's age by 2,the age of user-1 is {0}",db.HashIncrement("user-1","age",2)));
      When you debug the codes,the results are as follow.

      The next post of this series is the basic opreation of Set in Redis.
  • 相关阅读:
    Nodejs下载和第一个Nodejs示例
    永久关闭Win10工具栏的TaskbarSearch控件
    对称加密,非对称加密,散列算法,签名算法
    【转】TTL和RS232之间的详细对比
    zlg核心板linux系统中查看系统内存等使用信息
    Power BI后台自动刷新数据报错 The operation was throttled by Power BI Premium because there were too many datasets being processed concurrently.
    剪切板和上传文件内容获取
    CSS, LESS, SCSS, SASS总结
    文字程序
    electron 打包“ERR_ELECTRON_BUILDER_CANNOT_EXECUTE”
  • 原文地址:https://www.cnblogs.com/catcher1994/p/5838591.html
Copyright © 2011-2022 走看看