您的当前位置:首页正文

哈希Hash实战之简介与常用命令介绍

2024-11-25 来源:个人技术集锦

Redis hash是一个string类型的field和value的映射表,hash特别适合用于存储对象

Redis中每个hash可以存储2^32-1键值对(40多亿)

内容:正如JavaSE集合体系中的Map一样,只不过它是Map中嵌套了Map!

hset hsetnx hget hgetall hmset hmget hdel hlen hkeys

 一个大Key,一个小key

命令行

  • hmset myhash 10010 xiaoming 10011 xiaohong 10012 xiaoqiang //批量设置所有的值
  • hgetall myhash//获取所有的元素值
  • hset myhash 10013 dahong//单个值的添加
  • hdel myhash 10013 //删除某个指定的值
  • hexists myhash 10013//判断某个小键是否存在
  • hget myhash 10011//获取指定的值
  • hkeys myhash//获取所有的fields
  • hvals key//获取所有的value值
  • hlen myhash//获取存储的field有多少对
  • hmget myhash 10010 10011 //批量获取指定字段的值

API查询

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

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

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

        Map<String, String> dataMap = Maps.newHashMap();
        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-获取所有元素的value列表:{}",hashOperations.values(key));

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

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

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

 

 

 

 

 

 

 

 

显示全文