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

  • 相关阅读:
    对OpenCV学习笔记(1)遗留问题的思考
    转:争论32bit/64bit的人都搞错了方向,需要分清楚IA64和x64
    Win8_64bit+VS2012下的OpenCV学习笔记(1)
    pikachu练习平台-不安全的文件下载
    pikachu练习平台-文件包含漏洞(Files Inclusion)
    pikachu练习平台-RCE(远程系统命令、代码执行)
    pikachu练习平台(SQL注入 )
    pikachu练习平台(CSRF(跨站请求伪造) )
    pikachu练习平台(XSS-漏洞测试案例(cookie的窃取和利用、钓鱼攻击、XSS获取键盘记录))
    pikachu练习平台(XSS(跨站脚本))
  • 原文地址:https://www.cnblogs.com/whsongblog/p/7906866.html
Copyright © 2020-2023  润新知