example for ehcache
This commit is contained in:
parent
373b4fc924
commit
0a4b5ba7e9
|
@ -38,24 +38,19 @@ public class UserServiceTest {
|
||||||
User user = userService.getMapper().selectByPrimaryKey(1);
|
User user = userService.getMapper().selectByPrimaryKey(1);
|
||||||
System.out.println(null == user ? "null" :user.getNickname());
|
System.out.println(null == user ? "null" :user.getNickname());
|
||||||
|
|
||||||
|
// EhCache调用
|
||||||
// Create a cache manager
|
// Create a cache manager
|
||||||
final CacheManager cacheManager = CacheManager.getInstance();
|
final CacheManager cacheManager = CacheManager.getInstance();
|
||||||
|
|
||||||
// create the cache called "hello-world"
|
// create the cache called "hello-world"
|
||||||
final Cache cache = cacheManager.getCache("ehCache");
|
final Cache cache = cacheManager.getCache("ehcache_common");
|
||||||
|
|
||||||
// create a key to map the data to
|
// create a key to map the data to
|
||||||
final String key = "key";
|
final String key = "key";
|
||||||
|
|
||||||
// Create a data element
|
// Create a data element
|
||||||
final Element element = new Element(key, "value");
|
final Element element = new Element(key, "value");
|
||||||
|
|
||||||
// Put the element into the data store
|
// Put the element into the data store
|
||||||
cache.put(element);
|
cache.put(element);
|
||||||
|
|
||||||
// Retrieve the data element
|
// Retrieve the data element
|
||||||
final Element cacheElement = cache.get(key);
|
final Element cacheElement = cache.get(key);
|
||||||
|
|
||||||
// Print the value
|
// Print the value
|
||||||
System.out.println(cacheElement.getObjectValue());
|
System.out.println(cacheElement.getObjectValue());
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,74 @@
|
||||||
|
package com.zheng.cms.controller;
|
||||||
|
|
||||||
|
import com.zheng.common.util.EhCacheUtil;
|
||||||
|
import net.sf.ehcache.Element;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 缓存controller
|
||||||
|
* @author shuzheng
|
||||||
|
* @date 2016年10月15日
|
||||||
|
*/
|
||||||
|
@Controller
|
||||||
|
@RequestMapping("/cache")
|
||||||
|
public class CacheController {
|
||||||
|
|
||||||
|
private static Logger logger = LoggerFactory.getLogger(CacheController.class);
|
||||||
|
|
||||||
|
private final static String CACHE_NAME = "ehcache_common";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增缓存记录
|
||||||
|
* @param request
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@RequestMapping("/add")
|
||||||
|
@ResponseBody
|
||||||
|
public Object add(HttpServletRequest request) {
|
||||||
|
String key = request.getParameter("key");
|
||||||
|
String value = request.getParameter("value");
|
||||||
|
EhCacheUtil.put(CACHE_NAME, key, value);
|
||||||
|
return "success";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除缓存记录
|
||||||
|
* @param request
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@RequestMapping("/remove")
|
||||||
|
@ResponseBody
|
||||||
|
public Object remove(HttpServletRequest request) {
|
||||||
|
String key = request.getParameter("key");
|
||||||
|
EhCacheUtil.remove(CACHE_NAME, key);
|
||||||
|
return "success";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取缓存记录
|
||||||
|
* @param request
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@RequestMapping("/get")
|
||||||
|
@ResponseBody
|
||||||
|
public Object get(HttpServletRequest request) {
|
||||||
|
String key = request.getParameter("key");
|
||||||
|
Object object = EhCacheUtil.get(CACHE_NAME, key);
|
||||||
|
if (null == object) {
|
||||||
|
logger.debug("【Ehcache】没有找到key={}的记录!", key);
|
||||||
|
return "value";
|
||||||
|
}
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1 +1,2 @@
|
||||||
env=${profile.env}
|
env=${profile.env}
|
||||||
|
ehcache_name=ehcache_common
|
|
@ -0,0 +1,89 @@
|
||||||
|
package com.zheng.common.util;
|
||||||
|
|
||||||
|
import net.sf.ehcache.Cache;
|
||||||
|
import net.sf.ehcache.CacheManager;
|
||||||
|
import net.sf.ehcache.Element;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ehcache工具类
|
||||||
|
* Created by shuzheng on 2016/10/15.
|
||||||
|
*/
|
||||||
|
public class EhCacheUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取缓存
|
||||||
|
* @param cacheName
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static Cache getCache(String cacheName) {
|
||||||
|
CacheManager cacheManager = CacheManager.getInstance();
|
||||||
|
if (null == cacheManager) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Cache cache = cacheManager.getCache(cacheName);
|
||||||
|
if (null == cache) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增缓存记录
|
||||||
|
* @param cacheName
|
||||||
|
* @param key
|
||||||
|
* @param value
|
||||||
|
*/
|
||||||
|
public static void put(String cacheName, String key, Object value) {
|
||||||
|
Cache cache = getCache(cacheName);
|
||||||
|
if (null != cache) {
|
||||||
|
Element element = new Element(key, value);
|
||||||
|
cache.put(element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除缓存记录
|
||||||
|
* @param cacheName
|
||||||
|
* @param key
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static boolean remove(String cacheName, String key) {
|
||||||
|
Cache cache = getCache(cacheName);
|
||||||
|
if (null == cache) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return cache.remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除全部缓存记录
|
||||||
|
* @param cacheName
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static void removeAll(String cacheName) {
|
||||||
|
Cache cache = getCache(cacheName);
|
||||||
|
if (null != cache) {
|
||||||
|
cache.removeAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取缓存记录
|
||||||
|
* @param cacheName
|
||||||
|
* @param key
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static Object get(String cacheName, String key) {
|
||||||
|
Cache cache = getCache(cacheName);
|
||||||
|
if (null == cache) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Element cacheElement = cache.get(key);
|
||||||
|
if (null == cacheElement) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return cacheElement.getObjectValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -8,14 +8,14 @@
|
||||||
http://www.springframework.org/schema/cache
|
http://www.springframework.org/schema/cache
|
||||||
http://www.springframework.org/schema/cache/spring-cache.xsd">
|
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" />
|
<cache:annotation-driven cache-manager="cacheManager" />
|
||||||
|
|
||||||
|
<!-- cache管理器配置 -->
|
||||||
|
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
|
||||||
|
<property name="configLocation" value="classpath:ehcache-common.xml"/>
|
||||||
|
</bean>
|
||||||
|
|
||||||
<!-- 默认是cacheManager -->
|
<!-- 默认是cacheManager -->
|
||||||
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
|
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
|
||||||
<property name="cacheManager" ref="cacheManagerFactory"/>
|
<property name="cacheManager" ref="cacheManagerFactory"/>
|
||||||
|
|
|
@ -10,5 +10,5 @@
|
||||||
|
|
||||||
<!-- 装载service -->
|
<!-- 装载service -->
|
||||||
<context:component-scan base-package="**.service" />
|
<context:component-scan base-package="**.service" />
|
||||||
|
|
||||||
</beans>
|
</beans>
|
|
@ -0,0 +1,16 @@
|
||||||
|
<?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"/>
|
||||||
|
<cache
|
||||||
|
name="ehcache_common"
|
||||||
|
eternal="false"
|
||||||
|
timeToIdleSeconds="120"
|
||||||
|
timeToLiveSeconds="300"
|
||||||
|
maxEntriesLocalHeap="10000"
|
||||||
|
maxEntriesLocalDisk="10000000"
|
||||||
|
diskExpiryThreadIntervalSeconds="120"
|
||||||
|
memoryStoreEvictionPolicy="LRU">
|
||||||
|
<persistence strategy="localTempSwap"/>
|
||||||
|
</cache>
|
||||||
|
</ehcache>
|
|
@ -1,39 +0,0 @@
|
||||||
<?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