On Redis basic type Hash

Keywords: Redis MySQL Docker JSON

Redis has five basic types: string, list, Hash, set and sorted set. This time, the common operations of Hash are listed.

Redis official website: https://redis.io/ 

 

1, Hash introduction

Hash is the basic type in Redis. A key corresponds to a set, in which a dictionary is formed in the form of field value. It can be understood that in a dictionary, the single value part is another dictionary. Each hash can store 2 ^ 32-1 key value pairs. The data is serialized into json format as {key: {field1: value1, field2: Value2... Fieldn: valuen}}.

  • Internal storage with zipmap structure to save space
  • It is convenient to update. You only need to update the value corresponding to the field.

  

 

2, Hash common Api

  • Set field value

  

  eg:

  

  • Set the first added field value

  

  • Get value

   

  eg:

   

  • Batch settings

  

  eg:

  

  • Batch get value

   

  eg:

  

  • Get the set information under key

  

  eg:

   

  • Get all field s under key

  

   eg:

  

  • Get all value s under key

  

  eg:

   

  • Delete field under key

  

   eg:

   

 

3, Hash is not commonly used Api

  • Query whether the field under the key exists

   

   eg:

  

  • field mapping value under increasing key (△ n)

  

   eg:

   

  • field mapping value under increasing key (△ n floating-point number)

  

  eg:

  

  • Get the number of field s under key

    

   eg:

  

  • Get the length of field mapping value under key

  

  eg:

   

  • Key value pairs in an iterative hash table

  

eg: the number is limited, and the return cursor is still 0.

   

 

4, Hash simple application scenario

Simulate the Hash storage of article profile information and get it quickly. The scenario is as follows: the homepage of the website displays the article profile information page by page, which is defined as the hot area article. If an article changes content, first determine whether the article id belongs to the hot area, and if so, change the corresponding value of field in Hash.

1. Simulate article data and set up a bunch of seeds

var blogOutlineInfoList = new List<BlogOutlineInfo>()
{
    new BlogOutlineInfo()
    {
        Id = "9527",
        Title = "CSharp",
        Author = "smiling assassin ",
        CreateTime = DateTime.Now,
        Content = "CSharp From introduction to immortality",
        CommentCount =0,
        ReadCount = 0,
        RecommendCount = 0
    },
    new BlogOutlineInfo()
    {
        Id = "9528",
        Title = "Mysql",
        Author = "smiling assassin ",
        CreateTime = DateTime.Now,
        Content = "Mysql From entry to reclusion",
        CommentCount =0,
        ReadCount = 0,
        RecommendCount = 0
    },
    new BlogOutlineInfo()
    {
        Id = "9529",
        Title = "Docker",
        Author = "smiling assassin ",
        CreateTime = DateTime.Now,
        Content = "Docker From entry to transfer",
        CommentCount =0,
        ReadCount = 0,
        RecommendCount = 0
    },
    ...
};

2. Add seed data to Redis (CACHE preheating)

foreach (var blogOutlineInfo in blogOutlineInfoList)
{
    //set up Redis_key
    var blogOutlineInfoKey = $"blogOutlineInfo_{blogOutlineInfo.Id}";

    //Initialize property value
    service.HashSet(blogOutlineInfoKey, nameof(BlogOutlineInfo.Title), blogOutlineInfo.Title);
    service.HashSet(blogOutlineInfoKey, nameof(BlogOutlineInfo.Content), blogOutlineInfo.Content);
    service.HashSet(blogOutlineInfoKey, nameof(BlogOutlineInfo.Author), blogOutlineInfo.Author);
    service.HashSet(blogOutlineInfoKey, nameof(BlogOutlineInfo.CreateTime), blogOutlineInfo.CreateTime);
    service.HashSet(blogOutlineInfoKey, nameof(BlogOutlineInfo.CommentCount), blogOutlineInfo.CommentCount);
    service.HashSet(blogOutlineInfoKey, nameof(BlogOutlineInfo.ReadCount), blogOutlineInfo.ReadCount);
    service.HashSet(blogOutlineInfoKey, nameof(BlogOutlineInfo.RecommendCount), blogOutlineInfo.RecommendCount);
}

3. For the data needed for a page in the website, according to the current page, first obtain the related article Id from the Redis List, and then obtain the article summary information from Hash.

 

4. Simulate an article to increase recommendations, comments, or visits.

#region Increase number of recommendations
service.HashIncrement("blogOutlineInfo_9527", "RecommendCount", 1);
#endregion

#region Change introduction content
service.HashSet("blogOutlineInfo_9530", "Content", "k8s From entry to unemployment");
#endregion

#region Increase reading
service.HashIncrement("blogOutlineInfo_9528", "ReadCount", 1);
#endregion

5. Operation effect: the recommended quantity of the first article and the reading quantity of the second article can be changed directly.

 

  

Warehouse address: https://gitee.com/530521314/Partner.TreasureChest.git (RedisOperate folder)

On May 13, 2020, I hope that I can come back to see my own steps after the technology is completed

Posted by bmpco on Wed, 13 May 2020 18:07:39 -0700