Redis combat (10) -Hash help combat the command line and learn what a hash code form

Overview: The content of this blog series from debug personally involved in the recorded training classes: Cache Redis middleware technology introduction and practical application scenarios (SpringBoot2.x + grab a red envelope system design and actual) , junior partner interested can click on their own Go learn (after all, in the form of video to master the technology will be faster!), article belongs Rubric: caching middleware technology introduction and practical Redis

Abstract: For the hash data type Hash, some small partner in the actual development of the project relative to other Redis data structures, may have not much use. However, from the "Tao" level point of view, this did not affect an objective fact that there is law: that is the Hash itself is also very strong, from the beginning of this article we will describe the type of data - relevant characteristics and the Hash operation on the corresponding API and command-line level!

Content: For data type hash Hash, there may be some small partner in the project with the actual development was not a lot, but more alternatives are presented using the previous chapter: String String, List List, a collection of Set, has ordered set SortedSet other data structure.

However, with less with less return, its role is still very strong, especially when storing "the same kind of object type" data list the Hash better to cash their advantage, in addition to the maximum, intuitive the role is "reduces the number of cache Key", which is mainly due to storage had when the hash underlying data store, as shown below:

We can see from the figure above, the Hash underlying way of storing data with other data structures indeed a bit different, almost all other data structures: Key-Value storage, and Hash is: Key - [Field-Value] of storage , that value other data structures are generally the exact value, while the Hash value is a series of key-value pairs, usually we are called like this stored Hash: big key actual key, a small key to Field , and specific values corresponding to the value of Field .


First, the command line combat the Hash

Next, we first use the command line to get to know the chiefs, as shown below, debug give you small partners set out the common list of commonly used command line:


(1) To a hash table to add the specified Key File-Value pair (added alone Field-Value: HSET key field value; HSETNX key field value):

HMSET classOne 2010 xiaoming 2011 xiaohong 2012 debug 2013 jack

(2) Get all the fields and values ​​in the hash table specified Key:  

HGETALL classOne

返回值:
1) "2010"
2) "xiaoming"
3) "2011"
4) "xiaohong"
5) "2012"
6) "debug"
7) "2013"
8) "jack"

(3) obtain the value stored in the hash table specified field Key:  

HGET classOne 2010 return value: "xiaoming"

(4) deletion of one or more fields Field key corresponding to the hash table:  

HDEL ClassOne 2010

(5) Check the hash table key, the specified field is present:  

HEXISTS classOne 2010

Return Value: (integer) 0

(6) Get all the fields Field hash table specified Key:  

HKEYS ClassOne

return value:
1) "2011"
2) "2012"
3) "2013"

(7) acquires the hash table corresponding to the specified Key values ​​all fields Value:  

whales classOne

return value:
1) "xiaohong"
2) "debug"
3) "jack"

 (8)获取哈希表中指定Key的字段的数量:  

HLEN classOne

返回值:(integer)3

 (9)获取所有给定字段Field的值Value:  

HMGET classOne 2011 2012

返回值:
1) "xiaohong"
2) "debug"

二、Java单元测试-代码的方式实战Hash

同样的道理,我们仍然需要采用代码的形式来实战哈希Hash,将其常见的命令行与实际的代码API方法结合起来,如此才能更好的理解哈希Hash相关命令的使用:

   @Test
    public void method5() {
        log.info("----开始哈希Hash测试");

        final String key = "SpringBootRedis:Hash:Key:v1";
        redisTemplate.delete(key);

        HashOperations<String,String,String> hashOperations=redisTemplate.opsForHash();
        hashOperations.put(key,"10010","zhangsan");
        hashOperations.put(key,"10011","lisi");

        Map<String,String> dataMap= Maps.newHashMap();
        dataMap.put("10012","wangwu");
        dataMap.put("10013","zhaoliu");
        hashOperations.putAll(key,dataMap);

        log.info("---哈希hash-获取列表元素: {} ",hashOperations.entries(key));
        log.info("---哈希hash-获取10012的元素: {} ",hashOperations.get(key,"10012"));
        log.info("---哈希hash-获取所有元素的field列表: {} ",hashOperations.keys(key));

        log.info("---哈希hash-10013成员是否存在: {} ",hashOperations.hasKey(key,"10013"));
        log.info("---哈希hash-10014成员是否存在: {} ",hashOperations.hasKey(key,"10014"));

        hashOperations.putIfAbsent(key,"10020","sunwukong");
        log.info("---哈希hash-获取列表元素: {} ",hashOperations.entries(key));

        log.info("---哈希hash-删除元素10010 10011: {} ",hashOperations.delete(key,"10010","10011"));
        log.info("---哈希hash-获取列表元素: {} ",hashOperations.entries(key));

        log.info("---哈希hash-获取列表元素个数: {} ",hashOperations.size(key));
    }

其运行结果如下图所示:


好了,本篇文章我们就介绍到这里了,建议各位小伙伴一定要照着文章提供的样例代码撸一撸,只有撸过才能知道这玩意是咋用的,否则就成了“空谈者”!对Redis相关技术栈以及实际应用场景实战感兴趣的小伙伴可以咱们51cto学院 debug亲自录制的课程进行学习:缓存中间件Redis技术入门与应用场景实战(SpringBoot2.x + 抢红包系统设计与实战)

补充:

1、本文涉及到的相关的源代码可以到此地址,check出来进行查看学习:https://gitee.com/steadyjack/SpringBootRedis

2、目前debug已将本文所涉及的内容整理录制成视频教程,感兴趣的小伙伴可以前往观看学习:https://edu.51cto.com/course/20384.html

Guess you like

Origin blog.51cto.com/13877966/2471732