spring+ehCache简单整合使用示例

阅读更多
下面介绍一下简单使用的配置过程:ehcache.jar及spring相关jar就不说了,加到项目中就是了。

简单的使用真的很简单。但只能做为入门级了。

1.ehcache.xml,可放classpath根目录下,
         dynamicConfig="true">
<      diskStore path="java.io.tmpdir" />
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            diskSpoolBufferSizeMB="30"
            maxElementsOnDisk="10000000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
          
            />
                maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            diskSpoolBufferSizeMB="30"
            maxElementsOnDisk="10000000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
           
            />    



 

2.第二步,配置applicationContext-ehcache.xml,与spring整合文件


     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context.xsd 
          http://www.springframework.org/schema/tx  
          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
          http://www.springframework.org/schema/aop 
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
          http://www.springframework.org/schema/mvc 
          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
          default-autowire="byName" default-lazy-init="false"> 
            
            
              
            classpath:ehcache.xml    
        
    
       
 
         
              
            
              
                
         
    
              
              DEFAULT_CACHE    
         
    
       
           
   
 

实际上这样就把两者结合起来了。当然集群的话还得另外配置,这里只讲最简单的。

下面使用:

3.  添加数据到缓存:

net.sf.ehcache.Cache ehCache=ApplicationContextUtils.getBean("ehCache");
net.sf.ehcache.Element lgEle=new net.sf.ehcache.Element("loginName", users.getLoginName());
net.sf.ehcache.Element pwdEle=new net.sf.ehcache.Element("password", users.getPassword());
ehCache.put(lgEle);
ehCache.put(pwdEle);

这样使用就可。

当然,在spring管理的bean中,也可:

private Cache  ehCache; 
     
    @Resource(name="ehCache") 
    public void setEhCache(Cache ehCache) { 
        this.ehCache = ehCache; 
    } 
4.使用。

这个其实就不用说了,大家都会了,我相信,能过对应的key值去获取就是了。

如: Element lgEle= ehCache.get("loginName");
需要修改,就先取得再修改,删除就直接删除。



  • ehcache.xml.rar (1.4 KB)
  • 下载次数: 196

你可能感兴趣的:(spring+ehCache简单整合使用示例)