• springboot整合Redis


    目录

    springboot整合Redis

    添加依赖

    在application.properties配置redis




    本文是本人以前笔记,如果说是在掘金上看到的话没错,还是本人程程呀 的个人主页 - 动态 - 掘金目前只有这一个。如果有哪里不对的话欢迎各位大佬指出问题,本人是一个小白。

    上一篇文章链接为:http://t.csdn.cn/njrA2 ,本篇是连接http://t.csdn.cn/njrA2

    如果是springboot的话请看sspringboot创建项目_程程呀是小白的博客-CSDN博客_springboot创建项目 ,这个是从头开始的还没有学完springboot,一起学习呀!!!!

     

     

    springboot整合Redis

    添加依赖

    1. <dependency>
    2. <groupId>org.springframework.bootgroupId>
    3. <artifactId>spring-boot-starter-data-redisartifactId>
    4. dependency>
    5. <dependency>
    6. <groupId>org.apache.commonsgroupId>
    7. <artifactId>commons-pool2artifactId>
    8. dependency>

    在application.properties配置redis

    1. #redis服务器地址
    2. spring.redis.host=192.168.1.6
    3. #redis服务器连接端口
    4. spring.redis.port=6379
    5. #redis数据库索引(默认为0)
    6. spring.redis.database=0
    7. #连接超时时间(毫秒)
    8. spring.redis.timeout=1800000
    9. #连接池最大连接数(用负值表示没有限制)
    10. spring.redis.lettuce.pool.max-idle=20
    11. #最大阻塞等待时间(负数表示没有限制)
    12. spring.redis.jedis.pool.max-idle=5
    13. #连接池中的最小空闲连接
    14. spring.redis.lettuce.pool.min-idle=0

    添加redis配置类

    1. @EnableCaching
    2. @Configuration
    3. public class RedisConfig extends CachingConfigurationSelector {
    4.         @Bean
    5.         public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
    6.             RedisTemplate<String, Object> template = new RedisTemplate<>();
    7.             RedisSerializer<String> redisSerializer = new StringRedisSerializer();
    8.             Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    9.             ObjectMapper om = new ObjectMapper();
    10.             om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    11.             om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    12.             jackson2JsonRedisSerializer.setObjectMapper(om);
    13.             template.setConnectionFactory(factory);
    14.             //key序列化方式
    15.             template.setKeySerializer(redisSerializer);
    16.             //value序列化
    17.             template.setValueSerializer(jackson2JsonRedisSerializer);
    18.             //value hashmap序列化
    19.             template.setHashValueSerializer(jackson2JsonRedisSerializer);
    20.             return template;
    21.         }
    22.         @Bean
    23.         public CacheManager cacheManager(RedisConnectionFactory factory) {
    24.             RedisSerializer<String> redisSerializer = new StringRedisSerializer();
    25.             Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    26.             //解决查询缓存转换异常的问题
    27.             ObjectMapper om = new ObjectMapper();
    28.             om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    29.             om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    30.             jackson2JsonRedisSerializer.setObjectMapper(om);
    31.             // 配置序列化(解决乱码的问题),过期时间600秒
    32.             RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
    33.                     .entryTtl(Duration.ofSeconds(600))
    34.                     .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
    35.                     .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
    36.                     .disableCachingNullValues();
    37.             RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
    38.                     .cacheDefaults(config)
    39.                     .build();
    40.             return cacheManager;
    41.         }
    42.     }

    如果factory一直下面有红线可以:降低Autowired检测的级别,将Severity的级别由之前的error改成warning或其它可以忽略的级别

     

    测试一下

    1. @RestController
    2. @RequestMapping("/redisText")
    3. public class RedisController {
    4.     @Autowired
    5.     private RedisTemplate redisTemplate;
    6.     @GetMapping
    7.     public String testRedis(){
    8.         //设置值到redis
    9.         redisTemplate.opsForValue().set("name","lucy");
    10.         //从redis获取值
    11.         String name=(String) redisTemplate.opsForValue().get("name");
    12.         return name;
    13.     }
    14. }

    正确结果为

    lucy

    如果错误的话很有可能是common-pool2版本问题。改为下图所示应该可以了

    1. <dependency>
    2.     <groupId>org.apache.commonsgroupId>
    3.     <artifactId>commons-pool2artifactId>
    4. dependency>

     

    本文是本人以前笔记,如果说是在掘金上看到的话没错,还是本人程程呀 的个人主页 - 动态 - 掘金目前只有这一个。如果有哪里不对的话欢迎各位大佬指出问题,本人是一个小白。

    上一篇文章链接为:http://t.csdn.cn/njrA2 ,本篇是连接http://t.csdn.cn/njrA2

    如果是springboot的话请看sspringboot创建项目_程程呀是小白的博客-CSDN博客_springboot创建项目 ,这个是从头开始的还没有学完springboot,一起学习呀!!!!

  • 相关阅读:
    小程序直播 + 电商,想做新零售电商就用它吧!
    Ceph Luminous 12 添加/删除/更换 OSD 详解
    【计算机网络笔记四】应用层(一)DNS域名解析
    DBC文件解析成C语言
    计算机网络学习笔记(二)---运输层,应用层
    制作一个ros2机器人需要学习的课本(还不全面)
    TinyXML-2介绍
    系统编程 day11 信号量 (我也不知道是什么东西,不好解释 )(和之前的信号有一些不同 (函数), 但是也有一些操作相同 ,比如说 p v 操作 , )
    mysql数据库报错:1166-Incorrect column name ‘xxx‘
    基于Python的“书怡”在线书店系统的设计与实现毕业设计源码082332
  • 原文地址:https://blog.csdn.net/qq_57785602/article/details/126334354