`

spring缓存 ehcache

阅读更多

          ehcache 与spring集成实现简单的数据集缓存,事例是在ssh集成上实现,不对ssh做阐述。

 

1 : lib追加jar包

       ehcache-1.2.4.jar

 

2 : applicationContext-service.xml

      <bean id="GatewayIPManager"                          class="cn.com.superv.netmessage.oam.web.manager.GatewayIPManager">

        <property name="transactionManager">

            <ref bean="transactionManager" />

        </property>

        <property name="interProtocolDAO">

            <ref bean="InterProtocolDAO" />

        </property>

        <!-- 缓存对象-->

        <property name="gatewayIPCache" ref="GatewayIPManagerBean"/>

    </bean>

3 : applicationContext-cache.xml  缓存事例配置文件

     <?xml version="1.0"?>

     <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

        "http://www.springframework.org/dtd/spring-beans.dtd">

 

      <beans>

<!-- 缓存管理 -->

      <bean id="cacheManager"

         class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">

         <property name="configLocation"

            value="WEB-INF/ehcache.xml">

         </property>

       </bean>

 

       <bean id="GatewayIPManagerBean"       class="org.springframework.cache.ehcache.EhCacheFactoryBean">

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

          <property name="cacheName" value="GatewayIPManager"></property>

        </bean>

      </beans>

 

4  : WEB-INF中添加ehcache.xml 缓冲对象属性配置 
        <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="ehcache.xsd">
          <diskStore path="java.io.tmpdir"/>
          <!-- 
          maxElementsInMemory:  最大缓存100个对象 
                    eternal:不是永久有效
             overflowToDisk:缓存满了不写的磁盘
          timeToIdleSeconds:180秒后没有读就失效
          timeToLiveSeconds:300秒后失效
          memoryStoreEvictionPolicy:当缓存数达到最大,将移除使用最少的缓存对象
          -->
         <!-- 默认缓存设置 -->
        <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="180"
            timeToLiveSeconds="300"
            overflowToDisk="true"
            memoryStoreEvictionPolicy="LRU"
            />
         <!-- 缓存设置 -->
        <cache name="GatewayIPManager"
           maxElementsInMemory="100"
           eternal="false"
           overflowToDisk="false"
           timeToIdleSeconds="300"
           timeToLiveSeconds="600"
           memoryStoreEvictionPolicy="LFU"
           />
        </ehcache>


5  : GatewayIPManager class编写
        private Cache gatewayIPCache ; 
public void setGatewayIPCache(Cache gatewayIPCache) {
this.gatewayIPCache = gatewayIPCache;
}
private InterProtocolDAO interProtocolDAO ;
    
public void setInterProtocolDAO(InterProtocolDAO interProtocolDAO) {
this.interProtocolDAO = interProtocolDAO;
}


       public boolean isExist(String wapip){
   
        if (StringUtils.isEmpty(wapip)){
         throw new NullPointerException("param 'wapid' can't be null ! ");
     }
        List<String> wapIPList = null ;
       //如果缓存对象不存在,去数据库中获取
           Element element = gatewayIPCache.get(wapip);
           if (element == null) {
               wapIPList = interProtocolDAO.getInterProtocolsByType(InterProtocol.WAP_IP);
               element = new Element(wapip, wapIPList);
               gatewayIPCache.put(element);
               if (log.isInfoEnabled()) {
                   log.info("----------数据列表加载到缓存中-------------");
               }
            }
            wapIPList = (List<String>) element.getValue();
        return wapIPList.contains(wapip);
   
         }


         
 
1
0
分享到:
评论

相关推荐

    spring缓存ehcache

    Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该方法参数和返回结果作为一个键值对存放在缓存中,等到下次利用同样的参数来调用该方法时将不再执行该方法,而是直接从缓存中...

    spring + ehcache + redis两级缓存

    spring + ehcache + redis两级缓存

    Spring+EhCache缓存实例

    Spring+EhCache缓存实例

    Spring+Ehcache集成

    基于公司的项目在Spring中集成Ehcache,并提供EhcaheUtils工具类,并通过Spring的AOP编程实现方法缓存注解话,先奉献出核心代码,需要的朋友可以参考哦!

    Spring与ehcache结合使用

    Spring与ehcache结合使用,本地缓存的实现

    spring-ehcache-redis两级缓存

    两级缓存在redis的方案上做一步优化,在缓存到远程redis的同时,缓存一份到本地进程ehcache(此处的ehcache不用做集群,避免组播带来的开销),取缓存的时候会先取本地,没有会向redis请求,这样会减少应用服务器&lt;–...

    Spring 与Ehcache实现基于方法的缓存

    NULL 博文链接:https://liuyunlong1229.iteye.com/blog/2081421

    Ehcache分布式缓存与其在SpringBoot应用

    EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。Ehcache 是一种广泛使用的开源 Java 分布式缓存。主要面向通用缓存,Java EE 和轻量级容器。它具有内存和...

    整合spring 和ehcache

    配置ehcache缓存,存储内存的设置,与spring 的整合等

    springmvc4+spring4+hibernate5.1.3+二级缓存ehcache+fastjson配置

    此配置和包,是springMVC4.3.3 +spring4+hibernate5.1.3+二级缓存ehcache(不用可以关闭)+fastjson。 是正常运行的项目里拷出来的,方便大家使用。

    Spring AOP+ehCache简单缓存系统解决方案

    需要使用Spring来实现一个Cache简单的解决方案,具体需求如下:使用任意一个现有开源Cache Framework,要求可以Cache系统中Service或则DAO层的get/find等方法返回结果,如果数据更新(使用Create/update/delete方法...

    spring+ehcache实例demo

    EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。 Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,...

    Struts2+Spring+Hibernate+Ehcache+AJAX+JQuery+Oracle 框架集成用户登录注册Demo工程

    1.通过google ehcache-spring-annotatios.jar自动注解方式实现整合Spring+Ehcache。 2.Action里通过struts2-spring-plugin.jar插件自动根据名字注入。 3.Ajax无刷新异步调用Struts2,返回Json数据,以用户注册为例。...

    Spring 缓存

    介绍spring与ehcache缓存结合,使用spring注解访问,使用guava包中的缓存,封装guava缓存,为应用程序使用。类似于自定义的方式

    Ehcache 整合Spring 使用页面、对象缓存

    Ehcache 整合Spring 使用页面、对象缓存

    spring整合ehcache的完整用例

    这是一个spring整合ehcache的完整用例,导入包后直接可以运行。ehcache.jar要导入1.2以上的,否则会报错。

    spring整合ehCache

    spring整合了ehcache,实现了缓存功能。附带读取properties文件信息功能。

    Hibernate4二级缓存Ehcache案例

    Hibernate4二级缓存Ehcache案例,可参考博客:http://blog.csdn.net/coco2d_x2014/article/details/52927638

    Spring+ehcache整合

    Spring4.1+ehcache2.10.2实现缓存功能!

    Spring+EHcache缓存实例详解

    主要为大家详细介绍了Spring+EHcache缓存实例,EhCache是一个纯Java的进程内缓存框架,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

Global site tag (gtag.js) - Google Analytics