---
title: "jeecms v5 spring和ehcache的整合"
author: "longgangbai"
date: "2014-06-14T15:53:19Z"
tags: []
categories: ["互联网"]
slug: "jeecms-v5-spring-ehcache"
draft: false
lastmod: "2014-06-14T15:53:18Z"
id: 2079475
url: "https://topmanopensource.iteye.com/blog/2079475"
---

在jeecms v5中采用缓存管理回话的方式，这里采用最简单的ehcache实现回话的管理。

具体实现如下：

### Ehcache的配置：

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"&gt;

&lt;!--&lt;diskStore path="java.io.tmpdir/jeecms/hibernate"/&gt;--&gt;

&lt;defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120"

overflowToDisk="true" diskSpoolBufferSizeMB="30" maxElementsOnDisk="10000000" 

diskPersistent="false" diskExpiryThreadIntervalSeconds="120"/&gt;

 

&lt;cache name="com.jeecms.common.web.session.Session" maxElementsInMemory="500" eternal="false" timeToIdleSeconds="1200" overflowToDisk="true" diskPersistent="true"/&gt;

&lt;cache name="com.jeecms.cms.front.ContentCount" maxElementsInMemory="500" eternal="true" overflowToDisk="true" diskPersistent="false"/&gt;

 

&lt;/ehcache&gt;

### Spring的配置

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;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"&gt;

&lt;!--缓存--&gt;

&lt;bean id="cacheManager" class="com.jeecms.common.web.WebEhCacheManagerFacotryBean"&gt;

&lt;property name="configLocation"&gt;

&lt;value&gt;/WEB-INF/config/ehcache-application.xml&lt;/value&gt;

&lt;/property&gt;

&lt;property name="diskStoreLocation"&gt;

&lt;value&gt;/WEB-INF/cache/application&lt;/value&gt;

&lt;/property&gt;

&lt;/bean&gt;

&lt;!--SESSION缓存--&gt;

&lt;bean id="ehSessionCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean"&gt;

&lt;property name="cacheManager"&gt;

&lt;ref local="cacheManager"/&gt;

&lt;/property&gt;

&lt;property name="cacheName"&gt;

&lt;value&gt;com.jeecms.common.web.Session&lt;/value&gt;

&lt;/property&gt;

&lt;qualifier value="session"/&gt;

&lt;/bean&gt;

&lt;!--内容计数缓存--&gt;

&lt;bean id="ehContentCountCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean"&gt;

&lt;property name="cacheManager"&gt;

&lt;ref local="cacheManager"/&gt;

&lt;/property&gt;

&lt;property name="cacheName"&gt;

&lt;value&gt;com.jeecms.cms.front.ContentCount&lt;/value&gt;

&lt;/property&gt;

&lt;qualifier value="contentCount"/&gt;

&lt;/bean&gt;

 

&lt;!--站点流量缓存--&gt;

&lt;bean id="ehCmsSiteFlowCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean"&gt;

&lt;property name="cacheManager"&gt;

&lt;ref local="cacheManager"/&gt;

&lt;/property&gt;

&lt;property name="cacheName"&gt;

&lt;value&gt;com.jeecms.cms.front.CmsSiteFlow&lt;/value&gt;

&lt;/property&gt;

&lt;qualifier value="cmsSiteFlow"/&gt;

&lt;/bean&gt;

&lt;/beans&gt;

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&lt;CacheManager&gt;,

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".

 * &lt;p&gt;

 * 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配置文件中不要配置&lt;diskStore path=""/&gt;，否则本设置无效。

 * 

 * @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&lt;? extends CacheManager&gt; 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();

}

}

 

&lt;!--EndFragment--&gt;