• 系统性详解Redis操作Hash类型数据(带源码分析及测试结果)


    1 缘起

    系统讲解Redis的Hash类型CURD,
    帮助学习者系统且准确学习Hash数据操作,
    逐步养成测试的好习惯,
    本文较长,Hash的操作比较多,请耐心看,
    既可以集中时间看,亦可以碎片时间学习。

    特别声明:

    (1)文末附全部测试代码;
    (2)本篇文章将学习使用如下函数(方法):

    序号操作method
    1新增hset,hmset,hsetnx
    2删除hdel
    3修改hset,hmset,hincr,hincrBy,hincrByFloat
    4查询hget,hmget,hgetAll,hkeys,hvals,hexists

    (3) 依赖

    <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>3.5.1</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    (4)关于几个术语:Redis数据类型hash,使用时,有三个概念:key,field,value,
    其中,key为hash的变量名,field即HashMap中的键,value即HashMap中的值,后文,如为特别声明,
    使用键则指HashMap中的键。

    2 Hash类型操作

    Redis中的hash类型数据,对标Java的HashMap,存储键值对,但是Redis中的hash的值只能为字符串类型,rehash于HashMap也不同。
    Redis的hash为了提高性能,使用了渐进式rehash。

    2.1 连接池

    private static JedisPool getJedisPool() {
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            // Jedis池:最大连接数
            jedisPoolConfig.setMaxTotal(1);
            // Jedis池:最大空闲连接数
            jedisPoolConfig.setMaxIdle(10);
            // Jedis池:等待时间
            jedisPoolConfig.setMaxWaitMillis(3000);
            // Jedis池:连接Redis超时时间
            int connectTimeout = 2000;
            String redisHost = "127.0.0.1";
            int redisPort = 6379;
            String redisPassword = "123456";
            int redisDb = 0;
            // 创建连接池
            return new JedisPool(jedisPoolConfig, redisHost, redisPort, connectTimeout, redisPassword, redisDb);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    2.2 插入数据

    插入数据有3种方式:单条插入、多条插入、条件插入。

    2.2.1 单条插入:hset

    第一次插入不存在的数据,成功写入后返回1,如果第二次修改数据,成功修改,返回0,
    所以,不能以hset的返回数据判断是否写入或修改成功,
    每次只能插入一条数据,
    测试代码段如下:

        /**
         * 单条插入数据
         */
        @Test
        public void insertData() {
            try (Jedis jedis = getJedisPool().getResource()) {
                Long res1 = jedis.hset("userScore", "xiaoyi", "19");
                Long res2 = jedis.hset("userScore", "xiaoer", "20");
                Long res3 = jedis.hset("userScore", "xiaosan", "20");
                logger.info(">>>>>>>>Redis插入Hash数据:{}, {}, {}", res1, res2, res3);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis插入Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    测试结果如下图所示。
    在这里插入图片描述

    数据形式如下图所示,查询数据时,根据userScore定位到该Hash,然后取对应键的值。

    在这里插入图片描述
    hset源码如下图所示,由注释可知,hset既会创建新数据(Redis不能存在),又会更新数据(Redis存在旧数据)。
    位置:redis.clients.jedis.Jedis#hset(java.lang.String, java.lang.String, java.lang.String)
    在这里插入图片描述

    2.2.2 批量插入:hmset

    上面hset只能插入单条数据,
    有没有可以一次性插入多条的方法呢?
    有的,hmset即可一次性插入多条,直接将Map插入Redis,
    测试代码段如下:

        /**
         * 批量插入数据
         */
        @Test
        public void insertDataBatch() {
            try (Jedis jedis = getJedisPool().getResource()) {
                Map<String, String> userScore = new HashMap<>();
                userScore.put("xiaosi", "22");
                userScore.put("xiaowu", "20");
                String res1 = jedis.hmset("userScore", userScore);
                logger.info(">>>>>>>>Redis插入Hash数据:{}", res1);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis插入Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    由测试结果可知,成功插入返回:OK。
    在这里插入图片描述

    新增的数据如下图所示:
    在这里插入图片描述

    hmset源码如下图所示,由注释可知,hash中没有数据则新建,hmset有数据,则会替换旧值。
    位置:redis.clients.jedis.Jedis#hmset
    在这里插入图片描述

    2.2.3 插入不存在的数据:hsetnx

    如果只需要在Hash中添加新数据,旧数据保持不变,怎么办?
    hsetnx即满足这个需求,
    只增加不存在的数据,
    测试代码段如下:

        /**
         * 条件插入数据:
         * Redis不存在该属性插入,否则不插入
         */
        @Test
        public void insertDataIfNotExist() {
            try (Jedis jedis = getJedisPool().getResource()) {
                // ("xiaoyi", "19")已存在,不会插入,也不会更新旧数据
                Long res1 = jedis.hsetnx("userScore", "xiaoyi", "10");
                // ("xiaoliu", "10")不存在,会插入
                Long res2 = jedis.hsetnx("userScore", "xiaoliu", "10");
                logger.info(">>>>>>>>Redis插入Hash数据:{}, {}", res1, res2);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis插入Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    测试结果如下图所示,由结果可知,0表示已经存在的数据,1表示新增的数据,
    在hsetnx中,0表示已存在数据,没有更新;1表示:新增数据。
    在这里插入图片描述
    源码如下图所示,由源码可知,hsetnx只变更不存在的数据。
    位置:redis.clients.jedis.Jedis#hsetnx
    在这里插入图片描述

    2.3 删除数据:hdel

    删除数据,对于hash类型,该版本的Redis库提供了一个hdel方法,
    支持删除一条和多条,
    测试代码段如下:

        /**
         * 删除数据
         */
        @Test
        public void deleteData() {
            try (Jedis jedis = getJedisPool().getResource()) {
                Long res1 = jedis.hdel("userScore", "xiaoyi");
                Long res2 = jedis.hdel("userScore", "xiaoer", "xiaosan");
                logger.info(">>>>>>>>Redis删除Hash数据:{}, {}, {}", res1, res2);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis删除Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    测试结果如下图所示,hdel返回的数据是成功删除的数据个数,
    如果返回0,则表示,没有删除数据。
    在这里插入图片描述

    hdel源码如下图所示,由注释可知,时间复杂度为O(1)。
    位置:redis.clients.jedis.Jedis#hdel

    在这里插入图片描述

    2.4 修改数据

    修改数据有3种:直接修改内容、根据整数步长变更纯数字值、根据浮点步长变更纯数字值。

    2.4.1 直接修改:hset

    hset是新增和修改复用的函数(方法),上面新增数据时以讲解,这里不多讲,只给出测试代码段和测试结果。

        /**
         * 编辑数据:直接修改
         */
        @Test
        public void editData() {
            try (Jedis jedis = getJedisPool().getResource()) {
                Long res1 = jedis.hset("userScore", "xiaoyi", "6");
                logger.info(">>>>>>>>Redis修改Hash数据:{}", res1);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis修改Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    测试结果如下图所示。
    在这里插入图片描述

    2.4.2 指定整数步长修改:incrBy

    这个功能很有意思,可以根据整数步长变更数据的值,
    特别说明:正数增加,负数减少。
    测试代码段如下:

        /**
         * 以指定整数步长更新数值型,
         * 正数:增加
         * 负数:减少
         */
        @Test
        public void increaseByData() {
            try (Jedis jedis = getJedisPool().getResource()) {
                Long res1 = jedis.hincrBy("userScore", "xiaoyi", 2);
                Long res2 = jedis.hincrBy("userScore", "xiaoyi", -5);
                logger.info(">>>>>>>>Redis修改Hash数据:{}, {}", res1, res2);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis修改Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    hincrBy源码如下图所示,由注释可知,如果增加的键不存在,则会新建数据,同时支持增加和减少两种功能。
    位置:redis.clients.jedis.Jedis#hincrBy

    在这里插入图片描述

    2.4.3 指定浮点步长修改:incrByFloat

    根据浮点步长变更数据,顾名思义,使用浮点数作为变更的单位。
    特别说明:正数增加,负数减少。
    测试代码段如下:

        /**
         * 以指定浮点数步长更新数值型,
         * 正数:增加
         * 负数:减少
         */
        @Test
        public void increaseByFloatData() {
            try (Jedis jedis = getJedisPool().getResource()) {
                Double res1 = jedis.hincrByFloat("userScore", "xiaoyi", 2.0);
                Double res2 = jedis.hincrByFloat("userScore", "xiaoyi", -2.0);
                logger.info(">>>>>>>>Redis修改Hash数据:{}, {}", res1, res2);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis修改Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    hincrByFloat源码如下,由注释可知,处理浮点数据,不存在数据则新建数据。
    位置:redis.clients.jedis.Jedis#hincrByFloat

    在这里插入图片描述

    2.5 查询数据

    hash数据类型是键值对,因此,数据查询也是五花八门
    如,查询所有数据(键+值)、查询所有键(keys)、查询所有值(values)等。

    2.5.1 查询所有数据:hgetAll

    查询所有数据,如果机器内存够用或者不影响业务,可以直接查询所有数据,
    放入代码中的数据结构Map中做代码缓存,
    测试代码段如下:

        /**
         * 查询所有数据
         */
        @Test
        public void queryDataAll() {
            try (Jedis jedis = getJedisPool().getResource()) {
                Map<String, String> res = jedis.hgetAll("userScore");
                logger.info(">>>>>>>>Redis查询Hash数据:{}", res);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis查询Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    测试结果如下图所示,查询出所有结果。
    在这里插入图片描述
    hgetAll源码如下图所示,由注释可知,查询所有的hash数据。
    位置:redis.clients.jedis.Jedis#hgetAll
    在这里插入图片描述

    2.5.2 查询一条数据:hget

    查询单条数据,使用hget,指定键的值,
    测试代码段如下:

        /**
         * 查询单条数据
         */
        @Test
        public void queryDataOne() {
            try (Jedis jedis = getJedisPool().getResource()) {
                String res1 = jedis.hget("userScore", "xiaoyi");
                String res2 = jedis.hget("userScore", "xio");
                logger.info(">>>>>>>>Redis查询Hash数据:{}, {}", res1, res2);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis查询Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    测试结果如下图所示,这里提一下,如果查询的键不存在,则返回null。
    在这里插入图片描述
    hget源码如下,由注释可知,对于不存在的键返回null值。
    位置:redis.clients.jedis.Jedis#hget
    在这里插入图片描述

    2.5.3 批量查询:hmget

    有时我们需要查询指定范围内数据,
    数据量过多,无法一次性查询全部数据,
    因此,可以使用批量查询功能:hmget,
    测试代码段如下:

        /**
         * 批量查询数据:指定属性
         */
        @Test
        public void queryDataBatch() {
            try (Jedis jedis = getJedisPool().getResource()) {
                List<String> res1 = jedis.hmget("userScore", "xiaoyi", "xiaoer");
                logger.info(">>>>>>>>Redis查询Hash数据:{}", res1);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis查询Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    测试结果如下图所示,
    在这里插入图片描述
    hmget源码如下图所示,由注释可知,查询过程,遇到不存在的数据,返回null。
    位置:redis.clients.jedis.Jedis#hmget
    在这里插入图片描述

    2.5.4 查询所有键名:hkeys

    对于判断的业务场景,可以只遍历查询是否存在某个键,
    一来,只查键数据量与全部数据(键+值)相比,会减少许多;
    二来,查询速度也会提高;
    所以,有了,只查hash的键:hkeys,
    测试代码段如下:

        /**
         * 查询所有属性(HashMap中的键)
         */
        @Test
        public void queryDataAllKeys() {
            try (Jedis jedis = getJedisPool().getResource()) {
                Set<String> res = jedis.hkeys("userScore");
                logger.info(">>>>>>>>Redis查询Hash数据:{}", res);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis查询Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    测试结果如下图所示,返回键的列表。
    在这里插入图片描述
    hkeys源码如下图所示,由注释可知,返回hash所有的属性名称。
    位置:redis.clients.jedis.Jedis#hkeys
    在这里插入图片描述

    2.5.5 查询所有值:hvals

    前面有了只查询键,那么,如果有需要只查询值的场景,怎么办?
    当然,有方法:hvals,
    提供只查询值的功能,满足只需要根据值来做业务或者相关计算,
    测试代码段如下:

        /**
         * 查询所有值
         */
        @Test
        public void queryDataAllValues() {
            try (Jedis jedis = getJedisPool().getResource()) {
                List<String> res = jedis.hvals("userScore");
                logger.info(">>>>>>>>Redis查询Hash数据:{}", res);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis查询Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    测试结果如下图所示,由结果可知,返回的数据为列表类型。
    在这里插入图片描述
    hvals源码如下图所示,由注释可知,返回hash所有值。
    位置:redis.clients.jedis.Jedis#hvals

    在这里插入图片描述

    2.5.6 查询数据个数:hlen

    有时,我们需要根据插入数据的数量来判断是否将数据全部同步到Redis,
    或者,需要检查hash数据量,
    这里就需要使用hlen,查询当前某个hash的数据量,
    测试代码段如下:

        /**
         * 查询Hash中数据条数
         */
        @Test
        public void queryDataLength() {
            try (Jedis jedis = getJedisPool().getResource()) {
                Long res = jedis.hlen("userScore");
                logger.info(">>>>>>>>Redis查询Hash数据:{}", res);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis查询Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    测试结果如下图所示,由结果可知,当前hash,userScore存储了4条数据。
    在这里插入图片描述
    hlen源码如下图所示,如果查询的hash没有数据或者没有hash则返回0。
    位置:redis.clients.jedis.Jedis#hlen
    在这里插入图片描述

    2.5.7 查询数据是否存在:hexists

    这是一个点查功能,判断指定的某个键是否存在,
    无需使用临时变量存储,
    直接条件判断即可,因为,hexists返回的直接是布尔值,
    测试代码段如下:

        /**
         * 查询某个属性是否存在
         */
        @Test
        public void queryDataExistsOrNot() {
            try (Jedis jedis = getJedisPool().getResource()) {
                Boolean res1 = jedis.hexists("userScore", "xiaoyi");
                Boolean res2 = jedis.hexists("userScore", "xiao");
                logger.info(">>>>>>>>Redis查询Hash数据:{}, {}", res1, res2);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis查询Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    测试结果如下图所示,存在则返回true,不存在则返回false。
    在这里插入图片描述
    hexists源码如下图所示,由注释可知,判断hash键(属性)是否存在。
    位置:redis.clients.jedis.Jedis#hexists
    在这里插入图片描述

    3 小结

    核心:
    (1)增加数据:
    单条新增:直接新增:hset,key不存在则新增,key存在则覆盖旧值;
    单条新增:条件新增:hsetnx,key不存在则新增,key存在不操作;
    批量新增:hmset;
    (2)删除数据:可以单条也可批量删除;
    (3)修改:修改数据使用hset或者hmset,直接覆盖旧值。如果需要增加或减少数据,可以使用纯数字的数据,指定步长加或减,支持整数步长和浮点步长,通过步长的正负进行加减;
    (4)查询数据:单条查询(hget),返回单条数据;多条查询(hmget),返回列表数据,查询所有数据、所有键(属性)、所有值。

    序号操作method
    1新增hset,hmset,hsetnx
    2删除hdel
    3修改hset,hmset,hincr,hincrBy,hincrByFloat
    4查询hget,hmget,hgetAll,hkeys,hvals,hexists

    附件

    完整测试样例

    package database_test.redis_test;
    
    import org.junit.Test;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.JedisPoolConfig;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    /**
     * Hash数据类型操作.
     *
     * @author xindaqi
     * @since 2022-07-03 9:12
     */
    public class HashTest {
    
        private static final Logger logger = LoggerFactory.getLogger(HashTest.class);
    
        private static JedisPool getJedisPool() {
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            // Jedis池:最大连接数
            jedisPoolConfig.setMaxTotal(1);
            // Jedis池:最大空闲连接数
            jedisPoolConfig.setMaxIdle(10);
            // Jedis池:等待时间
            jedisPoolConfig.setMaxWaitMillis(3000);
            // Jedis池:连接Redis超时时间
            int connectTimeout = 2000;
            String redisHost = "127.0.0.1";
            int redisPort = 6379;
            String redisPassword = "123456";
            int redisDb = 0;
            // 创建连接池
            return new JedisPool(jedisPoolConfig, redisHost, redisPort, connectTimeout, redisPassword, redisDb);
        }
    
        /**
         * 单条插入数据
         */
        @Test
        public void insertData() {
            try (Jedis jedis = getJedisPool().getResource()) {
                Long res1 = jedis.hset("userScore", "xiaoyi", "66");
                Long res2 = jedis.hset("userScore", "xiaoer", "20");
                Long res3 = jedis.hset("userScore", "xiaosan", "20");
                logger.info(">>>>>>>>Redis插入Hash数据:{}, {}, {}", res1, res2, res3);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis插入Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
        /**
         * 批量插入数据
         */
        @Test
        public void insertDataBatch() {
            try (Jedis jedis = getJedisPool().getResource()) {
                Map<String, String> userScore = new HashMap<>();
                userScore.put("xiaosi", "22");
                userScore.put("xiaowu", "20");
                String res1 = jedis.hmset("userScore", userScore);
                logger.info(">>>>>>>>Redis插入Hash数据:{}", res1);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis插入Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
        /**
         * 条件插入数据:
         * Redis不存在该属性插入,否则不插入
         */
        @Test
        public void insertDataIfNotExist() {
            try (Jedis jedis = getJedisPool().getResource()) {
                // ("xiaoyi", "19")已存在,不会插入,也不会更新旧数据
                Long res1 = jedis.hsetnx("userScore", "xiaoyi", "10");
                // ("xiaoliu", "10")不存在,会插入
                Long res2 = jedis.hsetnx("userScore", "xiaoliu", "10");
                logger.info(">>>>>>>>Redis插入Hash数据:{}, {}", res1, res2);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis插入Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
        /**
         * 删除数据
         */
        @Test
        public void deleteData() {
            try (Jedis jedis = getJedisPool().getResource()) {
                Long res1 = jedis.hdel("userScore", "xiaoyi");
                Long res2 = jedis.hdel("userScore", "xiaoer", "xiaosan");
                logger.info(">>>>>>>>Redis删除Hash数据:{}, {}, {}", res1, res2);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis删除Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
        /**
         * 编辑数据:直接修改
         */
        @Test
        public void editData() {
            try (Jedis jedis = getJedisPool().getResource()) {
                Long res1 = jedis.hset("userScore", "xiaoyi", "6");
                logger.info(">>>>>>>>Redis修改Hash数据:{}", res1);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis修改Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
        /**
         * 以指定整数步长更新数值型,
         * 正数:增加
         * 负数:减少
         */
        @Test
        public void increaseByData() {
            try (Jedis jedis = getJedisPool().getResource()) {
                Long res1 = jedis.hincrBy("userScore", "xiaoyi", 2);
                Long res2 = jedis.hincrBy("userScore", "xiaoyi", -5);
                logger.info(">>>>>>>>Redis修改Hash数据:{}, {}", res1, res2);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis修改Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
    
        /**
         * 以指定浮点数步长更新数值型,
         * 正数:增加
         * 负数:减少
         */
        @Test
        public void increaseByFloatData() {
            try (Jedis jedis = getJedisPool().getResource()) {
                Double res1 = jedis.hincrByFloat("userScore", "xiaoyi", 2.0);
                Double res2 = jedis.hincrByFloat("userScore", "xiaoyi", -2.0);
                logger.info(">>>>>>>>Redis修改Hash数据:{}, {}", res1, res2);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis修改Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
        /**
         * 查询所有数据
         */
        @Test
        public void queryDataAll() {
            try (Jedis jedis = getJedisPool().getResource()) {
                Map<String, String> res = jedis.hgetAll("userScore");
                logger.info(">>>>>>>>Redis查询Hash数据:{}", res);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis查询Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
        /**
         * 查询单条数据
         */
        @Test
        public void queryDataOne() {
            try (Jedis jedis = getJedisPool().getResource()) {
                String res1 = jedis.hget("userScore", "xiaoyi");
                String res2 = jedis.hget("userScore", "xio");
                logger.info(">>>>>>>>Redis查询Hash数据:{}, {}", res1, res2);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis查询Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
        /**
         * 批量查询数据:指定属性
         */
        @Test
        public void queryDataBatch() {
            try (Jedis jedis = getJedisPool().getResource()) {
                List<String> res1 = jedis.hmget("userScore", "xiaoyi", "xiaoer");
                logger.info(">>>>>>>>Redis查询Hash数据:{}", res1);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis查询Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
    
        /**
         * 查询所有属性(HashMap中的键)
         */
        @Test
        public void queryDataAllKeys() {
            try (Jedis jedis = getJedisPool().getResource()) {
                Set<String> res = jedis.hkeys("userScore");
                logger.info(">>>>>>>>Redis查询Hash数据:{}", res);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis查询Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
        /**
         * 查询所有值
         */
        @Test
        public void queryDataAllValues() {
            try (Jedis jedis = getJedisPool().getResource()) {
                List<String> res = jedis.hvals("userScore");
                logger.info(">>>>>>>>Redis查询Hash数据:{}", res);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis查询Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
        /**
         * 查询Hash中数据条数
         */
        @Test
        public void queryDataLength() {
            try (Jedis jedis = getJedisPool().getResource()) {
                Long res = jedis.hlen("userScore");
                logger.info(">>>>>>>>Redis查询Hash数据:{}", res);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis查询Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    
        /**
         * 查询某个属性是否存在
         */
        @Test
        public void queryDataExistsOrNot() {
            try (Jedis jedis = getJedisPool().getResource()) {
                Boolean res1 = jedis.hexists("userScore", "xiaoyi");
                Boolean res2 = jedis.hexists("userScore", "xiao");
                logger.info(">>>>>>>>Redis查询Hash数据:{}, {}", res1, res2);
            } catch (Exception ex) {
                logger.error(">>>>>>>>Redis查询Hash数据异常:", ex);
                throw new RuntimeException(ex);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
  • 相关阅读:
    python 查找波峰和波谷
    2023最新SSM计算机毕业设计选题大全(附源码+LW)之java客户需求管理系统2v0d6
    2022年PMP项目管理考试敏捷知识点(6)
    程序员专用表情包,记得转发给你的秃头同事
    笔记41:关于CIAC_PNC_4选用控制器的一些感悟
    Vue 路由使用
    导数求函数最大值和最小值习题
    windows python多环境管理工具 pyenv-win安装与使用教程
    一文读懂 Jetpack 组件开源库中 MVVM 框架架构
    DayDayUp:计算机技术与软件专业技术资格证书之《系统集成项目管理工程师》课程讲解之十大知识领域之4核心—项目范围管理
  • 原文地址:https://blog.csdn.net/Xin_101/article/details/125581423