SpringBoot引入redis
1. 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.60</version>
</dependency>
2. 配置yml
spring:
redis:
port: 6379
host: 192.168.2.128
password: 123456
timeout: 20
jedis:
pool:
max-active: 8
min-idle: 0
max-wait: -1
max-idle: 8
3. 在springboot启动类上加上@EnableCaching
注解
@MapperScan("com.cgp.cache.mapper")
@SpringBootApplication
@EnableCaching
public class SpringbootCacheApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootCacheApplication.class, args);
}
}
配置序列化方式(FastJson)
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.cache.CacheManager;
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.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import javax.annotation.Resource;
import java.time.Duration;
@Configuration
@SuppressWarnings("unchecked")
public class RedisConfig {
@Resource
private RedisConnectionFactory redisConnectionFactory;
private StringRedisSerializer stringSerializer = new StringRedisSerializer();
private GenericFastJsonRedisSerializer fastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
@Bean(name = "redisTemplate")
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setValueSerializer(fastJsonRedisSerializer);
redisTemplate.setHashKeySerializer(stringSerializer);
redisTemplate.setHashValueSerializer(fastJsonRedisSerializer);
redisTemplate.setEnableDefaultSerializer(true);
redisTemplate.setDefaultSerializer(fastJsonRedisSerializer);
return redisTemplate;
}
@Bean(name = "cacheManager")
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(fastJsonRedisSerializer))
.entryTtl(Duration.ofDays(1));
return RedisCacheManager.builder(factory)
.cacheDefaults(config)
.build();
}
}
4. 在service层中使用注解
@Service
@CacheConfig(cacheNames = "emp")
public class EmployeeService {
@Resource
private EmployeeMapper employeeMapper;
@Cacheable(cacheNames = "emp")
public Employee getEmp(Integer id) {
System.out.println("查询" + id + "号员工");
return employeeMapper.getEmpById(id);
}
@CachePut(value = "emp", key = "#result.id")
public Employee updEmp(Employee employee) {
System.out.println("updEmp:" + employee);
employeeMapper.updEmp(employee);
return employee;
}
@CacheEvict(value = "emp", key = "#id")
public void delEmp(Integer id) {
System.out.println("delEmp:" + id);
employeeMapper.delEmpById(id);
}
}