• springmvc和encache集成


    虽然目前已经出了 ehcache3.x 了,但是,结合我搜索到的资料,我依旧只能先采用 ehcache2.x 来使用了

    首先,在pom 中引入jar

    <dependency>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache</artifactId>
                <version>2.10.4</version>
            </dependency>

    引入 ehcache2 的配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <ehcache updateCheck="false" name="defaultCache">
        <diskStore path="../temp/ehcache" />  <!--  <diskStore path="java.io.tmpdir" />  -->
        <!-- 默认缓存配置. -->
        <defaultCache maxEntriesLocalHeap="100" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600"
            overflowToDisk="true" maxEntriesLocalDisk="100000" />
        
        <!-- 系统缓存 -->
        <cache name="sysCache" maxEntriesLocalHeap="100" eternal="true" overflowToDisk="true"/>
    </ehcache>

    在spring 的配置文件中 spring-context 中引入

    <?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:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd 
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd">
        
        <!-- 当前这个配置文件,是没有用到的,保留是为了方便查阅  -->
        
        <!-- 缓存配置 -->  
        <!-- 启用缓存注解功能(请将其配置在Spring主配置文件中) -->  
    <!--     <cache:annotation-driven cache-manager="cacheManager"/>   -->
        <!-- Spring自己的基于java.util.concurrent.ConcurrentHashMap实现的缓存管理器(该功能是从Spring3.1开始提供的) -->  
        <!--   
        <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">  
            <property name="caches">  
                <set>  
                    <bean name="myCache" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"/>  
                </set>  
            </property>  
        </bean>  
         -->  
        <!-- 若只想使用Spring自身提供的缓存器,则注释掉下面的两个关于Ehcache配置的bean,并启用上面的SimpleCacheManager即可 -->  
        <!-- Spring提供的基于的Ehcache实现的缓存管理器 -->  
            <!-- 缓存配置 -->
        <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
            <property name="configLocation" value="classpath:ehcache.xml" />
        </bean>  
    <!--     <bean id="shiroCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">  
            <property name="cacheManager" ref="cacheManagerFactory"/>  
        </bean> -->
        <bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
            <property name="cacheManager" ref="cacheManager"/>
        </bean>
    
    </beans>  

    我这里使用了 shiro,为了 shiro 和 ehcache 结合,引入 shiro-ehcache 包

    CacheUtils  采用工具类的模式,在需要缓存的地方,手动加入或移除缓存

    /**
     * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
     */
    package com.thinkgem.jeesite.common.utils;
    
    import net.sf.ehcache.Cache;
    import net.sf.ehcache.CacheManager;
    import net.sf.ehcache.Element;
    
    /**
     * Cache工具类
     * @author ThinkGem
     * @version 2013-5-29
     */
    public class CacheUtils {
        
        private static CacheManager cacheManager = ((CacheManager)SpringContextHolder.getBean("cacheManager"));
    
        private static final String SYS_CACHE = "sysCache";
    
        /**
         * 获取SYS_CACHE缓存
         * @param key
         * @return
         */
        public static Object get(String key) {
            return get(SYS_CACHE, key);
        }
        
        /**
         * 写入SYS_CACHE缓存
         * @param key
         * @return
         */
        public static void put(String key, Object value) {
            put(SYS_CACHE, key, value);
        }
        
        /**
         * 从SYS_CACHE缓存中移除
         * @param key
         * @return
         */
        public static void remove(String key) {
            remove(SYS_CACHE, key);
        }
        
        /**
         * 获取缓存
         * @param cacheName
         * @param key
         * @return
         */
        public static Object get(String cacheName, String key) {
            Element element = getCache(cacheName).get(key);
            return element==null?null:element.getObjectValue();
        }
    
        /**
         * 写入缓存
         * @param cacheName
         * @param key
         * @param value
         */
        public static void put(String cacheName, String key, Object value) {
            Element element = new Element(key, value);
            getCache(cacheName).put(element);
        }
    
        /**
         * 从缓存中移除
         * @param cacheName
         * @param key
         */
        public static void remove(String cacheName, String key) {
            getCache(cacheName).remove(key);
        }
        
        /**
         * 获得一个Cache,没有则创建一个。
         * @param cacheName
         * @return
         */
        private static Cache getCache(String cacheName){
            Cache cache = cacheManager.getCache(cacheName);
            if (cache == null){
                cacheManager.addCache(cacheName);
                cache = cacheManager.getCache(cacheName);
                cache.getCacheConfiguration().setEternal(true);
            }
            return cache;
        }
    
        public static CacheManager getCacheManager() {
            return cacheManager;
        }
        
    }

    手动加入和移除呢,还是太麻烦。

    其实还有 spring 与 ehcache 结合的包,可以在方法上加注解的方式。参考博客:http://blog.csdn.net/u011068702/article/details/47780033

    /**  
     * ehcache-spring-annotations简介  
     * @see ----------------------------------------------------------------------------------------------------------------  
     * @see 关于Spring与Ehcache的集成,googlecode上有一个经Apache认证的开源项目叫做ehcache-spring-annotations  
     * @see 目前已经到了1.2.0版本,它只是简化了Spring和Ehcache集成的复杂性(事实上我觉得完全没必要,因为它俩集成并不复杂)  
     * @see 尽管如此还是要提一下,它的项目主页为https://code.google.com/p/ehcache-spring-annotations/  
     * @see 这篇文章中描述了其用法http://blog.goyello.com/2010/07/29/quick-start-with-ehcache-annotations-for-spring/  
     * @see ----------------------------------------------------------------------------------------------------------------  
     * @see 1)使用时在项目中引入ehcache-spring-annotations-1.2.0.jar和guava-r09.jar两个jar文件  
     * @see 2)然后在applicationContext.xml按照如下配置  
     * @see   <beans xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"  
     * @see         xsi:schemaLocation="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring  
     * @see         http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.2.xsd">  
     * @see   <ehcache:annotation-driven/>  
     * @see   <ehcache:config cache-manager="cacheManager">  
     * @see       <ehcache:evict-expired-elements interval="60"/>  
     * @see   </ehcache:config>  
     * @see   <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
     * @see       <property name="configLocation" value="classpath:ehcache.xml"/>  
     * @see   </bean>  
     * @see 3)最后在需要缓存的方法上使用@Cacheable和@TriggersRemove注解即可  
     * @see   经我亲测,@TriggersRemove(cacheName="..", when="..", removeAll=true)可移除缓存中的全部对象  
     * @see   但若写成@TriggersRemove(cacheName="..", when="..")则不会移除缓存中的单一的或所有的对象,即缓存中的对象无变化  
     * @see ----------------------------------------------------------------------------------------------------------------  
     * @create Oct 3, 2013 4:56:35 PM  
     * @author 玄玉<http://blog.csdn.net/jadyer>  
     */  

    另外针对每一个方法上写一遍缓存也是够繁琐的,是否有法子采用AOP 的方式来注入缓存呢

    比如 方法名为 getCacheXXX 或者 findCacheXXX 则需要缓存

    update 就移除缓存 之类

    等我哪天有心情研究下

    -----------

    Spring4整合Ehcache2.10 :http://blog.csdn.net/frankcheng5143/article/details/50776542

    spring+EnCache缓存示例 :http://blog.csdn.net/zjt1388/article/details/51810270 

    http://blog.csdn.net/frankcheng5143/article/details/50791757

  • 相关阅读:
    Android使用静默安装时碰见的问题
    Android 在Android代码中执行命令行
    android SystemServer.java启动的服务。
    Android AndroidRuntime类
    学习C的笔记
    Java虚拟机 JVM
    Android 关于ijkplayer
    Android 算法 关于递归和二分法的小算法
    Android 死锁和重入锁
    JAVA的内存模型(变量的同步)
  • 原文地址:https://www.cnblogs.com/panie2015/p/7867232.html
Copyright © 2020-2023  润新知