add Ehcache

This commit is contained in:
shuzheng 2016-10-14 22:29:54 +08:00
parent d6e9bf7203
commit 373b4fc924
7 changed files with 97 additions and 18 deletions

View File

@ -2,6 +2,9 @@ package com.zheng.cms.service;
import com.zheng.cms.model.User;
import com.zheng.cms.model.UserVO;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@ -17,7 +20,8 @@ import org.springframework.test.context.transaction.TransactionConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({
"classpath:applicationContext.xml",
"classpath:applicationContext-jdbc.xml"
"classpath:applicationContext-jdbc.xml",
"classpath:applicationContext-ehcache.xml"
})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public class UserServiceTest {
@ -33,6 +37,27 @@ public class UserServiceTest {
// 自动生成接口调用
User user = userService.getMapper().selectByPrimaryKey(1);
System.out.println(null == user ? "null" :user.getNickname());
// Create a cache manager
final CacheManager cacheManager = CacheManager.getInstance();
// create the cache called "hello-world"
final Cache cache = cacheManager.getCache("ehCache");
// create a key to map the data to
final String key = "key";
// Create a data element
final Element element = new Element(key, "value");
// Put the element into the data store
cache.put(element);
// Retrieve the data element
final Element cacheElement = cache.get(key);
// Print the value
System.out.println(cacheElement.getObjectValue());
}
}

View File

@ -28,7 +28,8 @@
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml,
classpath:applicationContext-jdbc.xml
classpath:applicationContext-jdbc.xml,
classpath:applicationContext-ehcache.xml
</param-value>
</context-param>

View File

@ -27,7 +27,7 @@
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
@ -61,15 +61,11 @@
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
<!-- 缓存 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-ehcache</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.ehcache</groupId>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.1.3</version>
<version>2.10.0</version>
</dependency>
<!-- 数据库 -->
<dependency>

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- cache管理器配置 -->
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
</bean>
<!-- 支持缓存注解 -->
<cache:annotation-driven cache-manager="cacheManager" />
<!-- 默认是cacheManager -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="cacheManagerFactory"/>
</bean>
</beans>

View File

@ -1,18 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 装载service -->
<context:component-scan base-package="**.service" />

View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache>
<cache
name="ehCache"
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</cache>
</ehcache>
<!--
nameCache的唯一标识
maxElementsInMemory内存中最大缓存对象数
maxElementsOnDisk磁盘中最大缓存对象数若是0表示无穷大
eternalElement是否永久有效一但设置了timeout将不起作用
overflowToDisk配置此属性当内存中Element数量达到maxElementsInMemory时Ehcache将会Element写到磁盘中
timeToIdleSeconds设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用可选属性默认值是0也就是可闲置时间无穷大
timeToLiveSeconds设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用默认是0.也就是element存活时间无穷大
diskPersistent是否缓存虚拟机重启期数据
diskExpiryThreadIntervalSeconds磁盘失效线程运行时间间隔默认是120秒
diskSpoolBufferSizeMB这个参数设置DiskStore磁盘缓存的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区
memoryStoreEvictionPolicy当达到maxElementsInMemory限制时Ehcache将会根据指定的策略去清理内存。默认策略是LRU最近最少使用。你可以设置为FIFO先进先出或是LFU较少使用
-->