ehcache 缓存的使用
合理的使用缓存会极大的提高程序的运行效率。切记:缓存请勿滥用。
配置ehcache与Shiro
shiro初识请查看该文章
https://blog.csdn.net/pyfysf/article/details/81952889
一、导入ehcache的依赖
<dependency>
<groupId>net.sf.ehcachegroupId>
<artifactId>ehcacheartifactId>
<version>2.6.7version>
<type>pomtype>
dependency>
二、创建ehcache.xml配置文件
简洁版
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
<cache
name="ehcacheName"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300000"
timeToLiveSeconds="600000"
overflowToDisk="true" />
<cache name="sysCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true"/>
ehcache>
注释版
"http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
"java.io.tmpdir"/>
"10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
"ehcacheName"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300000"
timeToLiveSeconds="600000"
overflowToDisk="true" />
"sysCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true"/>
三、配置ehcacheManager与shiro进行结合
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManagerConfigFile" value="classpath:ehcache.xml">property>
bean>
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="myRealm"/>
<property name="cacheManager" ref="cacheManager"/>
bean>
附录(EhcacheUtils):
/**
* Cache工具类
*/
public class CacheUtils {
private static CacheManager cacheManager;
private static final String SYS_CACHE = "sysCache";
public final static Byte[] locks = new Byte[0];
/**
* 获取SYS_CACHE缓存
*
* @param key
* @return
*/
public static Object get(String key) {
return get(SYS_CACHE, key);
}
/**
* 写入SYS_CACHE缓存
*
* @param key
* @return
*/
public static void put(String key, Object value) {
put(SYS_CACHE, key, value);
}
/**
* @param key
* @return
*/
public static void remove(String key) {
remove(SYS_CACHE, key);
}
/**
* 获取缓存
*
* @param cacheName
* @param key
* @return
*/
public static Object get(String cacheName, String key) {
Element element = getCache(cacheName).get(key);
return element == null ? null : element.getObjectValue();
}
/**
* 写入缓存
*
* @param cacheName
* @param key
* @param value
*/
public static void put(String cacheName, String key, Object value) {
Element element = new Element(key, value);
getCache(cacheName).put(element);
}
/**
* 从缓存中移除
*
* @param cacheName
* @param key
*/
public static void remove(String cacheName, String key) {
getCache(cacheName).remove(key);
}
public static void removeAll(String cacheName) {
getCache(cacheName).removeAll();
}
/**
* @param cacheName
* @return
*/
private static Cache getCache(String cacheName) {
if (cacheManager == null) {
synchronized (locks) {
if (cacheManager == null) {
cacheManager = ((CacheManager) SpringContextUtil.getBean("cacheManager"));
}
}
}
Cache cache = cacheManager.getCache(cacheName);
if (cache == null) {
cacheManager.addCache(cacheName);
cache = cacheManager.getCache(cacheName);
cache.getCacheConfiguration().setEternal(true);
}
return cache;
}
public static CacheManager getCacheManager() {
return cacheManager;
}
}