缓存介绍-》缓存是每一个系统都应该考虑的功能,它用于加速系统的访问,以及提速系统的性能。比如:
1. 经常访问的高频热点数据:
电商网站的商品信息:每次查询数据库耗时,可以引入缓存。微博阅读量、点赞数、热点话题等
2.临时性的数据:
发送手机验证码,1分钟有效,过期则删除,存数据库负担有点大,这些临时性的数据也 可以放到缓存中, 直接从缓存中存取数据。
Spring从3.1后定义了 org.springframework.cache.CacheManager 和org.springframework.cache.Cache 接口来统一不同的缓存技术;
1.CacheManager 缓存管理器,用于管理各种Cache缓存组件
2.Cache 定义了缓存的各种操作, Spring在Cache接口下提供了各种xxxCache的实现; 比如EhCacheCache,RedisCache, ConcurrentMapCache……
Spring 提供了缓存注解: @EnableCaching、@Cacheable、@CachePut
步骤:
1.pom.xml中引入 缓存 启动器: spring-boot-starter-cache
1.1 application.properties配置
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/jdbc?serverTimezone=GMT%2B8
2.创建 jdbc 数据库,导入 bill.sql 与 实体对象,创建注解版 mapper 、service 与 Controller
实体对象
package com.cc.huancun.entities;
import java.io.Serializable;
import java.util.Date;
/**
* 用户实体类
* @Title: Provider
* @Description: com.mengxuegu.springboot.entities
* @Auther: www.mengxuegu.com
* @Version: 1.0
*/
public class User implements Serializable{
private Integer id;
//用户名
private String username;
//真实姓名
private String realName;
//用户密码
private String password;
//性别:1 女 2 男
private Integer gender;
//生日
private Date birthday;
//1管理员 2经理 3普通用户
private Integer userType;
public User() {
}
public User(String username, Integer gender) {
this.username = username;
this.gender = gender;
}
public Integer getId() {
return id;
}
public User(Integer id, String username, String realName, String password, Integer gender, Integer userType) {
this.id = id;
this.username = username;
this.realName = realName;
this.password = password;
this.gender = gender;
this.birthday = new Date();
this.userType = userType;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Integer getUserType() {
return userType;
}
public void setUserType(Integer userType) {
this.userType = userType;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", realName='" + realName + '\'' +
", password='" + password + '\'' +
", gender=" + gender +
", birthday=" + birthday +
", userType=" + userType +
'}';
}
}
注解版mapper:
package com.cc.huancun.mapper;
import com.cc.huancun.entities.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
/**
* @Auther: 梦学谷
*/
public interface UserMapper {
@Select("select * from user where id = #{id}")
User getUserById(Integer id);
@Update("UPDATE `user` SET `username`=#{username}, `real_name`=#{realName}, `password`=#{password}, `gender`=#{gender}, `user_type`=#{userType} WHERE `id`=#{id}")
int updateUser(User user);
@Insert("INSERT INTO `user` ( `username`, `real_name`, `password`, `gender`, `birthday`, `user_type`) VALUES ( #{username}, #{realName}, #{password}, #{gender}, #{userType})")
int addUser(User user);
@Delete("delete from user where id = #{id}")
int deleteUserById(Integer id);
}
service:
package com.cc.huancun.service;
import com.cc.huancun.entities.User;
import com.cc.huancun.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
UserMapper userMapper;
@Cacheable(cacheNames = "user")
public User getUserById(Integer id){
User user=userMapper.getUserById(id);
return user;
}
}
controller
package com.cc.huancun.controller;
import com.cc.huancun.entities.User;
import com.cc.huancun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
UserService userService;
@GetMapping("/user/{id}")
public User getUser(@PathVariable("id") Integer id){
User user = userService.getUserById(id);
return user;
}
}
3.@EnableCaching:在启动类上,开启基于注解的缓存
4. @Cacheable : 标在方法上,返回的结果会进行缓存(先查缓存中的结果,没有则调用方法并将结果放到缓存中) 属性cacheNames是给缓冲器起一个名字。
package com.cc.huancun;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@EnableCaching
@SpringBootApplication
@MapperScan("com.cc.huancun.mapper")
public class HuancunApplication {
public static void main(String[] args) {
SpringApplication.run(HuancunApplication.class, args);
}
}
效果:
1.没有加上@EnableCaching和@Cacheable-------------每一次调用都要查询一次数据库
2.加上@EnableCaching和@Cacheable-----------只有第一次查询时访问数据库,其他时候不去数据库查询
清空idea的console ,右键clear all即可
测试
查看控制台打印的sql,重复的查询不会出现两次
application中配置
logging.level.com.cc.huancun.mapper:debug