wzh123

博客园 首页 新随笔 联系 订阅 管理

   首先讲讲EhCache。在默认情况下,即在用户未提供自身配置文件ehcache.xml或ehcache-failsafe.xml时,EhCache会依据其自身Jar存档包含的ehcache-failsafe.xml文件所定制的策略来管理缓存。如果用户在classpath下提供了ehcache.xml或ehcache-failsafe.xml文件,那么EhCache将会应用这个文件。如果两个文件同时提供,那么EhCache会使用ehcache.xml文件的配置。EhCache内容如下:

Xml代码 复制代码 收藏代码
  1. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  2.  xsi:noNamespaceSchemaLocation="ehcache.xsd">  
  3.   
  4.     <diskStore path="C:\Acegi6" />       
  5.        
  6.     <defaultCache  
  7.             maxElementsInMemory="10000"  
  8.             eternal="false"  
  9.             timeToIdleSeconds="120"  
  10.             timeToLiveSeconds="120"  
  11.             overflowToDisk="true"  
  12.             maxElementsOnDisk="10000000"  
  13.             diskPersistent="false"  
  14.             diskExpiryThreadIntervalSeconds="120"  
  15.             memoryStoreEvictionPolicy="LRU"  />               
  16.                
  17.     <cache name="cacheAcegi"  
  18.            maxElementsInMemory="1"  
  19.            maxElementsOnDisk="1000"  
  20.            eternal="false"  
  21.            overflowToDisk="true"  
  22.            timeToIdleSeconds="300"  
  23.            timeToLiveSeconds="600"  
  24.            memoryStoreEvictionPolicy="FIFO"  
  25.             />  
  26. </ehcache>  
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:noNamespaceSchemaLocation="ehcache.xsd">

    <diskStore path="C:\Acegi6" />    
    
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            maxElementsOnDisk="10000000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"  />            
            
    <cache name="cacheAcegi"
           maxElementsInMemory="1"
           maxElementsOnDisk="1000"
           eternal="false"
           overflowToDisk="true"
           timeToIdleSeconds="300"
           timeToLiveSeconds="600"
           memoryStoreEvictionPolicy="FIFO"
            />
</ehcache>

 

属性说明:
 diskStore:指定数据在磁盘中的存储位置。
 defaultCache:默认的缓存配置。是除制定的Cache外其余所有Cache的设置
以下属性是必须的:
 name - cache的标识符,在一个CacheManager中必须唯一
 maxElementsInMemory - 在内存中缓存的element的最大数目
 maxElementsOnDisk - 在磁盘上缓存的element的最大数目
 eternal - 设定缓存的elements是否有有效期。如果为true,timeouts属性被忽略
 overflowToDisk - 设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上
以下属性是可选的:
 timeToIdleSeconds - 缓存element在过期前的空闲时间。默认为0,表示可空闲无限时间.
(如果指定了这个时间,是否在被hit的前超过了这个时间就会被remove?在内存缓存数目超限之前不会被remove)
 timeToLiveSeconds - 缓存element的有效生命期。这个类似于timeouts,默认为0,不过期 (是否通常情况下应该大于等于timeToIdleSeconds,小于会如何?idle时间也会减小和这个数值一样)
 diskPersistent - 在VM重启的时候是否持久化磁盘缓存,默认是false。(测试一下true的情况?重载vm的时候会从磁盘进行序列化到对象)
 diskExpiryThreadIntervalSeconds - 磁盘缓存的清理线程运行间隔,默认是120秒。(测试一下0的时候会如何)
 memoryStoreEvictionPolicy - 当内存缓存达到最大,有新的element加入的时候,
        移除缓存中element的策略。默认是LRU,可选的有LFU和FIFO
