ehcache访问

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);
 }
 
 
      
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值