提供專業(yè)網(wǎng)站建設(shè)平臺(tái)收錄是什么意思
學(xué)習(xí)的最大理由是想擺脫平庸,早一天就多一份人生的精彩;遲一天就多一天平庸的困擾。各位小伙伴,如果您:
想系統(tǒng)/深入學(xué)習(xí)某技術(shù)知識(shí)點(diǎn)…
一個(gè)人摸索學(xué)習(xí)很難堅(jiān)持,想組團(tuán)高效學(xué)習(xí)…
想寫博客但無從下手,急需寫作干貨注入能量…
熱愛寫作,愿意讓自己成為更好的人…
文章目錄
- 一、Spring Cache是什么?
- 二、常用注解
- 三、使用步驟
- 1.導(dǎo)入依賴
- 2.@CachePut的使用
- 3.@Cacheable的使用
- 4.@CacheEvict的使用
- 5.@EnableCaching的使用
- 總結(jié)
一、Spring Cache是什么?
Spring Cache是一個(gè)框架,實(shí)現(xiàn)了基于注解的緩存功能,只需要簡單地加一個(gè)注解,就能實(shí)現(xiàn)緩存功能。
Spring Cache提供了一層抽象,底層可以切換不同的緩存實(shí)現(xiàn),例如:
- EHCache
- Caffeine
- Redis
二、常用注解
三、使用步驟
1.導(dǎo)入依賴
Spring Cache緩存框架的maven導(dǎo)入:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId>
</dependency>
Spring Cache緩存中間件的導(dǎo)入:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
總體pom文件如下:
<?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>
2.@CachePut的使用
@CachePut:將方法的返回值放到緩存中
案例代碼:
@PostMapping//@CachePut(cacheNames = "userCache",key = "#user.id")@CachePut(cacheNames = "userCache",key = "#result.id")public User save(@RequestBody User user){userMapper.insert(user);return user;}
其中#這種寫法是spring規(guī)范的。
cacheName:緩存名稱,是個(gè)字符串
key:緩存數(shù)據(jù)
如果使用Spring Cache緩存數(shù)據(jù),key的生成:userCache::緩存數(shù)據(jù)
3.@Cacheable的使用
@Cacheable:在方法執(zhí)行前先查詢緩存中是否有數(shù)據(jù),如果有數(shù)據(jù),則直接返回緩存數(shù)據(jù);如果沒有緩存數(shù)據(jù),調(diào)用方法并將方法返回值放到緩存中
案例代碼:
@GetMapping@Cacheable(cacheNames = "userCache",key = "#id")//key的生成:userCache::10public User getById(Long id){User user = userMapper.getById(id);return user;}
4.@CacheEvict的使用
@CacheEvict:將一條或多條數(shù)據(jù)從緩存中刪除
清理一條數(shù)據(jù)案例代碼:
@DeleteMapping@CacheEvict(cacheNames = "userCache",key="#id")//key的生成:userCache::10public void deleteById(Long id){userMapper.deleteById(id);}
清理多條數(shù)據(jù)(cacheNames定義的名字下的所有數(shù)據(jù)都刪除)案例代碼:
@DeleteMapping("/delAll")@CacheEvict(cacheNames = "userCache",allEntries = true)public void deleteAll(){userMapper.deleteAll();}
5.@EnableCaching的使用
@EnableCaching:開啟緩存注解功能,通常加在啟動(dòng)類上
案例代碼:
@SpringBootApplication
@EnableTransactionManagement //開啟注解方式的事務(wù)管理
@Slf4j
@EnableCaching//開啟緩存注解功能
public class SkyApplication {public static void main(String[] args) {SpringApplication.run(SkyApplication.class, args);log.info("server started");}
}
總結(jié)
以上就是Spring Cache(緩存框架)的相關(guān)知識(shí)點(diǎn),希望對(duì)你有所幫助。
積跬步以至千里,積怠惰以至深淵。時(shí)代在這跟著你一起努力哦!