(关于ehcache的更多信息,请访问ehcache的网站 http://ehcache.sourceforge.net

 

Java代码 复制代码 收藏代码
  1. <STRONG>EhCache程序1:</STRONG>   
  2. import net.sf.ehcache.Cache;   
  3. import net.sf.ehcache.CacheManager;   
  4. import net.sf.ehcache.Element;   
  5.   
  6. import org.apache.commons.logging.Log;   
  7. import org.apache.commons.logging.LogFactory;   
  8.   
  9. /**  
  10.  * 缓存管理器中不存在名为demoCache的缓存,所以需要先添加:  
  11. * manager.addCache("demoCache");  
  12.  */  
  13. public class EhCacheTestDemo {   
  14.   
  15.     protected static final Log log = LogFactory.getLog(EhCacheTestDemo.class);   
  16.   
  17.     public static void main(String[] args) {   
  18.            
  19.         CacheManager manager = new CacheManager();   
  20.         manager.addCache("demoCache");   
  21.            
  22.         String[] cacheNames = manager.getCacheNames();         
  23.         for (String cacheName : cacheNames) {   
  24.             log.info("缓存的名字:" + cacheName);   
  25.         }   
  26.            
  27.         //获得缓存   
  28.         Cache cache = manager.getCache("demoCache");   
  29.            
  30.         Element element = new Element("data1""缓存数据1");   
  31.         //往缓存中存放数据,EhCache会依据一定的策略将数据存储到内存或磁盘中   
  32.         cache.put(element);   
  33.         //获得已缓存的数据   
  34.         log.info(cache.get("data1").getValue());   
  35.            
  36.         element = new Element("data2""缓存数据2");           
  37.         cache.put(element);   
  38.         log.info(cache.get("data2").getValue());   
  39.            
  40.         log.info(cache);   
  41.         //打印出内存中已缓存的Element数量   
  42.         log.info(cache.getMemoryStoreSize());          
  43.         //打印出磁盘中已缓存的Element数量   
  44.         log.info(cache.getDiskStoreSize());   
  45.   
  46.         //将“data1”从缓存中销毁掉   
  47.         cache.remove("data1");   
  48.            
  49.         log.info(cache.getMemoryStoreSize());          
  50.         log.info(cache.getDiskStoreSize());   
  51.   
  52.         System.exit(-1);   
  53.     }   
  54.   
  55. }   
  56.   
  57.   
  58. <STRONG>EhCache程序2:</STRONG>   
  59. import net.sf.ehcache.Cache;   
  60. import net.sf.ehcache.CacheManager;   
  61. import net.sf.ehcache.Element;   
  62.   
  63. import org.apache.commons.logging.Log;   
  64. import org.apache.commons.logging.LogFactory;   
  65.   
  66. /**  
  67.  * 配置文件中已存在名称为cacheAcegi的缓存,不用添加到缓存管理器中  
  68.  */  
  69. public class EhCacheTestDemoVersion2 {   
  70.   
  71.     protected static final Log log = LogFactory.getLog(EhCacheTestDemoVersion2.class);   
  72.   
  73.     public static void main(String[] args) {   
  74.            
  75.         CacheManager manager = new CacheManager();   
  76.            
  77.         String[] cacheNames = manager.getCacheNames();         
  78.         for (String cacheName : cacheNames) {   
  79.             log.info("缓存的名字:" + cacheName);   
  80.         }   
  81.            
  82.         //获得缓存   
  83.         Cache cache = manager.getCache("cacheAcegi");   
  84.            
  85.         Element element = new Element("data1""缓存数据1");   
  86.         //往缓存中存放数据,EhCache会依据一定的策略将数据存储到内存或磁盘中   
  87.         cache.put(element);   
  88.         //获得已缓存的数据   
  89.         log.info(cache.get("data1").getValue());   
  90.            
  91.         element = new Element("data2""缓存数据2");           
  92.         cache.put(element);   
  93.         log.info(cache.get("data2").getValue());   
  94.            
  95.         log.info(cache);   
  96.         //打印出内存中已缓存的Element数量   
  97.         log.info(cache.getMemoryStoreSize());          
  98.         //打印出磁盘中已缓存的Element数量   
  99.         log.info(cache.getDiskStoreSize());   
  100.   
  101.         //将“data1”从缓存中销毁掉   
  102.         cache.remove("data1");   
  103.            
  104.         log.info(cache.getMemoryStoreSize());          
  105.         log.info(cache.getDiskStoreSize());   
  106.   
  107.         System.exit(-1);   
  108.     }   
  109.   
  110. }  
EhCache程序1:
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 缓存管理器中不存在名为demoCache的缓存,所以需要先添加:
* manager.addCache("demoCache");
 */
public class EhCacheTestDemo {

	protected static final Log log = LogFactory.getLog(EhCacheTestDemo.class);

	public static void main(String[] args) {
		
		CacheManager manager = new CacheManager();
		manager.addCache("demoCache");
		
		String[] cacheNames = manager.getCacheNames();		
		for (String cacheName : cacheNames) {
			log.info("缓存的名字:" + cacheName);
		}
		
		//获得缓存
		Cache cache = manager.getCache("demoCache");
		
		Element element = new Element("data1", "缓存数据1");
		//往缓存中存放数据,EhCache会依据一定的策略将数据存储到内存或磁盘中
		cache.put(element);
		//获得已缓存的数据
		log.info(cache.get("data1").getValue());
		
		element = new Element("data2", "缓存数据2");		
		cache.put(element);
		log.info(cache.get("data2").getValue());
		
		log.info(cache);
		//打印出内存中已缓存的Element数量
		log.info(cache.getMemoryStoreSize());		
		//打印出磁盘中已缓存的Element数量
		log.info(cache.getDiskStoreSize());

		//将“data1”从缓存中销毁掉
		cache.remove("data1");
		
		log.info(cache.getMemoryStoreSize());		
		log.info(cache.getDiskStoreSize());

		System.exit(-1);
	}

}


EhCache程序2:
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 配置文件中已存在名称为cacheAcegi的缓存,不用添加到缓存管理器中
 */
public class EhCacheTestDemoVersion2 {

	protected static final Log log = LogFactory.getLog(EhCacheTestDemoVersion2.class);

	public static void main(String[] args) {
		
		CacheManager manager = new CacheManager();
		
		String[] cacheNames = manager.getCacheNames();		
		for (String cacheName : cacheNames) {
			log.info("缓存的名字:" + cacheName);
		}
		
		//获得缓存
		Cache cache = manager.getCache("cacheAcegi");
		
		Element element = new Element("data1", "缓存数据1");
		//往缓存中存放数据,EhCache会依据一定的策略将数据存储到内存或磁盘中
		cache.put(element);
		//获得已缓存的数据
		log.info(cache.get("data1").getValue());
		
		element = new Element("data2", "缓存数据2");		
		cache.put(element);
		log.info(cache.get("data2").getValue());
		
		log.info(cache);
		//打印出内存中已缓存的Element数量
		log.info(cache.getMemoryStoreSize());		
		//打印出磁盘中已缓存的Element数量
		log.info(cache.getDiskStoreSize());

		//将“data1”从缓存中销毁掉
		cache.remove("data1");
		
		log.info(cache.getMemoryStoreSize());		
		log.info(cache.getDiskStoreSize());

		System.exit(-1);
	}

}

 

 

Spring EhCache集成引入Acegi

      每次当请求一个受保护的资源时,认证管理器就被调用以获取用户的安全信息。但如果获取用户信息涉及到查询数据库,每次都查询相同的数据可能在性能上表现得很糟糕。注意到用户信息不会频繁改变,也许更好的做法是在第一次查询时缓存用户信息,并在后续的查询中直接从缓存中获取用户信息。
      DaoAuthenticationProvider通过org.acegisecurity.providers.dao.UserCache接口的实现类支持对用户信息进行缓存。
public interface UserCache {
    public abstract UserDetails getUserFromCache(String s);
    public abstract void putUserInCache(UserDetails userdetails);
    public abstract void removeUserFromCache(String s);
}

      顾名思义,接口UserCache中方法提供了向缓存中放入、取得和删除用户明细信息的功能。我们可以写一个自己的UserCache实现类,实现对用户信息的缓存。然而,在你考虑开发自己的UserCache实现类之前,应该首先考虑Acegi提供的两个方便的UserCache实现类:
 org.acegisecurity.providers.dao.cache.NullUserCache
 org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache

      NullUserCache事实上不进行任何缓存。任何时候调用它的getUserFromCache方法,得到的返回值都是null。这是DaoAuthenticationProvider使用的默认UserCache实现。
public class NullUserCache implements UserCache {
    public NullUserCache() {}
    public UserDetails getUserFromCache(String username)  { return null; }
    public void putUserInCache(UserDetails userdetails) {}
    public void removeUserFromCache(String s) {}
}

      EhCacheBasedUserCache是一个更实用的缓存实现。类如其名,它是基于开源项目ehcache实现的。ehcache是一个简单快速的针对Java的缓存解决方案,同时也是Hibernate默认的和推荐的缓存方案。

Acegi配置如下:

Xml代码 复制代码 收藏代码
    1. <bean id="daoAuthenticationProvider"    
    2.         class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">  
    3.       ......   
    4.       <!-- 增加 -->  
    5.       <property name="userCache"><ref local="userCache"/></property>  
    6.    </bean>  
    7.   
    8. <!-- EhCacheBasedUserCache是EhCache的一个缓存实现,提供了向缓存中放入、取得和删除用户明细信息的功能,Acegi需要用它来管理缓存。 -->  
    9. <bean id="userCache" class="org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache">  
    10.       <property name="cache" ref="userCacheBackend" />  
    11.    </bean>  
    12.   
    13. <!-- EhCacheFactoryBean是用于维护Cache实例的工厂Bean,Cache需要依赖于CacheManager而存在 -->  
    14.    <bean id="userCacheBackend"    
    15.         class="org.springframework.cache.ehcache.EhCacheFactoryBean">  
    16.       <property name="cacheManager" ref="cacheManager" />  
    17.       <property name="cacheName" value="userCache" />! 缓存名称    
    18.    </bean>  
    19.   
    20.    <!-- 缓存管理器,一个CacheManager能够创建和维护多个Cache实例 -->  
    21.    <bean id="cacheManager"    
    22.         class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" />  
posted on 2013-10-28 19:59  wzh123  阅读(230)  评论(0编辑  收藏  举报