Guava Cache是一種本地緩存機制,之所以叫本地緩存,是因為它不會把緩存數據放到外部文件或者其他服務器上,而是存放到了應用內存中。
Guava Cache的優點是:簡單、強大、輕量級。
GuavaCache適用場景:
1.某些接口或者鍵值會被查詢多次以上;
2.願意使用或犧牲一些內存空間來提升訪問或者計算速度;
3.緩存內容或者結果值較小,不會超過內存總容量;
GuavaCache中基於注解的聲明式緩存操作
@Cacheable
觸發緩存邏輯
Spring 在執行 @Cacheable 標注的方法前先查看緩存中是否有數據,如果有數據,則直接返回緩存數據;若沒有數據,執行該方法並將方法返回值放進緩存。
參數: value緩存名、 key緩存鍵值、 condition滿足緩存條件、unless否決緩存條件
@CacheEvict
觸發緩存逐出邏輯
方法執行成功后會從緩存中移除相應數據。
參數: value緩存名、 key緩存鍵值、 condition滿足緩存條件、 unless否決緩存條件、 allEntries是否移除所有數據 (設置為true時會移除所有緩存)
@CachePut
和 @Cacheable 類似,但會把方法的返回值放入緩存中, 主要用於數據新增和修改方法。
pom.xml配置文件:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> <version>1.5.9.RELEASE</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.0</version> </dependency>
GuavaCacheConfig:

/** * Copyright (c) 2020, All Rights Reserved. * */ package com.demo.server.config; import java.util.concurrent.TimeUnit; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.guava.GuavaCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.google.common.cache.CacheBuilder; @Configuration @EnableCaching public class GuavaCacheConfig { @Bean public CacheManager cacheManager() { GuavaCacheManager cacheManager = new GuavaCacheManager(); cacheManager.setCacheBuilder( CacheBuilder.newBuilder(). expireAfterWrite(10, TimeUnit.SECONDS). //存活時間10秒 maximumSize(1000)); //最大線程數 return cacheManager; } }
CacheController:

/** * Copyright (c) 2020, All Rights Reserved. * */ package com.demo.server.guava; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value = "/guava") public class CacheController { @Autowired private CacheService cacheService; /** * 查詢方法 */ @RequestMapping(value = "/get", method = RequestMethod.GET) public String getByCache() { Long startTime = System.currentTimeMillis(); long timestamp = this.cacheService.getByCache(); Long endTime = System.currentTimeMillis(); System.out.println("耗時: " + (endTime - startTime)); return timestamp+""; } /** * 保存方法 */ @RequestMapping(value = "/save", method = RequestMethod.POST) public void save() { this.cacheService.save(); } /** * 刪除方法 */ @RequestMapping(value = "delete", method = RequestMethod.DELETE) public void delete() { this.cacheService.delete(); } }
CacheService:

/** * Copyright (c) 2020, All Rights Reserved. * */ package com.demo.server.guava; import java.sql.Timestamp; 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 CacheService { @CachePut(value = "guavacache") public long save() { long timestamp = new Timestamp(System.currentTimeMillis()).getTime(); System.out.println("進行緩存:" + timestamp); return timestamp; } @CacheEvict(value = "guavacache") public void delete() { System.out.println("刪除緩存"); } @Cacheable(value = "guavacache") public long getByCache() { try { Thread.sleep(3 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } return new Timestamp(System.currentTimeMillis()).getTime(); } }
Application:

package com.demo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.Banner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { private static final Logger LOG = LoggerFactory.getLogger(Application.class); public static void main(String[] args) { SpringApplication app = new SpringApplication(Application.class); app.setBannerMode(Banner.Mode.OFF); app.setWebEnvironment(true); app.run(args); LOG.info("**************** Startup Success ****************"); } }