目录
一、SpringCache简介
(一)、缓存的产生
1、原因:
在Web项目运营过程中,会发生很多访问数据库的情况,但是在很多的应用场景中通常说获取前后相同或更新频繁的数据,比如访问产品信息数据、网页数据、用户的基本信息数据。
2、结果:
为了减少频繁的访问数据库,提高数据读取的效率,产生了缓存。在Springboot中,使用SpringCache来实现了缓存的作用。
3、内部数据结构:
key:value
(二)、基于注解的缓存
1、主要的缓存注解
@Cacheable、@CachePut、@CacheEvict、@CacheConfig等
2、@Cacheable注解
作用:主要针对方法配置,能够根据方法的请求参数对其结果进行缓存
主要属性:
cacheNames/value:指定缓存组件的名字,将方法的返回结果以数组的格式放在指定缓存中,可以指定多个缓存
key:指定缓存数据的key,默认使用方法参数值,可以使用SpEL表达式。
keyGenerator:指定缓存数据key的生成器,与key属性二选一使用。
CacheManager:指定缓存管理器。
unless:指定在符合某条件下,不进行数据缓存。
示例代码:
通过@Cacheable注解来设置缓存,指定缓存的名称为“user”,使用usermapper对象调用getUserbyid()方法,根据参数id的值来获取对应user对象
/*** 打开缓存,并设置缓存名称为user*/
@Cacheable(cacheNames ={"user"})
public User getuser(int id)
{
System.out.println("查询"+id+"号员工");
User user=usermapper.getUserbyid(id);
return user;
}
3、@CachePut注解
作用:保证方法被调用的同时执行结果被缓存
主要属性:
cacheNames/value:指定缓存组件的名字,将方法的返回结果以数组的格式放在指定缓存中,可以指定多个缓存
key:指定缓存数据的key,默认使用方法参数值,可以使用SpEL表达式。
keyGenerator:指定缓存数据key的生成器,与key属性二选一使用。
CacheManager:指定缓存管理器。
unless:指定在符合某条件下,不进行数据缓存。
示例代码:
注解@CachePut中的参数key表示键的值,当前表示以浏览器中输入的参数id的值为键,使用usermapper对象调用updateUser方法,根据参数user对象的值来对相应的user对象进行修改。
缓存名称为user,更新缓存数据中键为key的值*/
@CachePut(value="user",key="#result.id")
public User updateUser(User user)
{
System.out.println("updateuser:"+user);
usermapper.updateUser(user);
return user;
}
二、使用缓存注解完成用户信息的增删改查。
(一)、项目文件目录
(二)、缓存设置过程-查询:
1、编辑SpringBoot01CacheApplication.java文件
通过@EnableCaching开启基于注解的缓存
@MapperScan("com.mapper")//指定需要遍历的mapper接口所在的包
@SpringBootApplication
@EnableCaching//开启基于注解的缓存
public class SpringBoot01CacheApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBoot01CacheApplication.class, args);
}
}
2、创建实体类
package com.bean;
public class User {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int id;
private String name;
public User()
{ super(); }
public User(int id,String name)
{
super();
this.id=id;
this.name=name;
}
}
3、创建UserMapper.java文件
在mapper文件夹下新建UserMapper.java文件,实现UserMapper接口,该接口负责完成与数据库中信息的交互。
package com.mapper;
import com.bean.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper//设置数据映射,从user表中根据id获得用户信息
public interface UserMapper {
@Select("SELECT * FROM user WHERE id=#{id}")
public User getUserbyid(int id);
}
}
4、创建UserService.java文件
在该文件中使用@Cacheable注解设置缓存名称为user,使用usermapper对象调用UserMapper.java文件中定义的getUserbyid方法,根据参数id的值来获取user对象
@Service
public class UserService {
/*打开缓存,并设置缓存名称为user*/
UserMapper usermapper;//声明一个usermapper对象
@Cacheable(cacheNames ={"user"})//设置缓存名称为user
public User getuser(int id)
{
System.out.println("查询"+id+"号员工");
User user=usermapper.getUserbyid(id);//调用UserMapper接口中的getUserbyid方法
return user;
}
}
5、创建UserController.java文件
新建UserController.java文件,实现Usercontroller类,该类主要是对返回的数据格式进行控制
@RestController//该注解表示以JSON格式返回数据
@ResponseBody
public class Usercontroller {
@Autowired//自动注入UserService类
UserService userservice;
@GetMapping("/user/{id}")//设置GET请求
public User getUser(@PathVariable("id") Integer id)//通过@PathVariable获取id的路径
{
User user=userservice.getuser(id);
return user;
}
}
(三)、结果展示-查询
在浏览器中访问http://localhost:8080/user/1001,获取id为1001的用户信息。当第一次执行时,在控制台的输出中可以看到相关信息。
当第二次执行时,在控制台的输出中可以看到相关信息。