欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringCache的基本使用方法

 更新時(shí)間:2024年01月16日 11:02:45   作者:Bunny0212  
Spring?Cache利用了AOP,實(shí)現(xiàn)了基于注解的緩存功能,并且進(jìn)行了合理的抽象,業(yè)務(wù)代碼不用關(guān)心底層是使用了什么緩存框架,只需要簡單地加一個(gè)注解,就能實(shí)現(xiàn)緩存功能了,本文介紹SpringCache的基本使用方法,感興趣的朋友一起看看吧

概述

常見的緩存的框架有Redis、Memcached、Guava、Caffeine等等, 各有各的優(yōu)勢。如果我們的程序想要使用緩存,就要與這些框架耦合。聰明的架構(gòu)師已經(jīng)在利用接口來降低耦合了,利用面向?qū)ο蟮某橄蠛投鄳B(tài)的特性,做到業(yè)務(wù)代碼與具體的框架分離。但我們?nèi)匀恍枰@式地在代碼中去調(diào)用與緩存有關(guān)的接口和方法,在合適的時(shí)候插入數(shù)據(jù)到緩存里,在合適的時(shí)候從緩存中讀取數(shù)據(jù)。

Spring Cache利用了AOP,實(shí)現(xiàn)了基于注解的緩存功能,并且進(jìn)行了合理的抽象,業(yè)務(wù)代碼不用關(guān)心底層是使用了什么緩存框架,只需要簡單地加一個(gè)注解,就能實(shí)現(xiàn)緩存功能了。而且Spring Cache也提供了很多默認(rèn)的配置,用戶可以3秒鐘就使用上一個(gè)很不錯(cuò)的緩存功能。

SpringCache使用

常用注解

注解說明
@EnableCaching開啟緩存注解功能,通常加在啟動(dòng)類上
@Cacheable在方法執(zhí)行前先查詢緩存中是否有數(shù)據(jù),如果有數(shù)據(jù),則直接返回緩存數(shù)據(jù);如果沒有緩存數(shù)據(jù),調(diào)用方法并將方法返回值放到緩存中
@CachePut將方法的返回值放到緩存中
@CacheEvict將一條或多條數(shù)據(jù)從緩存中刪除

引入相關(guān)依賴

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.3</version>
        <relativePath/>
    </parent>
    <groupId>com.itheima</groupId>
    <artifactId>springcache-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.76</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <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>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.7.3</version>
            </plugin>
        </plugins>
    </build>
</project>

使用步驟

配置文件

需要在配置文件中設(shè)置相關(guān)內(nèi)容,Redis地址、MySQL地址等,使用SpringCache可以將返回或者請求內(nèi)容自動(dòng)存入Redis

server:
  port: 8888
spring:
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://XXXX:3305/springcachedemo?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
      username: root
      password: "02120212"
  redis:
    host: XXXX
    port: 6378
    password: "02120212"
    database: 0
logging:
  level:
    com:
      itheima:
        mapper: debug
        service: info
        controller: info

開始使用

1、開啟緩存

需要在啟動(dòng)類上加上@EnableCaching 注解

2、設(shè)置緩存

設(shè)置緩存使用@CachePut 注解。如果需要將請求的內(nèi)容放入緩存可以按照以下步驟實(shí)現(xiàn)。

設(shè)置鍵的格式為參數(shù)::參數(shù),由于在Redis中可以設(shè)置樹型結(jié)構(gòu)。

關(guān)于Redis樹型結(jié)構(gòu)

如果需要設(shè)置Redis樹型結(jié)構(gòu),可以設(shè)置鍵為

abc:bca:cba

這樣設(shè)置在Redis中展現(xiàn)就像是樹型,像是文件夾的目錄結(jié)構(gòu)。

適應(yīng)不同用戶的習(xí)慣,有多種方式獲取參數(shù),這里的cacheNames設(shè)置是

// TODO 2. 設(shè)置緩存數(shù)據(jù)值
@CachePut(cacheNames = "userCache", key = "#user.id")// 格式為 userCache::abc ;key = "#user.id" 中usr與下面user對應(yīng)
@CachePut(cacheNames = "userCache",key = "#result.id")// #result.id 取到返回值id 對象導(dǎo)航
@CachePut(cacheNames = "userCache",key = "#p0.id")// 取到第一個(gè)參數(shù),這里沒有第二個(gè)參數(shù)所以取不到
@CachePut(cacheNames = "userCache",key = "#a0.id")// 取到第一個(gè)參數(shù),這里沒有第二個(gè)參數(shù)所以取不到
@CachePut(cacheNames = "userCache",key = "#root.args[0].id")// 取到第一個(gè)參數(shù),這里沒有第二個(gè)參數(shù)所以取不到
public User save(@RequestBody User user) {
    userMapper.insert(user);
    return user;
}

但是這也設(shè)置在Redis中會(huì)存在有個(gè)“文件夾”沒有名字,因?yàn)榻邮艿膮?shù)是內(nèi)容:內(nèi)容而在這的格式為內(nèi)容::內(nèi)容所以會(huì)有一個(gè)為空

在上面示例中,想獲取傳入的數(shù)值可以使用,如:user,獲取里面的id可以使用user.id以此類推

所以在上面設(shè)置中顯示的格式為userCache::id中的值

傳入的值是單個(gè)

如果傳入的值是一個(gè),也可以按照這個(gè)方式去設(shè)置,效果和原理也是一樣的。

@GetMapping
@Cacheable(cacheNames = "userCache", key = "#id")// TODO 3. 將key設(shè)置為 userCache::id
public User getById(Long id) {
    User user = userMapper.getById(id);
    return user;
}

那么在上面的示例中顯示的格式為userCache::id的值

3、清理緩存

清理緩存使用注解@CacheEvict,使用格式為@CacheEvict(cacheNames = "userCache", key = "#id")

@DeleteMapping("/delAll")
@CacheEvict(cacheNames = "userCache", allEntries = true) // TODO 5. 清理所有數(shù)據(jù)將userCache所有都刪除
public void deleteAll() {
    userMapper.deleteAll();
}

上述清理格式為userCache::id的值如果這個(gè)值存在就會(huì)被清除

4、清理所有數(shù)據(jù)將userCache所有都刪除

如果想將所有的開頭為userCache的緩存都清除可以使用@CacheEvict,和清除單個(gè)緩存一樣。

示例:@CacheEvict(cacheNames = "userCache", allEntries = true)

只需要將參數(shù)改為allEntries并且設(shè)置為true即可

@DeleteMapping("/delAll")
@CacheEvict(cacheNames = "userCache", allEntries = true) // TODO 5. 清理所有數(shù)據(jù)將userCache所有都刪除
public void deleteAll() {
    userMapper.deleteAll();
}

到此這篇關(guān)于SpringCache的基本使用方法的文章就介紹到這了,更多相關(guān)SpringCache使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論