add Ehcache
This commit is contained in:
parent
d6e9bf7203
commit
373b4fc924
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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>
|
||||
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
|
@ -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" />
|
||||
|
|
|
@ -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>
|
||||
<!--
|
||||
name:Cache的唯一标识
|
||||
maxElementsInMemory:内存中最大缓存对象数
|
||||
maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大
|
||||
eternal:Element是否永久有效,一但设置了,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(较少使用)
|
||||
-->
|
Loading…
Reference in New Issue