zoukankan      html  css  js  c++  java
  • Spring MVC 使用Ehcache作为缓存的例子

    一)POM文件

    pom.xml

    <dependency>
    		<groupId>net.sf.ehcache</groupId>
    		<artifactId>ehcache</artifactId>
    		<version>2.9.0</version>
    	</dependency>
    
            <!-- Optional, to log stuff -->
    	<dependency>
    		<groupId>ch.qos.logback</groupId>
    		<artifactId>logback-classic</artifactId>
    		<version>1.0.13</version>
    	</dependency>
    
    	<!-- Spring caching framework inside this -->
    	<dependency>
    		<groupId>org.springframework</groupId>
    		<artifactId>spring-context</artifactId>
    		<version>4.1.4.RELEASE</version>
    	</dependency>
    
    	<!-- Support for Ehcache and others -->
    	<dependency>
    		<groupId>org.springframework</groupId>
    		<artifactId>spring-context-support</artifactId>
    		<version>4.1.4.RELEASE</version>
    	</dependency>
    </project>
    

    二)配置文件
    注意cache name,后面的service要用到

    <ehcache>
    <diskStore path="java.io.tmpdir"/>
    <defaultCache
            maxElementsInMemory="1000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="false"/>
    <cache name="cache" 
           maxElementsOnDisk="20000"
           maxElementsInMemory="2000"
           eternal="true"
           overflowToDisk="true"
           diskPersistent="true"/>
    </ehcache>
    

    Spring的配置文件applicationContext.xml

        <cache:annotation-driven cache-manager="cacheManager"/>
    
        <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
            <property name="configLocation" value="classpath:ehcache.xml"/>
        </bean>
    
        <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
            <property name="cacheManager" ref="ehcacheManager"/>
            <property name="transactionAware" value="true"/>
        </bean>
    

    三)Service
    Cacheable:
    当读取的时候会从缓存中找数据,如果没有,那么就从数据库中获取,注意value的值,要和EHcache配置文件的cache名字相同
    CacheEvict:
    当执行这个方法的时候删除缓存。

        @Override
        @Cacheable(value="cache")
        public Map<String, List<CodeListCache>> getCodeListCache() {
            List<CCodelistLine> listCodeListLine = this.mapper.selectDataForCache();
            for (CCodelistLine item:listCodeListLine){
                CodeListCache codeListCache = new CodeListCache();
                codeListCache.setCode(item.getCode());
                codeListCache.setCodeValue(item.getCodeValue());
                codeListCache.setCodeText1(item.getCodeText1());
                codeListCache.setCodeText2(item.getCodeText2());
                codeListCache.setCodeText3(item.getCodeText3());
                if(!cacheMap.containsKey(item.getCode())){
                    cacheMap.put(item.getCode(),new ArrayList<>());
                }
                cacheMap.get(item.getCode()).add(codeListCache);
            }
            return cacheMap;
        }
    
        @CacheEvict(value="cache")
        public void updateCodeList(CCodelistLine codelistLine){
            if(codelistLine.getId() != null){
                this.mapper.update(codelistLine);
            }else {
                this.mapper.update(codelistLine);
            }
        }
        @CacheEvict(value="cache")
        public void deleteCodeList(String[] ids){
            this.mapper.deleteByPrimaryKeys(ids);
        }
    

    OK,整个配置过程就这么完成。

  • 相关阅读:
    echarts中如何使用timeline组件
    vs发布项目webconfig替换语法
    [置顶] MVC输出缓存(OutputCache参数详解)
    signalr中Group 分组群发消息的简单使用
    echarts异步数据加载(在下拉框选择事件中异步更新数据)
    自定义bootstrap样式-9行样式自定义漂亮大气bootstrap导航栏
    OpenCvSharp 图像旋转
    mybatis获取刚刚插入到数据库的数据的id(转载)
    axios 发 post 请求,后端接收不到参数的解决方案(转载)
    sql 时间获取
  • 原文地址:https://www.cnblogs.com/whsongblog/p/7906866.html
Copyright © 2011-2022 走看看