欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot2.0 基础案例(13):基于Cache注解模式,管理Redis缓存

程序员文章站 2022-05-26 14:27:30
一、Cache缓存简介 从Spring3开始定义Cache和CacheManager接口来统一不同的缓存技术; Cache接口为缓存的组件规范定义,包含缓存的各种操作集合; Cache接口下Spring提供了各种缓存的实现; 如RedisCache,EhCacheCache ,ConcurrentM ......
本文源码
github地址:知了一笑
https://github.com/cicadasmile/spring-boot-base

一、cache缓存简介

从spring3开始定义cache和cachemanager接口来统一不同的缓存技术;
cache接口为缓存的组件规范定义,包含缓存的各种操作集合;
cache接口下spring提供了各种缓存的实现;
如rediscache,ehcachecache ,concurrentmapcache等;

二、核心api

1、cache缓存接口
定义缓存操作。实现有:rediscache、ehcachecache、concurrentmapcache等
2、cachemanager
缓存管理器,管理各种缓存(cache)组件
3、@cacheable 主要针对方法配置,能够根据方法的请求参数对其进行缓存

cacheable 执行流程
1)方法运行之前,按照cachenames指定的名字先去查询cache 缓存组件
2)第一次获取缓存如果没有cache组件会自动创建
3)cache中查找缓存的内容,使用一个key,默认就是方法的参数
4)key是按照某种策略生成的;默认是使用keygenerator生成的,这里使用自定义配置
5)没有查到缓存就调用目标方法;
6)将目标方法返回的结果,放进缓存中

cacheable 注解属性
cachenames/value:指定方法返回结果使用的缓存组件的名字,可以指定多个缓存
key:缓存数据使用的key
key/keygenerator:key的生成器,可以自定义
cachemanager:指定缓存管理器
cacheresolver:指定缓存解析器
condition:指定符合条件的数据才缓存
unless:否定缓存;当unless指定的条件为true,方法的返回值就不会被缓存
sync:是否使用异步模式

4、@cacheevict
清除缓存

cacheevict:缓存清除
key:指定要清除的数据
allentries = true:指定清除这个缓存中所有的数据
beforeinvocation = false:方法之前执行清除缓存,出现异常不执行
beforeinvocation = true:代表清除缓存操作是在方法运行之前执行,无论方法是否出现异常,缓存都清除

5、@cacheput
保证方法被调用,又希望结果被缓存。
与@cacheable区别在于是否每次都调用方法,常用于更新,写入

cacheput:执行方法且缓存方法执行的结果
修改了数据库的某个数据,同时更新缓存;
执行流程
 1)先调用目标方法
 2)然后将目标方法的结果缓存起来

6、@enablecaching
开启基于注解的缓存
7、keygenerator
缓存数据时key生成策略
8、@cacheconfig
统一配置本类的缓存注解的属性

三、与springboot2.0整合

1、核心依赖

<dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-cache</artifactid>
</dependency>
<dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-data-redis</artifactid>
</dependency>

2、cache缓存配置

import org.springframework.cache.interceptor.keygenerator;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import java.lang.reflect.method;
@configuration
public class cacheconfig {
    /**
     * 自定义 cache 的 key 生成器
     */
    @bean("onekeygenerator")
    public keygenerator getkeygenerator (){
        return new keygenerator() {
            @override
            public object generate(object obj, method method, object... objects) {
                return "keygenerator:"+method.getname();
            }
        } ;
    }
}

3、启动类注解开启cache

@enablecaching            // 开启cache 缓存注解
@springbootapplication
public class cacheapplication {
    public static void main(string[] args) {
        springapplication.run(cacheapplication.class,args) ;
    }
}

4、cache注解使用代码

1)封装增删改查接口

import com.boot.cache.entity.user;
public interface userservice {
    // 增、改、查、删
    user adduser (user user) ;
    user updateuser (integer id) ;
    user selectuser (integer id) ;
    void deleteuser (integer id);
}

2)cache注解使用案例

import com.boot.cache.entity.user;
import com.boot.cache.service.userservice;
import org.springframework.cache.annotation.cacheevict;
import org.springframework.cache.annotation.cacheput;
import org.springframework.cache.annotation.cacheable;
import org.springframework.stereotype.service;
@service
public class userserviceimpl implements userservice {
    // 使用自定义的key生成策略
    // 缓存结果key:adduser::keygenerator:adduser
    @cacheput(value = "adduser",keygenerator="onekeygenerator")
    @override
    public user adduser(user user) {
        return user ;
    }
    // 缓存结果key:updateuser::2
    @cacheput(value = "updateuser",key = "#result.id")
    @override
    public user updateuser(integer id) {
        user user = new user() ;
        user.setid(id);
        user.setname("smile");
        return user;
    }
    // 缓存结果key: selectuser::3
    @cacheable(cachenames = "selectuser",key = "#id")
    @override
    public user selectuser(integer id) {
        user user = new user() ;
        user.setid(id);
        user.setname("cicadasmile");
        return user;
    }
    // 删除指定key: selectuser::3
    @cacheevict(value = "selectuser",key = "#id",beforeinvocation = true)
    @override
    public void deleteuser(integer id) {

    }
}

5、测试代码块

@runwith(springjunit4classrunner.class)
@springboottest(classes = cacheapplication.class)
public class cachetest {
    @resource
    private userservice userservice ;
    // 分别测试:增、改、查、删,四个方法
    @test
    public void testadd (){
        user user = new user() ;
        user.setid(1);
        user.setname("cicada");
        userservice.adduser(user) ;
    }
    @test
    public void testupdate (){
        userservice.updateuser(2) ;
    }
    @test
    public void testselect (){
        userservice.selectuser(3) ;
    }
    @test
    public void testdelete (){
        userservice.deleteuser(3) ;
    }
}

四、源代码地址

github地址:知了一笑
https://github.com/cicadasmile/spring-boot-base
码云地址:知了一笑
https://gitee.com/cicadasmile/spring-boot-base

SpringBoot2.0 基础案例(13):基于Cache注解模式,管理Redis缓存
SpringBoot2.0 基础案例(13):基于Cache注解模式,管理Redis缓存