SpringBoot 之 Spring Boot 使用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:啓用緩存註解
 
package com.roncoo.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;

@EnableCaching
@ServletComponentScan
@SpringBootApplication
public class SpringBootDemo201Application {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootDemo201Application.class, args);
	}
}
 
代碼實現:
package com.roncoo.example.cache;

import com.roncoo.example.bean.RoncooUserLog;

/**
 * @author 
 */
public interface RoncooUserLogCache {

	/**
	 * 查詢
	 * 
	 * @param id
	 * @return
	 */
	RoncooUserLog selectById(Integer id);

	/**
	 * 更新
	 * 
	 * @param roncooUserLog
	 * @return
	 */
	RoncooUserLog updateById(RoncooUserLog roncooUserLog);

	/**
	 * 刪除
	 * 
	 * @param id
	 * @return
	 */
	String deleteById(Integer id);
}
實現類:
 
package com.roncoo.example.cache.impl;

import org.springframework.beans.factory.annotation.Autowired;
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.Repository;

import com.roncoo.example.bean.RoncooUserLog;
import com.roncoo.example.cache.RoncooUserLogCache;
import com.roncoo.example.dao.RoncooUserLogDao;

/**
 * @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:主要針對方法配置,能夠根據一定的條件對緩存進行清空。適用於刪除
 
測試:
package com.roncoo.example;

import java.util.Date;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.test.context.junit4.SpringRunner;

import com.roncoo.example.bean.RoncooUserLog;
import com.roncoo.example.dao.RoncooUserLogMongoDao;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootDemo201ApplicationTests {
	@Autowired
	private RoncooUserLogMongoDao roncooUserLogMongoDao;

	@Test
	public void insert() {
		RoncooUserLog entity = new RoncooUserLog();
		entity.setId(1);
		entity.setUserName("無境");
		entity.setUserIp("192.168.0.1");
		entity.setCreateTime(new Date());
		roncooUserLogMongoDao.save(entity);
	}

	@Test
	public void delete() {
		roncooUserLogMongoDao.delete(1);
	}

	@Test
	public void update() {
		RoncooUserLog entity = new RoncooUserLog();
		entity.setId(2);
		entity.setUserName("無境2");
		entity.setUserIp("192.168.0.1");
		entity.setCreateTime(new Date());
		roncooUserLogMongoDao.save(entity);
	}

	@Test
	public void select() {
		RoncooUserLog result = roncooUserLogMongoDao.findOne(1);
		System.out.println(result);
	}

	@Test
	public void select2() {
		RoncooUserLog result = roncooUserLogMongoDao.findByUserName("無境2");
		System.out.println(result);
	}

	// 分頁
	@Test
	public void queryForPage() {
		Pageable pageable = new PageRequest(0, 20, new Sort(new Order(Direction.DESC, "id")));
		// Page<RoncooUserLog> result = roncooUserLogDao.findByUserName("無境2", pageable);
		Page<RoncooUserLog> result = roncooUserLogMongoDao.findAll(pageable);
		System.out.println(result.getContent());
	}


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