https://blog.csdn.net/a67474506/article/details/52608855

在不適用任何額外配置的情況下,默認使用SimpleCacheConfiguration
SpringBoot通過spring.cache為前綴來配置緩存

使用這些緩存實現的話,只需導入相關緩存的依賴,並在配置類中使用@EnableCaching開啟緩存即可
Guava實現
這里簡單介紹下使用Guava實現
引入的依賴
pom.xml
-
<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>
-
<groupId>com.ibigsea</groupId>
-
<artifactId>spirngboot-cache-demo</artifactId>
-
<version>0.0.1-SNAPSHOT</version>
-
-
-
<properties>
-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-
<boot.version>1.3.5.RELEASE</boot.version>
-
</properties>
-
-
<dependencies>
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-web</artifactId>
-
<version>${boot.version}</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-test</artifactId>
-
<version>${boot.version}</version>
-
<scope>test</scope>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-cache</artifactId>
-
<version>${boot.version}</version>
-
</dependency>
-
<dependency>
-
<groupId>com.google.guava</groupId>
-
<artifactId>guava</artifactId>
-
<version>19.0</version>
-
</dependency>
-
</dependencies>
-
</project>

dataCache.java
-
package com.ibigsea.springboot_cache_demo.cache;
-
-
import java.text.SimpleDateFormat;
-
import java.util.Date;
-
import java.util.HashMap;
-
import java.util.Map;
-
-
import javax.annotation.PostConstruct;
-
-
import org.springframework.cache.annotation.CacheConfig;
-
import org.springframework.cache.annotation.CacheEvict;
-
import org.springframework.cache.annotation.CachePut;
-
import org.springframework.cache.annotation.Cacheable;
-
import org.springframework.stereotype.Component;
-
-
-
public class DataCache {
-
-
private Map<Long, String> dataMap = new HashMap<>();
-
-
/**
-
* 初始化
-
*/
-
-
public void init() {
-
dataMap.put( 1L, "張三");
-
dataMap.put( 2L, "李四");
-
dataMap.put( 3L, "王五");
-
}
-
-
/**
-
* 查詢
-
* 如果數據沒有緩存,那么從dataMap里面獲取,如果緩存了,
-
* 那么從guavaDemo里面獲取
-
* 並且將緩存的數據存入到 guavaDemo里面
-
* 其中key 為 #id+dataMap
-
*/
-
-
public String query(Long id) {
-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-
System.out.println(sdf.format( new Date()) + " : query id is " + id);
-
return dataMap.get(id);
-
}
-
-
/**
-
* 插入 或者更新
-
* 插入或更新數據到dataMap中
-
* 並且緩存到 guavaDemo中
-
* 如果存在了那么更新緩存中的值
-
* 其中key 為 #id+dataMap
-
*/
-
-
public String put(Long id, String value) {
-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-
System.out.println(sdf.format( new Date()) + " : add data ,id is "+ id);
-
dataMap.put(id, value);
-
// data persistence
-
return value;
-
}
-
-
/**
-
* 刪除
-
* 刪除dataMap里面的數據
-
* 並且刪除緩存guavaDemo中的數據
-
* 其中key 為 #id+dataMap
-
*/
-
-
public void remove(Long id) {
-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-
System.out.println(sdf.format( new Date()) + " : remove id is "+ id + " data");
-
dataMap.remove(id);
-
// data remove
-
}
-
-
-
}
關於緩存注解中的value,就是配置文件中的cache-names
關於注解中的key這個值,如果不指定的話 ,那么會取方法參數當做Key
application.yml
-
spring:
-
cache:
-
#緩存名稱
-
cache-names: guavaDemo
-
#緩存最大數量500條, 緩存失效時間 6個小時
-
guava.spec: maximumSize=500,expireAfterWrite=360m
App.java
-
package com.ibigsea.springboot_cache_demo;
-
-
import java.text.SimpleDateFormat;
-
import java.util.Date;
-
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.boot.SpringApplication;
-
import org.springframework.boot.autoconfigure.SpringBootApplication;
-
import org.springframework.cache.annotation.EnableCaching;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
import org.springframework.web.bind.annotation.RestController;
-
-
import com.ibigsea.springboot_cache_demo.cache.DataCache;
-
-
/**
-
* 是Spring Boot項目的核心注解,主要是開啟自動配置
-
*/
-
-
-
// 開啟緩存
-
-
public class App {
-
-
-
private DataCache dataCache;
-
-
public static void main(String[] args) {
-
SpringApplication.run(App.class, args);
-
}
-
-
-
public String put(Long id, String value) {
-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-
return sdf.format(new Date()) + " : value is " + dataCache.put(id, value) ;
-
}
-
-
-
public String query(Long id){
-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-
return sdf.format(new Date()) + " : value is " +dataCache.query(id) ;
-
}
-
-
-
public String remove(Long id) {
-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-
dataCache.remove(id) ;
-
return sdf.format(new Date()) + " : success " ;
-
}
-
-
}
運行結果

關於注解配置:
@Cacheable

@CacheEvict

@CachePut
和上面的一樣,只是這個注解是用來更新或者插入數據到緩存中的,
其中key自己定義,返回值會緩存
還有就是SpringBoot會根據你的類路徑里面的依賴jar,來確定使用什么類型進行緩存,所以基本是我們是不用配置spring.cache.type這個屬性的
Redis實現
Redis緩存:
如果是用redis作為緩存的話
我們只需要引入redis相關依賴,修改yml配置屬性
-
<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>
-
<groupId>com.ibigsea</groupId>
-
<artifactId>spirngboot-cache-demo</artifactId>
-
<version>0.0.1-SNAPSHOT</version>
-
-
-
<properties>
-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-
<boot.version>1.3.5.RELEASE</boot.version>
-
</properties>
-
-
<dependencies>
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-web</artifactId>
-
<version>${boot.version}</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-test</artifactId>
-
<version>${boot.version}</version>
-
<scope>test</scope>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-cache</artifactId>
-
<version>${boot.version}</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-redis</artifactId>
-
<version>${boot.version}</version>
-
</dependency>
-
<!-- <dependency> -->
-
<!-- <groupId>com.google.guava</groupId> -->
-
<!-- <artifactId>guava</artifactId> -->
-
<!-- <version>19.0</version> -->
-
<!-- </dependency> -->
-
</dependencies>
-
</project>
application.yml
-
spring:
-
cache:
-
#緩存名稱
-
cache-names: guavaDemo
-
#緩存最大數量500條, 緩存失效時間 6個小時
-
#guava.spec: maximumSize=500,expireAfterWrite=360m
-
# REDIS (RedisProperties)
-
redis :
-
host : localhost # server host
-
port : 6379 # connection port
-
pool.max-idle : 8 # pool settings ...
-
pool.min-idle : 1
-
pool.max-active : 8
-
pool.max-wait : -1
就這樣就OK了,代碼什么的都是不用改變的,是不是很方便
測試結果


數據都會緩存到redis里面
其他的地方就不測試了 都是差不多的
使用其他實現導入對應的依賴,然后添加配置即可
注意:
如果使用guava緩存的時候 ,同時添加了redis的jar依賴,或者其他的依賴,可能會出現異常
這個時候加上 type: GUAVA 就可以