在jeecms v5中采用缓存管理回话的方式,这里采用最简单的ehcache实现回话的管理。
具体实现如下:
Ehcache的配置:
<?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/jeecms/hibernate"/>-->
<defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120"
overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000"
diskPersistent="false" diskExpiryThreadIntervalSeconds="120"/>
<cache name="com.jeecms.common.web.session.Session" maxElementsInMemory="500" eternal="false" timeToIdleSeconds="1200" overflowToDisk="true" diskPersistent="true"/>
<cache name="com.jeecms.cms.front.ContentCount" maxElementsInMemory="500" eternal="true" overflowToDisk="true" diskPersistent="false"/>
</ehcache>
Spring的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-lazy-init="true">
<!--缓存-->
<bean id="cacheManager" class="com.jeecms.common.web.WebEhCacheManagerFacotryBean">
<property name="configLocation">
<value>/WEB-INF/config/ehcache-application.xml</value>
</property>
<property name="diskStoreLocation">
<value>/WEB-INF/cache/application</value>
</property>
</bean>
<!--SESSION缓存-->
<bean id="ehSessionCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<ref local="cacheManager"/>
</property>
<property name="cacheName">
<value>com.jeecms.common.web.Session</value>
</property>
<qualifier value="session"/>
</bean>
<!--内容计数缓存-->
<bean id="ehContentCountCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<ref local="cacheManager"/>
</property>
<property name="cacheName">
<value>com.jeecms.cms.front.ContentCount</value>
</property>
<qualifier value="contentCount"/>
</bean>
<!--站点流量缓存-->
<bean id="ehCmsSiteFlowCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<ref local="cacheManager"/>
</property>
<property name="cacheName">
<value>com.jeecms.cms.front.CmsSiteFlow</value>
</property>
<qualifier value="cmsSiteFlow"/>
</bean>
</beans>
package com.jeecms.common.web;
import java.io.IOException;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.ObjectExistsException;
import net.sf.ehcache.config.Configuration;
import net.sf.ehcache.config.ConfigurationFactory;
import net.sf.ehcache.config.DiskStoreConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
/**
* 用于Web的EhCacheManagerFacotryBean。可以基于Web根目录指定diskStore地址。
*/
public class WebEhCacheManagerFacotryBean implements FactoryBean<CacheManager>,
InitializingBean, DisposableBean {
private final Logger log = LoggerFactory
.getLogger(WebEhCacheManagerFacotryBean.class);
private Resource configLocation;
private Resource diskStoreLocation;
private String cacheManagerName;
private CacheManager cacheManager;
/**
* Set the location of the EHCache config file. A typical value is
* "/WEB-INF/ehcache.xml".
* <p>
* Default is "ehcache.xml" in the root of the class path, or if not found,
* "ehcache-failsafe.xml" in the EHCache jar (default EHCache
* initialization).
*
* @see net.sf.ehcache.CacheManager#create(java.io.InputStream)
* @see net.sf.ehcache.CacheManager#CacheManager(java.io.InputStream)
*/
public void setConfigLocation(Resource configLocation) {
this.configLocation = configLocation;
}
/**
* 设置ehcahce的diskStore path,例如:/WEB-INF/cache
*
* 如设置了此项,则在ehcache配置文件中不要配置<diskStore path=""/>,否则本设置无效。
*
* @param diskStoreLocation
*/
public void setdiskStoreLocation(Resource diskStoreLocation) {
this.diskStoreLocation = diskStoreLocation;
}
/**
* Set the name of the EHCache CacheManager (if a specific name is desired).
*
* @see net.sf.ehcache.CacheManager#setName(String)
*/
public void setCacheManagerName(String cacheManagerName) {
this.cacheManagerName = cacheManagerName;
}
public void afterPropertiesSet() throws IOException, CacheException {
log.info("Initializing EHCache CacheManager");
Configuration config = null;
if (this.configLocation != null) {
config = ConfigurationFactory
.parseConfiguration(this.configLocation.getInputStream());
if (this.diskStoreLocation != null) {
DiskStoreConfiguration dc = new DiskStoreConfiguration();
dc.setPath(this.diskStoreLocation.getFile().getAbsolutePath());
try {
config.addDiskStore(dc);
} catch (ObjectExistsException e) {
log.warn("if you want to config distStore in spring,"
+ " please remove diskStore in config file!", e);
}
}
}
if (config != null) {
this.cacheManager = new CacheManager(config);
} else {
this.cacheManager = new CacheManager();
}
if (this.cacheManagerName != null) {
this.cacheManager.setName(this.cacheManagerName);
}
}
public CacheManager getObject() {
return this.cacheManager;
}
public Class<? extends CacheManager> getObjectType() {
return (this.cacheManager != null ? this.cacheManager.getClass()
: CacheManager.class);
}
public boolean isSingleton() {
return true;
}
public void destroy() {
log.info("Shutting down EHCache CacheManager");
this.cacheManager.shutdown();
}
}
<!--EndFragment-->