Spring - Ehcache RMI方式集群 自动发现配置

1,首先jar包引入

<!-- ehcache 相关jar包   **beging** -->

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-api</artifactId>

<version>1.7.10</version>

</dependency>

<dependency>

<groupId>com.googlecode.ehcache-spring-annotations</groupId>

<artifactId>ehcache-spring-annotations</artifactId>

<version>1.2.0</version>

</dependency>

<dependency>

<groupId>net.sf.ehcache</groupId>

<artifactId>ehcache-core</artifactId>

<version>2.6.9</version>

</dependency>

<!-- ehcache 相关jar包   **end** 已经在上边添加过-->


            另外还有 spring-context-support ,log4j

2,ehcache.xml中配置如下:

    

<?xml version="1.0" encoding="UTF-8"?>  

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"> 

   <!--  

   当内存缓存中对象数量超过maxElementsInMemory时,将缓存对象写到磁盘缓存中

   用来配置磁盘缓存使用的物理路径

   部署前,需要手动配置

     -->

    <diskStore path="java.io.tmpdir/ehcache"/>

    

    

<!-- 

属性详解:

name :缓存名称,cache的唯一标识;

maxElementsOnDisk:磁盘缓存中最多可以存放的元素数量,0表示无穷大;

maxElementsInMemory:内存缓存中最多可以存放的元素数量,若放入Cache中的元素超过这个数值,则有以下两种情况:

1)若overflowToDisk=true,则会将Cache中多出的元素放入磁盘文件中

2)若overflowToDisk=false,则根据memoryStoreEvictionPolicy策略替换Cache中原有的元素;

Eternal:缓存中对象是否永久有效,即是否永驻内存,true时将忽略timeToIdleSeconds和timeToLiveSeconds;

timeToIdleSeconds:缓存数据在失效前的允许闲置时间(单位:秒),仅当eternal=false时使用,默认值是0表示可闲置时间无穷大,

此为可选属性即访问这个cache中元素的最大间隔时间,若超过这个时间没有访问此Cache中的某个元素,

那么此元素将被从Cache中清除。

timeToLiveSeconds:缓存数据在失效前的允许存活时间(单位:秒),仅当eternal=false时使用,默认值是0表示可存活时间无穷大,

即Cache中的某元素从创建到清楚的生存时间,也就是说从创建开始计时,

当超过这个时间时,此元素将从Cache中清除。

overflowToDisk:内存不足时,是否启用磁盘缓存(即内存中对象数量达到maxElementsInMemory时,Ehcache会将对象写到磁盘中),

会根据标签中path值查找对应的属性值,写入磁盘的文件会放在path文件夹下,文件的名称是cache的名称,后缀名是data

diskPersistent:是否持久化磁盘缓存,当这个属性的值为true时,系统在初始化时会在磁盘中查找文件名为cache名称,后缀名为index的文件,

这个文件中存放了已经持久化在磁盘中的cache的index,找到后会把cache加载到内存,要想把cache真正持久化到磁盘,

写程序时注意执行net.sf.ehcache.Cache.put(Element element)后要调用flush()方法。

diskExpiryThreadIntervalSeconds:磁盘缓存的清理线程运行间隔,默认是120秒。


diskSpoolBufferSizeMB:设置DiskStore(磁盘缓存)的缓存区大小,默认是30MB


memoryStoreEvictionPolicy:内存存储与释放策略,即达到maxElementsInMemory限制时,Ehcache会根据指定策略清理内存,

共有三种策略,分别为LRU(最近最少使用)、LFU(最常用的)、FIFO(先进先出)。

-->   

<!--  缓存1 --> 

<!-- 

需要注意的是当一个支持缓存的方法在对象内部被调用时是不会触发缓存功能的

如何理解?

自己当前的理解:由于ehcache支持在类上添加缓存注解,故,用当前注解缓存类访问 其内部的方法的时候,缓存失效

但是,当,,,, 

