写在前面
本文旨在记录如何简单使用springboot的缓存功能。
步骤
1、创建项目,这里使用springbootinitializer快速创建一个普通项目(没有web,没有数据库交互)
2、在启动类上标注注解@EnableCaching
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
//缓存打开注解
@EnableCaching
public class SpringbootwithcacheApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootwithcacheApplication.class, args);
}
}
3、编写业务逻辑
这里简单的模拟查询、更新、删除操作
User:
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
}
UserService(缓存是加在Service层):
import com.springbootstudy.springbootwithcache.bean.User;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.HashMap;
@Service
@CacheConfig(cacheNames = {"userCache"})
public class UserService {
private HashMap map = new HashMap<>();
public void addUser(User user){
System.out.println("添加方法调用-------------------");
map.put(user.getName(),user);
}
@Cacheable(key = "#name")
public User getUserByName(String name){
System.out.println("查询方法调用-------------------");
return map.get(name);
}
@CacheEvict(key = "#name")
public void updateUserByName(String name){
System.out.println("更新方法调用---------------------");
User user = map.get(name);
user.setAge(99);
map.put(name,user);
}
//假设没有使用删除数据之后清缓存的 设置
//@CacheEvict(key = "#name")
public void deleteUserByName(String name){
System.out.println("删除方法调用---------------------");
map.remove(name);
}
}
测试代码:
//创建
userService.addUser(new User("user1", 18));
//第一次查询 缓存中没有
System.out.println(userService.getUserByName("user1"));
//第二次查询同样的对象 缓存中就有了 不会再走一遍查询方法
System.out.println(userService.getUserByName("user1"));
//更新user1 这个方法上面有这个注解@CacheEvict(key = "#name")
//意味着 当更新或者操作了 会删除掉 缓存中以这个key为键的缓存
userService.updateUserByName("user1");
//在查询 就要在走一遍查询方法
System.out.println(userService.getUserByName("user1"));
//删除user1在数据库中的 记录 假设不给缓存做清空
userService.deleteUserByName("user1");
//就会从缓存中拿到一个数据库中不存在的数据
System.out.println(userService.getUserByName("user1"));
测试结果:
添加方法调用-------------------
查询方法调用-------------------
User{name='user1', age=18}
User{name='user1', age=18}
更新方法调用---------------------
查询方法调用-------------------
User{name='user1', age=99}
删除方法调用---------------------
User{name='user1', age=99}
将删除方法上面的@CacheEvict(key = “#name”)打开,删除会同时删除缓存的,避免读取不到新数据,结果:
添加方法调用-------------------
查询方法调用-------------------
User{name='user1', age=18}
User{name='user1', age=18}
更新方法调用---------------------
查询方法调用-------------------
User{name='user1', age=99}
删除方法调用---------------------
查询方法调用-------------------
null
写在后面
还有好几个注解没看怎么用呢,还有怎么配合redis使用也没学到呢



