<!-- 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 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>
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 "清空緩存成功";
}
}
註解說明:
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());
}
}