Spring Boot基礎教程19-使用Caching-EhCache

Spring boot 支持的緩存: 
• Generic
• JCache (JSR-107)
• EhCache 2.x
• Hazelcast
• Infinispan
• Couchbase
• Redis
• Caffeine
• Guava
• Simple
最常用的是 EhCache,文檔多,資料全
一、 添加依賴 
<!-- caching -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
二、 配置文件: 
spring.cache.type=ehcache
spring.cache.ehcache.config=classpath:config/ehcache.xml
ehcache.xml 
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd">
<cache name="roncooCache"
eternal="false"
maxEntriesLocalHeap="0"
timeToIdleSeconds="50"></cache>
<!-- eternal:true表示對象永不過期,此時會忽略timeToIdleSeconds和
timeToLiveSeconds屬性,默認爲false -->
<!-- maxEntriesLocalHeap:堆內存中最大緩存對象數,0沒有限制 --><!-- timeToIdleSeconds: 設定允許對象處於空閒狀態的最長時間,以秒爲
單位。當對象自從最近一次被訪問後,如果處於空閒狀態的時間超過了
timeToIdleSeconds屬性值,這個對象就會過期,EHCache將把它從緩存中清空。
只有當eternal屬性爲false,該屬性纔有效。如果該屬性值爲0,則表示對象可以
無限期地處於空閒狀態 -->
</ehcache>
三、 啓用註解支持: 
@EnableCaching:啓用緩存註解
代碼實現: 
/**
* @author wujing
*/
public interface RoncooUserLogCache {
/**
* 查詢

* @param id
* @return
*/
RoncooUserLog selectById(Integer id);
/**
* 更新

* @param roncooUserLog
* @return
*/
RoncooUserLog updateById(RoncooUserLog roncooUserLog);
/**
* 刪除

* @param id
* @return
*/
String deleteById(Integer id);
}實現類: 
/**
* @author wujing
*/
@CacheConfig(cacheNames = "roncooCache")
@Repository
public class RoncooUserLogCacheImpl implements
RoncooUserLogCache {
@Autowired
private RoncooUserLogDao roncooUserLogDao;
@Cacheable(key = "#p0")
@Override
public RoncooUserLog selectById(Integer id) {
System.out.println("查詢功能,緩存找不到,直接讀庫, id=" + 
id);
return roncooUserLogDao.findOne(id);
}
@CachePut(key = "#p0")
@Override
public RoncooUserLog updateById(RoncooUserLog 
roncooUserLog) {
System.out.println("更新功能,更新緩存,直接寫庫, id=" + 
roncooUserLog);
return roncooUserLogDao.save(roncooUserLog);
}
@CacheEvict(key = "#p0")
@Override
public String deleteById(Integer id) {
System.out.println("刪除功能,刪除緩存,直接寫庫, id=" + id);
return "清空緩存成功";
}
}
註解說明: 
@CacheConfig:緩存配置
@Cacheable:應用到讀取數據的方法上,即可緩存的方法,如查找方法:先從緩存中讀取,如果沒有再調
用方法獲取數據,然後把數據添加到緩存中。適用於查找
@CachePut:主要針對方法配置,能夠根據方法的請求參數對其結果進行緩存,和 @Cacheable 不同的是,它每次都會觸發真實方法的調用。適用於更新和插入 
@CacheEvict:主要針對方法配置,能夠根據一定的條件對緩存進行清空。適用於刪除
測試: 
@RequestMapping(value = "/select", method = RequestMethod.GET)
public RoncooUserLog get(@RequestParam(defaultValue = "1") Integer id) {
return RoncooUserLogCache.selectById(id);
}
@RequestMapping(value = "/update", method = RequestMethod.GET)
public RoncooUserLog update(@RequestParam(defaultValue = "1") Integer id) {
RoncooUserLog bean = RoncooUserLogCache.selectById(id);
bean.setUserName("測試");
bean.setCreateTime(new Date());
RoncooUserLogCache.updateById(bean);
return bean;
}
@RequestMapping(value = "/del", method = RequestMethod.GET)
public String del(@RequestParam(defaultValue = "1") Integer id) {
return RoncooUserLogCache.deleteById(id);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章