• Springboot 引入 Redis 并配置序列化和封装RedisTemplate



    前言

    为什么要配置序列化:如果不配置序列化的话,我们在redis数据库中存储的数据可能以乱码形式显示出来,不方便我们判断数据存储的正确性,说白了就是序列化以后存进去的是什么,查询出来的就是什么,否则我们的键值都会变成一串看不懂的乱码。
    在这里插入图片描述

    为什么要封装RedisTemplate,因为如果不进行封装的话,大家请看,是不是有黄色的警告信息,看着起来很不舒服,RedisTemplate后面的尖括号可以填泛型,填写以后警告就消失了,但我们的类型很多,每次只能Autowired一个RedisTemplate,所以不能写尖括号内的类型,同时封装也能按照自己的习惯自定义方法,更好用。
    在这里插入图片描述


    一、引入依赖

    只需要引入这一个就可以了,现在的版本里面包含了 lettuce ,所以不需要再额外引入 common-pool2

    <dependency>
       <groupId>org.springframework.bootgroupId>
       <artifactId>spring-boot-starter-data-redisartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4

    二、配置yml

    因为我使用的是Springboot 3.0,所以配置层级是 spring-data-redis,如果你们的配置文件爆红,说明你们是旧版,那么把data去掉就可以了,直接是 spring-redis

    server:
      port: 8081
    spring:
      data:
        redis:
          #数据库索引
          database: 0
          #redis 服务器地址
          host: 127.0.0.1
          #redis 端口
          port: 6379
          #redis 密码 默认为空
          password:
          # 链接超时时间
          connect-timeout: 10s
          #lettuce连接池配置
          lettuce:
            pool:
              # 链接池中最小的空闲链接 默认为0
              min-idle: 0
              # 链接池中最大的空闲连接 默认为 8
              max-idle: 8
              #连接池中最大数据库链接数 默认为8
              max-active: 8
              #连接池最大阻塞等待时间 负值表示没有限制
              max-wait: -1ms
    
    
    • 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

    三、RedisConfig 配置序列化

    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
    import org.springframework.boot.SpringBootConfiguration;
    import org.springframework.context.annotation.Bean;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.*;
    
    /**
     * @author 徐一杰
     * @date 2022/3/13
     * @description redis配置
     */
    @SpringBootConfiguration
    public class RedisConfig {
    
        @Bean
        public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
            RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
            redisTemplate.setConnectionFactory(redisConnectionFactory);
            //创建一个json的序列化对象
            GenericJackson2JsonRedisSerializer jackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
            //设置value的序列化方式json
            redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
            //设置key序列化方式String
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            //设置hash key序列化方式String
            redisTemplate.setHashKeySerializer(new StringRedisSerializer());
            //设置hash value序列化json
            redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
            // 设置支持事务
            redisTemplate.setEnableTransactionSupport(true);
            redisTemplate.afterPropertiesSet();
            return redisTemplate;
        }
    
        @Bean
        public RedisSerializer<Object> redisSerializer() {
            //创建JSON序列化器
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
            //必须设置,否则无法将JSON转化为对象,会转化成Map类型
            objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);
            return new GenericJackson2JsonRedisSerializer(objectMapper);
        }
        
    }
    
    
    • 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

    四、封装RedisTemplate

    这里这样封装,使用的时候直接注入RedisUtils,就可以不填写尖括号指定类型了,这样就支持任何类型的存储和读取了,也没有烦人的警告

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.*;
    import org.springframework.stereotype.Component;
    
    import java.util.*;
    import java.util.concurrent.TimeUnit;
    
    /**
     * @author 徐一杰
     * @date 2022/4/3
     * @description 对RedisTemplate进行封装
     */
    @Component
    @SuppressWarnings({"unchecked", "rawtypes"})
    public class RedisUtils {
    
    	@Autowired
        private RedisTemplate redisTemplate;
    
        /**
         * 根据key 获取过期时间
         *
         * @param key 键 不能为null
         * @return 时间(秒) 返回0代表为永久有效,-2代表键不存在
         */
        public <K> long getExpireTime(K key) {
            Long expire = redisTemplate.getExpire(key, TimeUnit.SECONDS);
            if (expire != null) {
                return expire;
            }
            return -2;
        }
    
        /**
         * 指定缓存失效时间
         *
         * @param key        键
         * @param expireTime 时间(秒)
         */
        public <K> void setExpireTime(K key, long expireTime) {
            try {
                if (expireTime > 0) {
                    redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 移除指定 key 的过期时间
         *
         * @param key 键
         */
        public <K> void removeExpireTime(K key) {
            redisTemplate.boundValueOps(key).persist();
        }
    
        /**
         * 获取缓存中所有的键
         *
         * @param key 键
         * @return 缓存中所有的键
         */
        public <K> Set<K> keys(K key) {
            return redisTemplate.keys(key);
        }
    
        /**
         * 判断key是否存在
         *
         * @param key 键
         * @return true 存在 false不存在
         */
        public <K> boolean hasKey(K key) {
            try {
                return Boolean.TRUE.equals(redisTemplate.hasKey(key));
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    
        /**
         * 根据key删除缓
         *
         * @param keys 键
         */
        public <K> void delete(Collection<K> keys) {
            redisTemplate.delete(keys);
        }
    
        /**
         * 设置分布式锁
         *
         * @param key     键,可以用用户主键
         * @param value   值,可以传requestId,可以保证锁不会被其他请求释放,增加可靠性
         * @param expire  锁的时间(秒)
         * @return 设置成功为 true
         */
        public <K, V> Boolean setNx(K key, V value, long expire) {
            return redisTemplate.opsForValue().setIfAbsent(key, value, expire, TimeUnit.SECONDS);
        }
    
        /**
         * 设置分布式锁,有等待时间
         *
         * @param key     键,可以用用户主键
         * @param value   值,可以传requestId,可以保证锁不会被其他请求释放,增加可靠性
         * @param expire  锁的时间(秒)
         * @param timeout 在timeout时间内仍未获取到锁,则获取失败
         * @return 设置成功为 true
         */
        public <K, V> Boolean setNx(K key, V value, long expire, long timeout) {
            long start = System.currentTimeMillis();
            //在一定时间内获取锁,超时则返回错误
            for (; ; ) {
                // 获取到锁,并设置过期时间返回true
                if (Boolean.TRUE.equals(redisTemplate.opsForValue().setIfAbsent(key, value, expire, TimeUnit.SECONDS))) {
                    return true;
                }
                //否则循环等待,在timeout时间内仍未获取到锁,则获取失败
                if (System.currentTimeMillis() - start > timeout) {
                    return false;
                }
            }
        }
    
        /**
         * 释放分布式锁
         * @param key 锁
         * @param value 值,可以传requestId,可以保证锁不会被其他请求释放,增加可靠性
         * @return 成功返回true, 失败返回false
         */
        public <K, V> boolean releaseNx(K key, V value) {
            Object currentValue = redisTemplate.opsForValue().get(key);
            if (String.valueOf(currentValue) != null && value.equals(currentValue)) {
                return Boolean.TRUE.equals(redisTemplate.opsForValue().getOperations().delete(key));
            }
            return false;
        }
    
        /***********      String 类型操作           **************/
        /**
         * 普通缓存放入
         *
         * @param key   键
         * @param value 值
         */
        public <K, V> void set(K key, V value) {
            try {
                redisTemplate.opsForValue().set(key, value);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 普通缓存放入并设置时间
         *
         * @param key   键
         * @param value 值
         * @param time  时间(秒) time要大于0 如果time小于等于0 将设置无限期
         */
        public <K, V> void set(K key, V value, long time) {
            try {
                if (time > 0) {
                    redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
                } else {
                    redisTemplate.opsForValue().set(key, value);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * value增加值
         *
         * @param key    键
         * @param number 增加的值
         * @return 返回增加后的值
         */
        public Long incrBy(String key, long number) {
            return (Long) redisTemplate.execute((RedisCallback<Object>) connection -> connection.incrBy(key.getBytes(), number));
        }
    
        /**
         * value减少值
         *
         * @param key    键
         * @param number 减少的值
         * @return 返回减少后的值
         */
        public Long decrBy(String key, long number) {
            return (Long) redisTemplate.execute((RedisCallback<Object>) connection -> connection.decrBy(key.getBytes(), number));
        }
    
        /**
         * 根据key获取value
         *
         * @param key 键
         * @return 返回值
         */
        public <K, V> V get(K key) {
            BoundValueOperations<K, V> boundValueOperations = redisTemplate.boundValueOps(key);
            return boundValueOperations.get();
        }
    
        /***********      list 类型操作           **************/
    
        /**
         * 将value从右边放入缓存
         *
         * @param key   键
         * @param value 值
         */
        public <K, V> void listRightPush(K key, V value) {
            ListOperations<K, V> listOperations = redisTemplate.opsForList();
            //从队列右插入
            listOperations.rightPush(key, value);
        }
    
        /**
         * 将value从左边放入缓存
         *
         * @param key   键
         * @param value 值
         */
        public <K, V> void listLeftPush(K key, V value) {
            ListOperations<K, V> listOperations = redisTemplate.opsForList();
            //从队列右插入
            listOperations.leftPush(key, value);
        }
    
        /**
         * 将list从右边放入缓存
         *
         * @param key   键
         * @param value 值
         */
        public <K, V> void listRightPushAll(K key, List<V> value) {
            try {
                redisTemplate.opsForList().rightPushAll(key, value);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 将list从左边放入缓存
         *
         * @param key   键
         * @param value 值
         */
        public <K, V> void listLeftPushAll(K key, List<V> value) {
            try {
                redisTemplate.opsForList().leftPushAll(key, value);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 通过索引 获取list中的值
         *
         * @param key   键
         * @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
         * @return 返回列表中的值
         */
        public <K, V> V listGetWithIndex(K key, long index) {
            ListOperations<K, V> listOperations = redisTemplate.opsForList();
            return listOperations.index(key, index);
        }
    
        /**
         * 从list左边弹出一条数据
         *
         * @param key 键
         * @return 队列中的值
         */
        public <K, V> V listLeftPop(K key) {
            ListOperations<K, V> listOperations = redisTemplate.opsForList();
            return listOperations.leftPop(key);
        }
    
        /**
         * 从list左边定时弹出一条
         *
         * @param key     键
         * @param timeout 弹出时间
         * @param unit    时间单位
         * @return 队列中的值
         */
        public <K, V> V listLeftPop(K key, long timeout, TimeUnit unit) {
            ListOperations<K, V> listOperations = redisTemplate.opsForList();
            return listOperations.leftPop(key, timeout, unit);
        }
    
        /**
         * 从list右边弹出一条数据
         *
         * @param key 键
         * @return 队列中的值
         */
        public <K, V> V listRightPop(K key) {
            ListOperations<K, V> listOperations = redisTemplate.opsForList();
            return listOperations.rightPop(key);
        }
    
        /**
         * 从list左边定时弹出
         *
         * @param key     键
         * @param timeout 弹出时间
         * @param unit    时间单位
         * @return 队列中的值
         */
        public <K, V> V listRightPop(K key, long timeout, TimeUnit unit) {
            ListOperations<K, V> listOperations = redisTemplate.opsForList();
            return listOperations.leftPop(key, timeout, unit);
        }
    
        /**
         * 获取list缓存的内容
         *
         * @param key   键
         * @param start 开始下标
         * @param end   结束下标  0 到 -1 代表所有值
         * @return list内容
         */
        public <K, V> List<V> listRange(K key, long start, long end) {
            try {
                ListOperations<K, V> listOperations = redisTemplate.opsForList();
                return listOperations.range(key, start, end);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    
        /**
         * 获取list缓存的长度
         *
         * @param key 键
         * @return list长度
         */
        public <K> long listSize(K key) {
            Long size = redisTemplate.opsForList().size(key);
            return Objects.requireNonNullElse(size, 0).longValue();
        }
    
        /**
         * 根据索引修改list中的某条数据
         *
         * @param key   键
         * @param index 下标
         * @param value 值
         */
        public <K, V> void listSet(K key, long index, V value) {
            try {
                redisTemplate.opsForList().set(key, index, value);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 从lit中移除N个值为value的值
         *
         * @param key   键
         * @param count 移除多少个
         * @param value 值
         * @return 移除的个数
         */
        public <K, V> long listRemove(K key, long count, V value) {
            Long count1 = redisTemplate.opsForList().remove(key, count, value);
            if (count1 != null) {
                return count1;
            }
            return 0;
        }
    
        /***********      hash 类型操作           **************/
    
        /**
         * 根据key和键获取value
         *
         * @param key  键 不能为null
         * @param item 项 不能为null
         * @return 值
         */
        public <K, HK, HV> HV hashGet(K key, String item) {
            HashOperations<K, HK, HV> hashOperations = redisTemplate.opsForHash();
            return hashOperations.get(key, item);
        }
    
        /**
         * 获取key对应的所有键值
         *
         * @param key 键
         * @return 对应的多个键值
         */
        public <K, HK, HV> Map<HK, HV> hashMGet(K key) {
            HashOperations<K, HK, HV> hashOperations = redisTemplate.opsForHash();
            return hashOperations.entries(key);
        }
    
        /**
         * 添加map到hash中
         *
         * @param key 键
         * @param map 对应多个键值
         */
        public <K> void hashMSet(K key, Map<String, Object> map) {
            try {
                redisTemplate.opsForHash().putAll(key, map);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 添加map到hash中,并设置过期时间
         *
         * @param key        键
         * @param map        对应多个键值
         * @param expireTime 时间(秒)
         */
        public <K> void hashMSet(K key, Map<String, Object> map, long expireTime) {
            try {
                redisTemplate.opsForHash().putAll(key, map);
                if (expireTime > 0) {
                    redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 向hash表中放入一个数据
         *
         * @param key   键
         * @param hKey  map 的键
         * @param value 值
         */
        public <K, HK, HV> void hashPut(K key, HK hKey, HV value) {
            try {
                HashOperations<K, HK, HV> hashOperations = redisTemplate.opsForHash();
                hashOperations.put(key, hKey, value);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 向hash表中放入一个数据,并设置过期时间
         *
         * @param key        键
         * @param hKey       map 的键
         * @param value      值
         * @param expireTime 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
         */
        public <K, HK, HV> void hashPut(K key, HK hKey, HV value, long expireTime) {
            try {
                redisTemplate.opsForHash().put(key, hKey, value);
                if (expireTime > 0) {
                    redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 判断hash表中是否有该项的值
         *
         * @param key  键 不能为null
         * @param hKey map 的键 不能为null
         * @return true 存在 false不存在
         */
        public <K, HK, HV> boolean hashHasKey(K key, HK hKey) {
            HashOperations<K, HK, HV> hashOperations = redisTemplate.opsForHash();
            return hashOperations.hasKey(key, hKey);
        }
    
        /**
         * 取出所有 value
         *
         * @param key 键
         * @return map 中所有值
         */
        public <K, HK, HV> List<HV> hashValues(K key) {
            HashOperations<K, HK, HV> hashOperations = redisTemplate.opsForHash();
            return hashOperations.values(key);
        }
    
        /**
         * 取出所有 hKey
         *
         * @param key 键
         * @return map 所有的键
         */
        public <K, HK, HV> Set<HK> hashHKeys(K key) {
            HashOperations<K, HK, HV> hashOperations = redisTemplate.opsForHash();
            return hashOperations.keys(key);
        }
    
        /**
         * 删除hash表中的键值,并返回删除个数
         *
         * @param key      键
         * @param hashKeys 要删除的值的键
         * @return 删除个数
         */
        public <K, HK, HV> Long hashDelete(K key, Object... hashKeys) {
            HashOperations<K, HK, HV> hashOperations = redisTemplate.opsForHash();
            return hashOperations.delete(key, hashKeys);
        }
    
        /***********      set 类型操作           **************/
        /**
         * 将数据放入set缓存
         *
         * @param key    键
         * @param values 值 可以是多个
         */
        public <K, V> void setAdd(K key, V... values) {
            redisTemplate.opsForSet().add(key, values);
        }
    
        /**
         * 将set数据放入缓存,并设置过期时间
         *
         * @param key        键
         * @param expireTime 时间(秒)
         * @param values     值 可以是多个
         */
        public <K, V> void setAdd(K key, long expireTime, V... values) {
            redisTemplate.opsForSet().add(key, values);
            if (expireTime > 0) {
                redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            }
        }
    
        /**
         * 获取set缓存的长度
         *
         * @param key 键
         * @return set缓存的长度
         */
        public <K> long setSize(K key) {
            Long size = redisTemplate.opsForSet().size(key);
            if (size != null) {
                return size;
            }
            return 0;
        }
    
        /**
         * 根据key获取Set中的所有值
         *
         * @param key 键
         * @return Set中的所有值
         */
        public <K, V> Set<V> setValues(K key) {
            SetOperations<K, V> setOperations = redisTemplate.opsForSet();
            return setOperations.members(key);
        }
    
        /**
         * 根据value从一个set中查询,是否存在
         *
         * @param key   键
         * @param value 要查询的值
         * @return true 存在 false不存在
         */
        public <K, V> boolean setHasKey(K key, V value) {
            return Boolean.TRUE.equals(redisTemplate.opsForSet().isMember(key, value));
        }
    
        /**
         * 根据value删除,并返回删除的个数
         *
         * @param key   键
         * @param value 要删除的值
         * @return 删除的个数
         */
        public <K, V> Long setDelete(K key, Object... value) {
            SetOperations<K, V> setOperations = redisTemplate.opsForSet();
            return setOperations.remove(key, value);
        }
    
        /***********      zset 类型操作           **************/
        /**
         * 在 zset中插入一条数据
         *
         * @param key   键
         * @param value 要插入的值
         * @param score 设置分数
         */
        public <K, V> void zSetAdd(K key, V value, long score) {
            ZSetOperations<K, V> zSetOperations = redisTemplate.opsForZSet();
            zSetOperations.add(key, value, score);
        }
    
        /**
         * 得到分数在 score1,score2 之间的值
         *
         * @param key    键
         * @param score1 起始分数
         * @param score2 终止分数
         * @return 范围内所有值
         */
        public <K, V> Set<V> zSetValuesRange(K key, long score1, long score2) {
            ZSetOperations<K, V> zSetOperations = redisTemplate.opsForZSet();
            return zSetOperations.range(key, score1, score2);
        }
    
        /**
         * 根据value删除,并返回删除个数
         *
         * @param key   键
         * @param value 要删除的值,可传入多个
         * @return 删除个数
         */
        public <K, V> Long zSetDeleteByValue(K key, Object... value) {
            ZSetOperations<K, V> zSetOperations = redisTemplate.opsForZSet();
            return zSetOperations.remove(key, value);
        }
    
        /**
         * 根据下标范围删除,并返回删除个数
         *
         * @param key   键
         * @param size1 起始下标
         * @param size2 结束下标
         * @return 删除个数
         */
        public <K, V> Long zSetDeleteRange(K key, long size1, long size2) {
            ZSetOperations<K, V> zSetOperations = redisTemplate.opsForZSet();
            return zSetOperations.removeRange(key, size1, size2);
        }
    
        /**
         * 删除分数区间内元素,并返回删除个数
         *
         * @param key    键
         * @param score1 起始分数
         * @param score2 终止分数
         * @return 删除个数
         */
        public <K, V> Long zSetDeleteByScore(K key, long score1, long score2) {
            ZSetOperations<K, V> zSetOperations = redisTemplate.opsForZSet();
            return zSetOperations.removeRangeByScore(key, score1, score2);
        }
    
    }
    
    
    
    • 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
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434
    • 435
    • 436
    • 437
    • 438
    • 439
    • 440
    • 441
    • 442
    • 443
    • 444
    • 445
    • 446
    • 447
    • 448
    • 449
    • 450
    • 451
    • 452
    • 453
    • 454
    • 455
    • 456
    • 457
    • 458
    • 459
    • 460
    • 461
    • 462
    • 463
    • 464
    • 465
    • 466
    • 467
    • 468
    • 469
    • 470
    • 471
    • 472
    • 473
    • 474
    • 475
    • 476
    • 477
    • 478
    • 479
    • 480
    • 481
    • 482
    • 483
    • 484
    • 485
    • 486
    • 487
    • 488
    • 489
    • 490
    • 491
    • 492
    • 493
    • 494
    • 495
    • 496
    • 497
    • 498
    • 499
    • 500
    • 501
    • 502
    • 503
    • 504
    • 505
    • 506
    • 507
    • 508
    • 509
    • 510
    • 511
    • 512
    • 513
    • 514
    • 515
    • 516
    • 517
    • 518
    • 519
    • 520
    • 521
    • 522
    • 523
    • 524
    • 525
    • 526
    • 527
    • 528
    • 529
    • 530
    • 531
    • 532
    • 533
    • 534
    • 535
    • 536
    • 537
    • 538
    • 539
    • 540
    • 541
    • 542
    • 543
    • 544
    • 545
    • 546
    • 547
    • 548
    • 549
    • 550
    • 551
    • 552
    • 553
    • 554
    • 555
    • 556
    • 557
    • 558
    • 559
    • 560
    • 561
    • 562
    • 563
    • 564
    • 565
    • 566
    • 567
    • 568
    • 569
    • 570
    • 571
    • 572
    • 573
    • 574
    • 575
    • 576
    • 577
    • 578
    • 579
    • 580
    • 581
    • 582
    • 583
    • 584
    • 585
    • 586
    • 587
    • 588
    • 589
    • 590
    • 591
    • 592
    • 593
    • 594
    • 595
    • 596
    • 597
    • 598
    • 599
    • 600
    • 601
    • 602
    • 603
    • 604
    • 605
    • 606
    • 607
    • 608
    • 609
    • 610
    • 611
    • 612
    • 613
    • 614
    • 615
    • 616
    • 617
    • 618
    • 619
    • 620
    • 621
    • 622
    • 623
    • 624
    • 625
    • 626
    • 627
    • 628
    • 629
    • 630
    • 631
    • 632
    • 633
    • 634
    • 635
    • 636
    • 637
    • 638
    • 639
    • 640
    • 641
    • 642
    • 643
    • 644
    • 645
    • 646
    • 647
    • 648
    • 649
    • 650
    • 651
    • 652
    • 653
    • 654
    • 655
    • 656
    • 657
    • 658
    • 659
    • 660
    • 661

    五、controller使用RedisUtil

    package com.xuyijie.redisdemo.controller;
    
    import com.xuyijie.redisdemo.utils.RedisUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    /**
     * @author 徐一杰
     * @date 2022/9/20 10:30
     * @description
     */
    @RestController
    @RequestMapping("/test")
    public class TestController {
    
        @Autowired
        private RedisUtils redisUtils;
    
        @GetMapping("/helloString/{key}")
        public String helloString(@PathVariable String key){
            redisUtils.set("key", key);
            return redisUtils.get("key");
        }
    
        @GetMapping("/helloList")
        public List<String> helloList(){
            //向key中push一个元素“0”
            redisUtils.listLeftPush("key2", "0");
            //把一个列表push进key
            List<String> list = new ArrayList<>(Arrays.asList("1", "2"));
            redisUtils.listLeftPushAll("key2", list);
            //读取存储的列表中的全部元素,0, -1代表从第一个元素到最后一个元素
            return redisUtils.listRange("key2", 0, -1);
        }
    
    }
    
    
    • 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

    六、操作演示

    我们先来演示一下第二个方法,直接浏览器请求,可以看到,存入并查询出了我们存入的数据
    在这里插入图片描述
    redis控制台也能查询出来
    在这里插入图片描述

    为什么先演示第二个方法呢,因为要用第一个向你们展示一个“小错误”,我们输入中文来测试一下
    在这里插入图片描述
    很棒,成功存入并正确读取出来了对不对

    那我们去控制台看一看,注意看,我们的键还是“key”,没有乱码,value的前半部分中文乱码,后面的String是正常显示的,说明这不是序列化的问题
    在这里插入图片描述
    解决方案是,使用 redis-cli --raw 进入控制台,flushall 先清空数据,再次请求接口存储数据,查询出来的中文就出来了,但是我们还是看不懂,这和代码和redis没有关系
    在这里插入图片描述
    原因是电脑的 cmd 字符默认是 GBK,你们bing一下,看看怎么把cmd改为 UTF-8,好像要从注册表里面改,改好就可以正确显示了

    其实代码可以正确读取出来就说明一切正常的了,我们控制台看到乱码不必理会,这里只是提一下


    总结

  • 相关阅读:
    Xshell连接Docker版本CentOS7
    【PCBA方案设计】握力计方案
    每日OJ题_二叉树dfs③_力扣814. 二叉树剪枝
    Redis5种数据类型
    【Python】
    JavaScript函数的使用
    【产品应用】一体化伺服电机在系留无人机中的应用
    如何设置 Git 短命令
    如何与 TENNECO 建立 EDI 连接?
    我才35岁就要面临“人到中年不服老不行”?大龄测试的救赎之路就在其中!
  • 原文地址:https://blog.csdn.net/qq_48922459/article/details/126948455