spring中缓存配置(完善)

1.引入依赖包

<dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache-core</artifactId>
        <version>2.5.1</version>
    </dependency>

 

2.配置

以下配置是用spring自定义的一个简单的缓存管理器

    <cache:annotation-driven/>

    <!-- generic cache manager --> 
   <bean id="cacheManager" 
   class="org.springframework.cache.support.SimpleCacheManager">
     <property name="caches"> 
       <set> 
         <bean 
           class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
           p:name="default" /> 
         <bean 
           class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
           p:name="nurseCache" /> 
       </set> 
     </property> 
   </bean>

 

可以用第三方的 如ehcache    ehcache.xml配置

 

<?xml version="1.0"?> 
<ehcache xmlms:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="ehcache.xsd"> 
    <diskStore path="cache" /> 
<!--     <defaultCache maxElementsInMemory="10000" eternal="false" --> 
<!--         timeToIdleSeconds="30" timeToLiveSeconds="30" overflowToDisk="false" /> --> 
    <cache name="nurseCache" maxElementsInMemory="10000" eternal="false" 
        timeToIdleSeconds="300" timeToLiveSeconds="10" overflowToDisk="false" 
        memoryStoreEvictionPolicy="LFU" /> 
    <cache name="bdHospitalCache" maxElementsInMemory="10000" eternal="false" 
        timeToIdleSeconds="300" timeToLiveSeconds="10" overflowToDisk="false" 
        memoryStoreEvictionPolicy="LFU" /> 
    <cache name="dicCache" maxElementsInMemory="10000" eternal="false" 
        timeToIdleSeconds="300" timeToLiveSeconds="360000" overflowToDisk="false" 
        memoryStoreEvictionPolicy="LFU" /> 
    <cache name="bannerCache" maxElementsInMemory="10000" eternal="false" 
        timeToIdleSeconds="300" timeToLiveSeconds="36000" overflowToDisk="false" 
        memoryStoreEvictionPolicy="LFU" /> 
    <cache name="dicItemCache" maxElementsInMemory="10000" eternal="false" 
        timeToIdleSeconds="300" timeToLiveSeconds="10" overflowToDisk="false" 
        memoryStoreEvictionPolicy="LFU" /> 
    <cache name="queryDicById" maxElementsInMemory="10000" eternal="false" 
        timeToIdleSeconds="300" timeToLiveSeconds="10" overflowToDisk="false" 
        memoryStoreEvictionPolicy="LFU" /> 
    <cache name="getCustomerInfo" maxElementsInMemory="10000" eternal="false" 
        timeToIdleSeconds="300" timeToLiveSeconds="10" overflowToDisk="false" 
        memoryStoreEvictionPolicy="LFU" /> 
</ehcache>

spring文件配置

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

  <!-- cacheManager工厂类,指定ehcache.xml的位置 --> 
    <bean id="ehcacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> 
         <property name="configLocation" value="classpath:ehcache.xml" /> 
    </bean> 
    <!-- 声明cacheManager --> 
    <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> 
         <property name="cacheManager" ref="ehcacheManagerFactory" /> 
    </bean>

 

 

3.编码

//存到缓存中的key值是查询参数

    @Cacheable(value="nurseCache",key="#idItems")
    public List<Nurse> queryNurseListByIdCol(String idItems) {
        System.out.println("查询数据库");
        System.out.println(idItems);
        if(idItems == null || "".equals(idItems)){
            return null;
        }


        String url = apiQueryNursesByIds+"?idItems="+idItems;
        String result = HttpUtil.sendGet(url, "utf-8");
        System.out.println(result);
        HttpNurseListResult resultList = JSON.parseObject(result, HttpNurseListResult.class);
        if(resultList == null){
            return null;
        }
        int success = resultList.getSuccess();
        if(success != 1){
            return null;
        }

        return resultList.getData();
    }

多值组合key,比如下面例子代码中的bannerDtoRpc对象中的city属性与position属性联合组成key

    @Cacheable(value="bannerCache",key="#bannerDtoRpc.city.concat(T(String).valueOf(#bannerDtoRpc.position))") 
    public List<BannerRpc> queryCustomerBanners(BannerDtoRpc bannerDtoRpc) { 
        System.out.println("实际查询"); 

        return bannerRpcDao.queryCustomerBanners(bannerDtoRpc); 
    }

 

 

 

4.管理缓存

 

在使用缓存时,如果我们对数据增,删,改操作时,这个时候需要及时的清理相应key里面的缓存,

在spring中可以直接用 @CacheEvict 注解,因为我们的缓存并没有独立开来,例如用redis来管理缓存

这个时候对数据的增,删,改可能在其他系统操作,则不能用这个注解,可以通过编码的方式对外提供缓存

清理的接口

 

private CacheManager ehcacheManager; 

    @Override 
    public ResponseVoRpc clearCache(@Param("cacheName")String cacheName,@Param("request")HttpServletRequest request) { 
        ResponseVoRpc resp = new ResponseVoRpc(); 

         ServletContext servletContext = request.getServletContext();  
         WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);               
         ehcacheManager = (CacheManager)ctx.getBean("ehcacheManager" );  

        Cache cache = ehcacheManager.getCache(cacheName); 
        if(cache==null){ 
            resp.setCode(ResponseVoRpc.CODE_COMMON_SUCCESS); 
            resp.setCodeMsg(ResponseVoRpc.CODE_MAP.get(ResponseVoRpc.CODE_COMMON_SUCCESS)); 
            return resp; 
        } 
        cache.clear(); 
        resp.setCode(ResponseVoRpc.CODE_COMMON_SUCCESS); 
        resp.setCodeMsg(ResponseVoRpc.CODE_MAP.get(ResponseVoRpc.CODE_COMMON_SUCCESS)); 
        return resp; 
    }

注意:

5.redis管理缓存

 

http://blog.csdn.net/yang7678287/article/details/50844479

 

--------------

spring mvc+tomcat源码分析视频 (复制链接在浏览器打开)

https://study.163.com/course/courseMain.htm?share=2&shareId=480000001919582&courseId=1209399899&_trace_c_p_k2_=6d81bc445e9c462ab8d6345e40f6b0bf

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值