spring3 + ehcache 采用annotation的使用方式说明文档

此文是根据几篇文章综合一起产生的总结!

* ehcache的配置 : 详细的配置文件中的各参数说明见附件里面的  ehcache.xml

在src目录下建立文件: ehcache.xml
ehcache一般常用的配置两种:1. 一般情况下的配置   2. 使用RMI集群的配置

1. 一般情况下的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">            
   <cache name="home.Group"
            maxElementsInMemory="4"
            eternal="false"
            timeToIdleSeconds="15"
            timeToLiveSeconds="0"
            overflowToDisk="false" 
            diskPersistent="false"           
/> 
 </ehcache>

2. 使用RMI集群的配置 : IP广播形式的,优点每个服务都同样配置
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=automatic,multicastGroupAddress=230.0.0.1,multicastGroupPort=4446, timeToLive=32"
propertySeparator=","/>
<cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"/>  
    <cache  name="home.Group"
            maxElementsInMemory="4"
            eternal="false"
            timeToIdleSeconds="15"
            timeToLiveSeconds="0"
            overflowToDisk="false" 
            diskPersistent="false"           
            > 
    <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
    properties="replicateAsynchronously=true,
    replicatePuts=true,
    replicateUpdates=true,
    replicateUpdatesViaCopy=false,
    replicateRemovals=true"/>
    </cache>              
</ehcache>

------------------------------------------------------------------------------------------------------------
另外还有一种,IP端口被固定的,所谓的手工配置
server1:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
<!-- 采用手工配置 -->
<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,rmiUrls=//localhost:40002/com.autonavi.home.Group" />

<cacheManagerPeerListenerFactory  class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"   
properties="hostName=localhost,port=40001,socketTimeoutMillis=2000"/>
  
    <cache  name="home.Group"
            maxElementsInMemory="4"
            eternal="false"
            timeToIdleSeconds="15"
            timeToLiveSeconds="0"
            overflowToDisk="false" 
            diskPersistent="false"           
            > 
    <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
    properties="replicateAsynchronously=true,
    replicatePuts=true,
    replicateUpdates=true,
    replicateUpdatesViaCopy=false,
    replicateRemovals=true"/>
    </cache>              
</ehcache>

server2:相应的就要修改IP与端口,因为在集群中它们必须唯一

<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,rmiUrls=//localhost:40001/com.autonavi.home.Group" />

<cacheManagerPeerListenerFactory  class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"   
properties="hostName=localhost,port=40002,socketTimeoutMillis=2000"/>

* spring里面的配置,采用annotation的方式进行缓存的处理

<!-- 开启包扫描 -->
<context:component-scan base-package="cacheOfAnno" />

<!-- 缓存配置 -->

<cache:annotation-driven/>            
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:ehcache.xml" p:shared="false" />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="cacheManagerFactory" />

* JAVA 代码实例
 
 package cacheOfAnno; 
 import org.springframework.cache.annotation.CacheEvict; 
 import org.springframework.cache.annotation.Cacheable; 

 public class AccountService { 
   @Cacheable(value="home.Group")// 使用了一个缓存名叫 home.Group , 与ehcache.xml里面的名字匹配 
   public Account getAccountByName(String userName) { 
     // 方法内部实现不考虑缓存逻辑,直接实现业务
     System.out.println("real query account."+userName); 
     return getFromDB(userName); 
   } 
  
   private Account getFromDB(String acctName) { 
     System.out.println("real querying db..."+acctName); 
     return new Account(acctName); 
   } 
 } 

// 上述代码的说明: getAccountByName 方法上有一个注释 annotation,即 @Cacheable(value=”home.Group”),这个注释的意思是,当调用这个// 方法的时候,会从一个名叫 home.Group 的缓存中查询,如果没有,则执行实际的方法(即查询数据库),并将执行的结果存入缓存中,否则返// 回缓存中的对象。这里缓存中的 key 就是参数 userName,value 就是返回值 Account 对象。
// 注意:有多个参数时,key采用的就是这些参数组合而成的 hashcode 码 ,在这样的情形下你的参数是自己定义的类,可能需要实现 hashcode.

@CacheEvict(value="home.Group",key="#account.getName()")   //清空 accountCache 缓存
   public void updateAccount(Account account) {
     updateDB(account); 
   } 

private void updateDB(Account account) { 
     System.out.println("real update db..."+account.getName()); 
   } 

// 上段代码调用此方法时,将清空缓存里面key 为account.getName()的缓存对象,并把更新保存到数据库

 @CachePut(value="home.Group",key="#account.getName()")// 更新 accountCache 缓存
 public Account updateAccount2(Account account) { 
    return updateDB(account); 
 }

 private Account updateDB2(Account account) { 
   System.out.println("real updating db..."+account.getName()); 
   return account; 
 } 

 // 上段代码调用此方法时,把更新保存到数据库,并能把更新后的结果缓存

 // 注意:如果程序代码中对缓存有更复杂的处理,只能把缓存注入到类里面,用代码实现你想要的缓存情形与逻辑过程
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值