spring boot with cache

简介

spring cache是spring为项目中的缓存提供的一种简便的解决方案。可以通过注解的方式设置要缓存的方法,从而减少缓存代码和业务代码的耦合,并且可以方便得集成第三方缓存技术。

redis高速缓存
redis是用c语言编写的非关系型数据库,它是在内存中的,并且具有并发量非常大,并发速度非常快等优点。

集成实例

1.基本配置
spring cache可以采用redis作为缓存实现。而在spring boot项目中,只需要配置:

spring:
  cache:
    type: redis

在依赖中添加spring cache和redis的依赖

        
            org.springframework.boot
            spring-boot-starter-data-redis
        

        
            org.springframework.boot
            spring-boot-starter-cache
        

然后做一下简单的连接池配置:

spring:
  redis:
    host: 127.0.0.1
    port: 6379
    password: 123456
    database: 0
    pool:
      max-active: 8 #最大连接数
      max-idle: 8 #最大空闲连接数
      min-idle: 0 #最小空闲连接数
      max-wait: -1 #最大等待时间

此时redis和缓存的配置都已经完成。
接下来可以直接使用了。

2.使用
第一步先在springboot的启动类上加上,@EnableCaching注解开启缓存。

@SpringBootApplication
@EnableCaching
public class SpringboottestApplication {
···

接下来就在要缓存的方法上加@Cacheable注解

public interface StudentDao extends JpaRepository{
    @Cacheable("students")
    @Override
    Student findOne(String id);
···

加上这个注解后,每次调用该方法前都会去检查缓存中是否已经有值,如果没有则调用该方法,将参数作为key,方法返回值作为value存入缓存中,如果有,则不再调用此方法,直接返回缓存中的值。

3.测试
编写一个controller来测试一下。

@RestController
@RequestMapping("student")
public class StudentController {
    @Autowired
    StudentDao studentDao;

    @GetMapping("{id}")
    public Object selectOne(@PathVariable("id") String code){
        System.out.println(code);
        return studentDao.findOne(code);
    }

    @PostMapping
    public Object insertOne(Student student){
        studentDao.save(student);
        return "ok";
    }
}

首先使用post请求添加一个学生信息


spring boot with cache_第1张图片
添加.png

然后通过学号查询该学生信息


spring boot with cache_第2张图片
查询.png

控制台:
2014213880
Hibernate: select student0_.code as code1_1_0_, student0_.age as age2_1_0_, student0_.banji as banji3_1_0_, student0_.name as name4_1_0_ from sys_student student0_ where student0_.code=?

第一次查询打印了sql语句,证明是访问了数据库的
再次发出get请求,查询学生信息,浏览器同样得到了返回的学生信息,再看控制台

2014213880

这次没有打印sql语句,证明第二次没有真正的访问数据库,而是从缓存中获取信息。
进一步确认缓存可以打开一个redis的客户端,看看里面存的东西:

输入keys *
找到students有关的内容

spring cache是将要缓存的方法的所有参数拼接为key,然后方法的返回值作为value存在缓存数据库中的。

spring cache注解的使用

@Cacheable
标记方法为被缓存的方法,当通过参数拼接出的key在缓存中找不到对应的value时,会调用该方法,然后将参数以一定规则拼接为key,方法的返回值作为value记录到缓存数据库中。常用于select操作。
主要属性:
** cacheNames(value) **:缓存名,可指定多个。
** key **:缓存的key的取值规则,可通过SpEl表达式自定义,如#code,则表示key取值为传入的code参数。为空则默认将所有参数拼接为一个key值。
** condition **:记录缓存的条件,可通过SpEl表达式自定义,保证表达式返回true或false,返回true时才会记录缓存。

@CacheEvict
标记方法为更新缓存方法,当此方法被调用后,会清空缓存,从而让被@Cacheable标记的方法在调用时重新获取值存入缓存。常用于delete操作。
主要属性:
前三个属性与@Cacheable注解类似,除了condition,这里的condition是返回true时才会清除缓存。
** allEntries **:是否清空所有缓存,默认为false。默认情况是只有key值相同的缓存才会被清除,若指定为true,则清空所有cacheName相同的缓存记录。
** beforeInvocation **:是否在调用方法之前就执行清除缓存操作,默认是false,当调用方法抛出异常时不会清除缓存,如果指定为true就表示无论方法是否执行成功,都一定会清除缓存。

@CachePut
与@Cacheable注解一样都可以使标记方法被缓存,但不同之处在于,@CachePut标记的方法无论是否有缓存记录都会被执行,因此常用于update操作。

@CacheConfig
作用于类上,可以为此类所有的方法上的缓存注解设置默认的属性。
如:

@CacheConfig(cacheNames = "students")

则标记的类的所有方法上的缓存相关注解的cacheNames属性默认都为"students"。

你可能感兴趣的:(spring boot with cache)