• SpringBoot项目Redis使用


    SpringBoot项目Redis使用

    引入依赖

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

    对Redis的信息进行配置

      redis:
        database: 0 # 使用Redis的哪一个数据库,0-15 任选
        port: 6379 # 自己的Redis配置的端口
        host: localhost # 自己Redis的网络自己,我这里是本地
        password: xxxxxxx # 自己Redis配置的密码
        lettuce: # 关于lettuce这个默认也行
          pool:
            min-idle: 1
            max-active: 10
            max-idle: 10
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    为方便使用设置序列化器,使用时统一用RedisTemplate模式

    这里就是注册一个Redis序列化的Bean方便使用

    @Configuration
    public class RedisConfig {
        @Bean
        public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
            // 我们为了自己开发方便,一般直接使用 
            RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
            template.setConnectionFactory(redisConnectionFactory);
            // Json序列化配置
            Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
            ObjectMapper om = new ObjectMapper();
            om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
            om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,ObjectMapper.DefaultTyping.NON_FINAL);
            jackson2JsonRedisSerializer.setObjectMapper(om);
            // String 的序列化
            StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
            // key采用String的序列化方式
            template.setKeySerializer(stringRedisSerializer);
            // hash的key也采用String的序列化方式
            template.setHashKeySerializer(stringRedisSerializer);
            // value序列化方式采用jackson
            template.setValueSerializer(jackson2JsonRedisSerializer);
            // hash的value序列化方式采用jackson
            template.setHashValueSerializer(jackson2JsonRedisSerializer);
            template.afterPropertiesSet();
            return template;
        }
    }
    
    • 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

    到这里基本就可以使用了

    使用

    我这里进行测试使用

    使用key:string-value:string模式

    首先直接使用一般的设置Value,这里的代码就是设置一对键值

    @SpringBootTest
    class SpringbootTemplateApplicationTests {
    
    	@Resource
    	RedisTemplate<String,Object> redisTemplate ;
    	@Test
    	void testRedis(){
    		String phone = "12345678901";
    		redisTemplate.opsForValue().set("test:code:"+ phone,"123456");
     		System.out.println(redisTemplate.opsForValue().get("test:code:"+phone));
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    结果:

    在这里插入图片描述

    用图形化工具查看

    符合预期的结果
    在这里插入图片描述

    使用Hash模式

    使用代码:

    @Test
    	void testRedisHashValue(){
    		String token = UUID.randomUUID().toString();
    		User user = new User(1235432l,"xwhking","password","xiaoxiaowei");
    		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(user);
    		redisTemplate.opsForHash().putAll("test:user:"+token,stringObjectMap);
    		System.out.println(redisTemplate.opsForHash().entries("test:user:"+token));
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    打印结果:

    在这里插入图片描述

    图形化结果查看:

    在这里插入图片描述

    直接存一个一个对象呢?会转化为JSON进行存储

    只不过对这里取出来的值进行使用的时候要进行强制类型转换
    使用代码:

    void testRedisJson(){
    		String token = UUID.randomUUID().toString();
    		User user = new User(1235432l,"xwhking","password","xiaoxiaowei");
    		redisTemplate.opsForValue().set("test:user:"+token,user);
    		System.out.println(redisTemplate.opsForValue().get("test:user:"+token));  
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    运行结果:

    在这里插入图片描述

    图形化结果:

    在这里插入图片描述

  • 相关阅读:
    3.自定义工程目录配置CMakeLists
    黑马程序员RabbitMQ入门到实战教程【高级篇】学习笔记
    深度理解 RGMII (包含Linux和realtek)
    显示控件——半圆进度条
    CompletionService使用及原理(源码分析)
    Hyperf微服务——五、JsonRpc远程调用
    游戏公会在去中心化游戏中的未来
    9.2CubeMx配置SD卡FATFS系统_stm32H7系列 SD卡 FR_NO_FILESYSTEM 找不到FatFs系统的问题
    基于Python+Pygame实现一个俄罗斯方块小游戏【完整代码】
    2022BATJ1000 道 Java 面试题解析,已有 372 人上岸(必看攻略)
  • 原文地址:https://blog.csdn.net/Go_ahead_forever/article/details/133144686