springboot缓存使用

springboot缓存使用

步骤

  1. 导入坐标依赖(pom.xml)
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>

2.在主程序中开启缓存注解

package com.lmh.springboot07;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;

/**
 * 缓存注解的几个用法
 * @Cacheable:主要针对方法配置,能根据方法参数的请求参数对其结果进行缓存
 * @CacheEvict:清空缓存
 * @CachePut:保证方法被调用,有希望结果被缓存
 */
@SpringBootApplication
@EnableCaching//开启注解缓存
public class SpringBoot07Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringBoot07Application.class, args);
    }

}

缓存注解使用

1.@Cacheable使用方法

@Autowired
    private StudentMapper studentMapper;//可自定义mapper,操作数据便于测试

    /**
     * @Cacheable的属性:
     *      cacheName/value:指定缓存组件的名字;
     *      key:缓存数据使用的key;可以用它来指定,用默认方法使用的是参数的值;也可以编写spEL表达式:#id;参数id的值
     *      keyGenerator:key的生成器;可以自己指定key的生成器的组件id
     *          key和keyGenerator二选一
     *      cacheManager:指定缓存管理器,例如指定使用redis的,不同的缓存管理器功能是不一样的,与cacheResolver二选一
     *      condition:指定符合条件的情况才缓存
     *      unless:否定缓存,当unless指定条件为true,方法的返回值就不会被缓存,可以获取到结果进行判断
     *      sync:是否使用异步模式,异步开启事unless就不可以用了
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    @Cacheable(cacheNames = "student",key = "#id",condition = "#id>1",unless = "#id==2")
    public Student findById(Integer id) throws Exception {
        return studentMapper.findById(id);
    }

作用:第一次查询数据,先在缓存中查找又没有cacheNames = “student”,key = “#id”,如果没有,则开启以个缓存,在执行方法,当第二次访问时直接在缓存中取

2.@Cacheput

/**
     * @CachePut,既调用方法,有更新缓存数据
     * 修改某个数据,并同时更新
     * 运行时机是,先调用方法,在将目标方法结果缓存起来
     */
    @Override
    @CachePut(cacheNames = "student",key = "#result.id")
    public Student update(Student student) throws Exception {
         studentMapper.update(student);
         return student;
    }

作用为:一般用于更新数据的操作中使用该缓存注解,当更改一个数据时,改完后的结果存入缓存中,使用其他查询方法,从缓存中取的数据就是更新后的数据,与@Cacheable执行过程不同,@CachePut可以使用result的原理就是先执行方法,在将结果存,@Cacheable不可以使用result

@CacheEvict

/**
     * @CacheEvict,缓存删除
     * key:指定要清除的数据
     * allEntries=true,指定清除这个缓存所有数据
     * beforeInvocation=false:缓存的清除是否在缓存之前执行,默认false,在方法之后再执行的,true为方法之前,可以防止删除异常,缓存还在的情况
     */
    @CacheEvict(cacheNames = "student",key = "#id")
    public void delete(Integer id) throws Exception {
        studentMapper.delete(id);
    }

作用:一般用于删除操作,删除一个数据,也把相应的缓存删除,在此查询是要调用sql语句操作数据库

@Caching

/**
     * @Caching一般用于很复杂的操作,可以将他们组合使用,下面例子没有一一测试
     */
    @Caching(
            cacheable = {
                    @Cacheable(cacheNames = {"student"},key ="#id",condition = "#id>0",unless = "#name=lmh")
            },
            put = {
                    @CachePut(cacheNames = {"student"},key = "#id"),
                    @CachePut(cacheNames = {"hhh"},key = "#id")
            },
            evict = {
                    @CacheEvict(cacheNames = {"hhh"},key = "#id")
            }


    )
    public Student all(Student student){
        //省略过程了,操作数据的操作自定义即可
        return student;
    }

作用:一般用于复杂操作,一个方法改变数据很多时,可使用该注解,将上面三种注解组合使用,以达到目标效果

缓存工具

默认使用springboot指定的可以使用redis,则需要整合redis
相关文章见博客 SpringBoot 2X 整合redis json序列化

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值