SpringBoot--配置Redis单机缓存

本是古典 何须时尚 2021-08-24 17:12 488阅读 0赞

Redis单机缓存

如果在classpath下存在Redis并且Redis已经配置好了,此时默认就会使用RedisCacheManager作为缓存提供者。

  1. 创建项目,添加缓存依赖:



    org.springframework.boot
    spring-boot-starter-data-redis


    lettuce-core
    io.lettuce




    redis.clients
    jedis


    org.springframework.boot
    spring-boot-starter-cache


    org.springframework.boot
    spring-boot-starter-web


    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    2.1.1



    mysql
    mysql-connector-java
    runtime


    org.springframework.boot
    spring-boot-starter-test
    test

  2. 缓存配置:

    过期时间

    spring.cache.redis.time-to-live=1800s

    Redis数据库索引(默认为0)

    spring.redis.database=0

    Redis服务器地址

    spring.redis.host=localhost

    Redis服务器连接端口

    spring.redis.port=6379

    Redis服务器连接密码(默认为空)

    连接池最大连接数(使用负值表示没有限制)

    spring.redis.jedis.pool.max-active=8

    连接池最大阻塞等待时间(使用负值表示没有限制)

    spring.redis.jedis.pool.max-wait=-1

    连接池中的最大空闲连接

    spring.redis.jedis.pool.max-idle=8

    连接池中的最小空闲连接

    spring.redis.jedis.pool.min-idle=0

  3. 项目入口开启缓存:

    @EnableCaching
    @MapperScan(“pers.zhang.sb_cache.mapper”)
    @SpringBootApplication
    public class SbCacheApplication {

    1. public static void main(String[] args) {
    2. SpringApplication.run(SbCacheApplication.class, args);
    3. }

    }

  4. Mapper:

    @Mapper
    public interface EmployeeMapper {

    1. @Select("select * from employee where id = #{id}")
    2. Employee getEmpById(Integer id);

    }

  5. Service:

    @Service
    public class EmployeeService {

    1. @Autowired
    2. EmployeeMapper employeeMapper;
    3. @Cacheable(cacheNames = "emp", key = "#id")
    4. public Employee getEmp(Integer id){
    5. System.out.println("查询" + id + "号员工");
    6. Employee emp = employeeMapper.getEmpById(id);
    7. return emp;
    8. }

    }

  6. Controller:

    @RestController
    public class EmployeeController {

    1. @Autowired
    2. EmployeeService employeeService;
    3. @GetMapping("/emp/{id}")
    4. public Employee getEmployee(@PathVariable("id") Integer id){
    5. Employee emp = employeeService.getEmp(id);
    6. return emp;
    7. }

    }

  7. 配置序列化方式为JSON:

    @Configuration
    public class MyRedisConfig {

    1. @Bean
    2. public CacheManager cacheManager(RedisConnectionFactory factory){
    3. RedisSerializer<String> redisSerializer = new StringRedisSerializer();
    4. Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    5. //解决查询缓存异常的问题
    6. ObjectMapper om = new ObjectMapper();
    7. om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    8. om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    9. jackson2JsonRedisSerializer.setObjectMapper(om);
    10. //配置序列化
    11. RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig()
    12. .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
    13. .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
    14. .disableCachingNullValues();
    15. RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
    16. .cacheDefaults(configuration)
    17. .build();
    18. return cacheManager;
    19. }
  1. }
  1. 测试:
    在这里插入图片描述

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,488人围观)

还没有评论,来说两句吧...

相关阅读