美文网首页coder
SpringBoot集成redis (数据库缓存)

SpringBoot集成redis (数据库缓存)

作者: S拒绝拖延 | 来源:发表于2020-03-14 22:47 被阅读0次

1.pom.xml 引入maven依赖

<!--redis-->
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
      <groupId>org.springframework.session</groupId>
      <artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-cache</artifactId>
</dependency>

2.application.yml配置

spring:
  redis:
    #数据库索引
    database: 2
    host: 192.168.219.130
    port: 6379
    password: admin123456
    jedis:
      pool:
        max-active: 8
        max-wait: -1ms
        max-idle: 8
        min-idle: 0
    timeout: 300s

3.编写配置类

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.time.Duration;

@Configuration
public class RedisConfig extends CachingConfigurerSupport {
    @Bean
    public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {
        //初始化一个RedisCacheWriter
        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
        //设置CacheManager的值序列化方式为json序列化
        RedisSerializer<Object> jsonSerializer = new GenericJackson2JsonRedisSerializer();
        RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair
                .fromSerializer(jsonSerializer);
        RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig()
                .serializeValuesWith(pair);
        //设置默认超过期时间是30秒
        defaultCacheConfig.entryTtl(Duration.ofSeconds(30));
        //初始化RedisCacheManager
        return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
    }
}

4.启动类开启注解

@EnableCaching

5.编写需要缓存的方法,添加注解

 @Cacheable(value = "student", key = "#root.targetClass", unless = "#result eq null")
    @Override
    public List<StudentEntity> getStudents(String stuno) {
        return studentDao.getStudents(stuno);
    }
@CacheEvict(value = "student", key = "#root.targetClass")
    @Override
    public void addStudent(StudentEntity studentEntity) throws Exception {
        studentDao.addStudent(studentEntity);
    }

相关文章

网友评论

    本文标题:SpringBoot集成redis (数据库缓存)

    本文链接:https://www.haomeiwen.com/subject/fnnoshtx.html