-->  

    

    

    

    

    <!--EHCache分布式缓存集群环境配置-->  

  <!--

  rmi 自动配置

  多播( multicast )来维护集群中的所有有效节点。这也是最为简单而且灵活的方式,与手工模式不同的是,每个节点上的配置信息都相同;

  其中需要指定节点发现模式 peerDiscovery 值为 automatic 自动;同时组播地址可以指定 D 类 IP 地址空间,范围从 224.0.1.0 到 238.255.255.255 中的任何一个地址

  -->

  <cacheManagerPeerProviderFactory

   class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"

   properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1,

   multicastGroupPort=4446, timeToLive=32"

/>


    <cacheManagerPeerListenerFactory  

            class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"  

            properties="hostName=localhost,port=40001, socketTimeoutMillis=120000"/>  

            

    <defaultCache  

            maxElementsInMemory="10000"  

            eternal="false"  

            timeToIdleSeconds="120"  

            timeToLiveSeconds="120"  

            overflowToDisk="true"  

            diskSpoolBufferSizeMB="30"  

            maxElementsOnDisk="10000000"  

            diskPersistent="false"  

            diskExpiryThreadIntervalSeconds="120"  

            memoryStoreEvictionPolicy="LRU"> 

         <!-- 

         配置Replicator是同步还是异步的,并配置Put、Update、Remove等哪些事件需要同步 

         另外:最简单的配置方式,EventListener的所有属性都采用默认值,即:

         <cacheEventListenerFactory  

                class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>

        --> 

        <cacheEventListenerFactory 

class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" 

properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,

replicateUpdatesViaCopy=false, replicateRemovals=true "/>  

    </defaultCache> 

     

    <cache name="commonDataCache"  

           maxElementsInMemory="1000"  

           eternal="false"  

           timeToIdleSeconds="100000"  

           timeToLiveSeconds="100000"  

           overflowToDisk="false">  

         <!-- 

         配置Replicator是同步还是异步的,并配置Put、Update、Remove等哪些事件需要同步 

         另外:最简单的配置方式,EventListener的所有属性都采用默认值,即:

         <cacheEventListenerFactory  

                class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>

        --> 

        <cacheEventListenerFactory 

class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" 

properties="replicateAsynchronously=true, replicatePuts=true, replicateUpdates=true,

replicateUpdatesViaCopy=false, replicateRemovals=true "/>   

    </cache> 

</ehcache>


3,ehcache-context.xml中配置如下:


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:context="http://www.springframework.org/schema/context" 

    xmlns:cache="http://www.springframework.org/schema/cache"

    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-3.0.xsd

        http://www.springframework.org/schema/cache

        http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">

    

    <cache:annotation-driven cache-manager="cacheManager"/>

    

    <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">

     <!-- 加载ehcache 的配置文件【参数配置】 -->

        <property name="configLocation" value="classpath:config/ehcache/ehcache.xml" />

    </bean>

    

    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">    

        <property name="cacheManager"  ref="cacheManagerFactory"/>    

    </bean>

 

</beans>


4,然后,在ApplicationContext.xml中 引入配置文件 ehcache-context.xml,


5,给方法,类上添加缓存注解标签

@CacheEvict(value="menuCache",key="'UserMenuKey'+#userRoleBean.getUserId()")

public boolean delUserRole(UserRoleBean userRoleBean) {

……

}

另外:注意:

<cache:annotation-driven/>还可以指定一个mode属性,可选值有proxyaspectj。默认是使用proxymodeproxy时,只有缓存方法在外部被调用的时候Spring Cache才会发生作用,也就是当一个支持缓存的方法在对象内部被调用时是不会触发缓存功能的。

使用proxy时,只有public方法上的@Cacheable等标注才会起作用,如果需要非public方法上的方法也可以使用Spring Cache时把mode设置为aspectj。


转载于:https://my.oschina.net/tinglanrmb32/blog/489113

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值