1.spring的配置文件中添加
<bean id="functionPointEHCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<!-- <ref local="ehCacheManagerForFunctionPoint"/> -->
<ref local="ehCacheManager"/>
</property>
<property name="cacheName">
<value>CLAIM_USER_FUNCPOINT</value>
</property>
</bean>
<bean id="functionPointCacheService" class="com.pca.claims.common.cache.FunctionPointCacheService">
<property name="functionPointEHCache" ref="functionPointEHCache" />
<property name="functionPointsService" ref="functionPointsService" />
</bean>
2.ehcache.xml配置文件中添加定义
<cache name="CLAIM_USER_FUNCPOINT" maxElementsInMemory="1000"
eternal="false" timeToIdleSeconds="600" timeToLiveSeconds="600"
overflowToDisk="false" />
3.接下来就可以在service中获取cache的值
/**
* Copyright (C) 2010 PCA
*/
package com.pca.claims.common.cache;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javassist.bytecode.Descriptor.Iterator;
import net.sf.ehcache.Cache;
import com.pca.claims.common.codetype.CodeEntry;
import com.pca.claims.commonfunc.web.FunctionPointSearchCriteria;
import com.pca.claims.integration.vo.FunctionPointValuesDTO;
import com.pca.claims.security.bo.FunctionPointsService;
public class FunctionPointCacheService{
private FunctionPointsService functionPointsService;
private Cache functionPointEHCache;
private ICache<String, Set<String>> functionPointCache;
public FunctionPointsService getFunctionPointsService() {
return functionPointsService;
}
public void setFunctionPointsService(FunctionPointsService functionPointsService) {
this.functionPointsService = functionPointsService;
}
private ICache<String, Set<String>> getfunctionPointValuesCache(){
return functionPointCache;
}
public void setFunctionPointEHCache(Cache functionPointEHCache) {
this.functionPointEHCache = functionPointEHCache;
functionPointCache = new EhcacheWrapper<String,Set<String>>(this.functionPointEHCache);
}
//If there are nothing in the Cache,then put the input into Cache and return the data;
private void putFunctionPointToChache(String userName, Set<String> funcPoints) {
getfunctionPointValuesCache().put(userName, funcPoints);
}
//Get the function from the Cache;
public Set<String> getFunctionPointByUserName(String userName) {
//search in the EHCASH first
Set<String> functionPointValues =getfunctionPointValuesCache().get(userName);
//if there are no date in the cache,search in the Datebase and put them into Cache
if(functionPointValues == null){
functionPointValues = new HashSet<String>();
Set<CodeEntry> allfunctionPonintList = functionPointsService.getFunctionPointsByUser(userName);
for(java.util.Iterator<CodeEntry> it = allfunctionPonintList.iterator(); it.hasNext();) {
CodeEntry entry = it.next();
functionPointValues.add(entry.getCodeId());
}
putFunctionPointToChache(userName, functionPointValues);
}
return functionPointValues;
}
//remove the all Cache
public void removeCache(){
this.functionPointEHCache.removeAll();
}
//remove some parts of Cache by useerId
public void removePartOfCacheByUserName(String userName){
this.functionPointEHCache.remove(userName);
}
}