1. 該說的話
每個人都應當學會獨立地去思考、去尋找答案,而不是一味地伸手向他人索取所謂的標準答案, 首先,別成為“拿來主義”者,其次遠離"拿來主義"的人,
2. ehcache
2.1 主要特性
- 快速,簡單.
- 多種快取策略
- 快取資料有兩級:記憶體和磁盤,因此無需擔心容量問題
- 快取資料會在虛擬機重啟的程序中寫入磁盤
- 可以通過RMI、可插入API等方式進行分布式快取
- 具有快取和快取管理器的偵聽介面
- 支持多快取管理器實體,以及一個實體的多個快取區域
- 提供Hibernate的快取實作
2.2 和redis相比
- ehcache直接在jvm虛擬機中快取,速度快,效率高;但是快取共享麻煩,集群分布式應用不方便,
- redis是通過socket訪問到快取服務,效率比ecache低,比資料庫要快很多.
2.3 在應用程式中的位置

3. spring boot 整合
1.搭建spring boot 專案
2. pom.xml檔案中添加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
- 添加ehcache.xml組態檔
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<!--
磁盤存盤:將快取中暫時不使用的物件,轉移到硬碟,類似于Windows系統的虛擬記憶體
path:指定在硬碟上存盤物件的路徑
path可以配置的目錄有:
user.home(用戶的家目錄)
user.dir(用戶當前的作業目錄)
java.io.tmpdir(默認的臨時目錄)
ehcache.disk.store.dir(ehcache的配置目錄)
絕對路徑(如:d:\\ehcache)
查看路徑方法:String tmpDir = System.getProperty("java.io.tmpdir");
-->
<diskStore path="java.io.tmpdir" />
<!--
defaultCache:默認的快取配置資訊,如果不加特殊說明,則所有物件按照此配置項處理
maxElementsInMemory:設定了快取的上限,最多存盤多少個記錄物件
eternal:代表物件是否永不過期 (指定true則下面兩項配置需為0無限期)
timeToIdleSeconds:最大的發呆時間 /秒
timeToLiveSeconds:最大的存活時間 /秒
overflowToDisk:是否允許物件被寫入到磁盤
說明:下列配置自快取建立起600秒(10分鐘)有效 ,
在有效的600秒(10分鐘)內,如果連續120秒(2分鐘)未訪問快取,則快取失效,
就算有訪問,也只會存活600秒,
-->
<defaultCache maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="600" timeToLiveSeconds="600" overflowToDisk="true" />
<!--有效時間: 7200秒 = 2小時 ,連續180秒 = 3分鐘未訪問快取,則失效-->
<cache name="userCache" maxElementsInMemory="10000" eternal="false"
timeToIdleSeconds="1800" timeToLiveSeconds="7200" overflowToDisk="true" />
</ehcache>
- 啟用快取
在啟動類添加@EnableCaching注解,
5.應用
如下代碼所示,你可以在函式上使用@Cacheable,@CachePut,@CacheEvict,來新增、更新、洗掉快取,
注意,當這個類中所有的快取都處于同一快取區時,你可以在類名上方使用@CacheConfig(cacheNames =userCache )來配置,這樣在函式注解上就不需要再寫 value = https://www.cnblogs.com/liululee/p/userCache,(cacheNames 的值在ehcache.xml檔案中配置,)
@Service
public class UserServiceImpl implements IUserService {
@Resource
private UserRepository userRepository;
@Override
@Cacheable(value = "https://www.cnblogs.com/liululee/p/userCache", key = "#user.id")
public boolean addUser(UserEntity user) {
return userRepository.saveAndFlush(user).getId() != null;
}
@Override
@CachePut(value = "https://www.cnblogs.com/liululee/p/userCache", key = "#user.id")
public boolean updateUser(UserEntity user) {
return userRepository.saveAndFlush(user).getId() != null;
}
@Override
@CacheEvict(value = "https://www.cnblogs.com/liululee/p/userCache", key = "#id")
public boolean deleteUser(Long id) {
return false;
}
@Override
@Cacheable(value = "https://www.cnblogs.com/liululee/p/userCache", key = "#id")
public UserEntity selectUser(Long id) {
return null;
}
}
當你想要洗掉某一快取區所有快取時,可以使用 @CacheEvict(value = "https://www.cnblogs.com/liululee/p/userCache", allEntries = true), 洗掉userCache中所有的快取,
4. “毒雞湯”,和我一起干了吧!
每個人都“畫地為牢”,把志同道合者劃入圈內,把異己者排除在外,選好你的“地”,劃好你的“圈”,“親賢臣,遠小人,此先漢所以興隆也;親小人,遠賢臣,此后漢所以傾頹也,” 把自己經營好,就相當于把自己的圈子經營好,和志同道合者,一同去征服星辰大海!
示例代碼可在我的 github.com 中找到,
歡迎關注我,
公眾號: 鍋外的大佬 ,歡迎加群~
博客地址: http://www.developlee.top
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/138750.html
標籤:Java
