@EnableTransactionManagement
@Tansaction
locback配置:
logging: pattern: console: info:%level %msg%n level: root: warn com.kaikeba.jspdemo.dao: debug
或者在resources中加入logging.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml"/> <logger name="org.springframework.web" level="INFO"/> </configuration>
spring boot 中使用redis:使用的两类场景,DB中相关表发生更新,redis缓存中存放的相关数据要清除,第二种设置过期时效
高并发下产生穿透(设置默认空值) 和雪崩(人为设置)大堆缓存同时进入 设置时效就可能产生热点缓存(双重检测锁)防止高并发下对数据库产生压力。
https://www.cnblogs.com/fengli9998/p/8630513.html对穿透比较详细描述
必须的步骤
1.pom中加入springboot和redis的整合依赖
2配置文件中配置redis
3.由于要查询的实体类对象缓存到redis,redis要求实体类必须序列化。
rides高可用集群:如何做到高可用哨兵机制
使用注解方式
1.在工程启动类加
@EnableCaching
2.在查询方法前添加@Cacheable 在增删改前加@CacheEvict
3.在主配置中注册缓存空间名
缓存雪崩大量的缓存在同一时间同时到期
@EnableCaching
public class UserServiceImpl implements UserService { @Autowired public UserDao userDao; @Autowired private RedisTemplate<Object, Object> redisTemplate; @CacheEvict(value = "realTimeCache", allEntries = true) @Override public void sava(User employee) { userDao.insertUser(employee); } @Cacheable(value = "realTimeCache", key = "'user_'+#id") @Override public User findOne(int id) { return userDao.findUserById(id); } //使用双重检测锁 @Override public Integer findUserCount() { //获取redis操作对象 BoundValueOperations<Object, Object> ops = redisTemplate.boundValueOps("count"); Object count = ops.get(); //双重检测锁主要 if (count == null) { synchronized (this) { count = ops.get(); if (count == null) { count = userDao.findCount(); DateTimeFormatter s = DateTimeFormatter.ofPattern("yyyy-MM-dd HH-mm-ss"); System.out.println(LocalDateTime.now().format(s)+"d"+userDao.findCount() + "dddddd"); //将查询的数据写入到redis中,并设置到期时效 ops.set(count, 10, TimeUnit.SECONDS); } } } //从缓存中读取数据 return (Integer) count; } }