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,整个配置过程就这么完成。

  • 相关阅读:
    jTopo——js库
    node.js
    php 入门笔记
    D3 入门笔记
    webpack笔记
    React.js
    Grunt等前端自动化构建工具
    vue3.0的新特性
    electron-builder 打包流程
    vue里面如何下载图片,如何下载文件
  • 原文地址:https://www.cnblogs.com/whsongblog/p/7906866.html
Copyright © 2011-2022 走看看