依赖
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 通用池 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
业务使用类
@Service
@Slf4j
public class RedisCsServiceImpl extends ServiceImpl<RedisCsMapper, RedisCs> implements RedisCsService {
@Autowired
private RedisCsMapper redisCsMapper;
@Autowired
private RedisUtils redisUtils;
@Autowired
private RedisTemplate redisTemplate;
@Override
public Object findById(Integer id) {
//创建key的展示形式
String key = "user:" + id;
//先查询Redis缓存
Object userObj = redisTemplate.opsForValue().get(key);
if (userObj == null) {
//多线程同步数据
synchronized (this.getClass()) {
//再查一次缓存,防止查询数据有延迟
userObj = redisTemplate.opsForValue().get(key);
if (userObj==null){
log.debug("====查询数据库====");
//缓存没有数据,查询MySQL数据库里面的数据
RedisCs redisCs = baseMapper.selectById(id);
//帮MySQL查询出来的数据返回的同时,把数据写入到Redis进行缓存
redisTemplate.opsForValue().set(key,redisCs);
return redisCs;
}else {
log.debug("====查询缓存(同步代码块)====");
return userObj;
}
}
}else {
log.debug("====查询缓存====");
}
return userObj;
}
}
配置类1
/**
* 设定redis返回的展示格式
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
//指定key,value的序列化方式
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setKeySerializer(new StringRedisSerializer());
return redisTemplate;
}
}
配置类2
package com.hhmt.flowalliance.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfiguration {
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
template.setKeySerializer(stringRedisSerializer);
template.setHashKeySerializer(stringRedisSerializer);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
redis配置
## Redis 配置
## Redis数据库索引(默认为0)
spring.redis.database=0
## Redis服务器地址
spring.redis.host=127.0.0.1
## Redis服务器连接端口
spring.redis.port=6379
## Redis服务器连接密码(默认为空)
spring.redis.password=
## 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
## 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
## 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
## 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
## 连接超时时间(毫秒)
spring.redis.timeout=0
redis配置2
server:
port: 8989
spring:
datasource:
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
#连接本地数据库
url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
redis:
host: 127.0.0.1
timeout: 1000
jedis:
pool:
min-idle: 5
max-idle: 10
max-wait: -1
mybatis-plus:
# mapper.xml 文件扫描
mapper-locations: classpath*:/mapper/*.xml
configuration:
map-underscore-to-camel-case: true
# 日志配置
logging:
level:
com.xxx.xxx: debug
org.springframework: warn
版权声明:本文为m0_50302965原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。