zoukankan      html  css  js  c++  java
  • SpringMVC集成缓存框架Ehcache

      

      在互联网应用中,应用并发比传统企业及应用会高出很多。解决并发的根本在于系统的响应时间与单位时间的吞吐量。思路可分为:一减少系统的不必要开支(如缓存),二是提高系统单位时间内的运算效率(如集群)。 在硬件资源一定的情况下,在软件层面上解决高并发会比较经济实惠一些。缓存又分为客户端缓存(web浏览器)与服务器缓存;常用的比较流行的服务器缓存框架如Ehcache。下面针对最近学习的Ehcache缓存做一下介绍。

    一、ehcache需要引入包

    <!--ehcache 相关包 -->
    <dependency>
        <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
        <version>2.7.5</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.ehcache-spring-annotations</groupId>
        <artifactId>ehcache-spring-annotations</artifactId>
        <version>1.2.0</version>
    </dependency>

    二、配置applicationContext-ehcache.xml和ehcache.xml文件

    <?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:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
        
        <bean id="cacheManagerFactory" 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="cacheManagerFactory"/>
        </bean>
        
        <!-- 开启spring缓存 -->
        <cache:annotation-driven cache-manager="cacheManager" />
    
    </beans>
    ehcache配置文件

    必须导入导入命名空间

    xmlns:cache="http://www.springframework.org/schema/cache"

    http://www.springframework.org/schema/cache

    http://www.springframework.org/schema/cache/spring-cache-4.1.xsd

    以及ehcache缓存管理器及开启;

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- updateCheck:是否检查当前使用的Ehcache的版本 -->
    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
        monitoring="autodetect" dynamicConfig="true">
    
        <!-- 缓存到磁盘路径 -->
        <diskStore path="d:/cache" />
        <!--   
        eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期
        maxElementsInMemory:缓存中允许创建的最大对象数
        timeToIdleSeconds:缓存数据的钝化时间,也就是在一个元素消亡之前,两次访问时间的最大时间间隔值,这只能在元素不是永久驻留时有效,如果该值是 0 就意味着元素可以停顿无穷长的时间。
        timeToLiveSeconds:缓存数据的生存时间,也就是一个元素从构建到消亡的最大时间间隔值,这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。
        memoryStoreEvictionPolicy:缓存满了之后的淘汰算法。
            1 FIFO,先进先出
            2 LFU,最少被使用,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。
            3 LRU,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
        -->
        
        <!-- 默认缓存 -->
        <defaultCache maxElementsInMemory="10000" eternal="false"
            timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
            maxElementsOnDisk="10000000" diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />
        
        <!-- 自定义缓存 -->
        <cache name="baseCache" maxElementsInMemory="200"
            maxElementsOnDisk="1000" eternal="false" overflowToDisk="true"
            diskSpoolBufferSizeMB="20" timeToIdleSeconds="300" timeToLiveSeconds="600"
            memoryStoreEvictionPolicy="LFU" />
    </ehcache>
    ehcache.xml

    一般使用默认的缓存defaultCache,根据缓存的配置不同,可自定义缓存如文件中的baseCache。

    三、ehcache缓存的使用

    1、使用@Cacheable注解类缓存数据

      @Cacheable注解可以用在方法或者类级别,用在方法上,仅仅缓存此方法的返回值; 用在类上,针对该类所有的方法的返回值缓存。下面展示方法级别缓存,,

      当缓存中没有该对象的时候,当然要从数据库里面访问了,从数据库查出来之后,缓存管理器会将此对象放到缓存中,下一次访问的时候,只要该对象没有消亡则会从缓存里取,不会再查询数据库。

      @Cacheable注解包含三个参数 @Cacheable(value,key,condition);  

      value:我们自定义缓存的name,将被缓存的位置;

      key :任何存储在缓存中的数据为了高速访问都需要一个key。spring默认使用被@Cacheable注解的方法的签名来作为key,当然你可以重写key,自定义key可以使用SpEL表达式。key的值可是使用 key = "#page" SpEl表达式,也可以使用 'String' 字符形式。

      condition:也使用SpEl表达式,用条件控制是否缓存。

     2、@CacheEvict 这个注解的作用就是当数据发生变化的时候(增删改)清除缓存,做到数据同步

     参数value对应的默认缓存或者自定义缓存;key 对应哪个缓存

     

    lift is made up of small pleasures. 生活是由各种微小的幸福构成。 日积月累,就会产生意想不到的Miracles。 每一天的坚持,每一天的收获,我与你同在!!
  • 相关阅读:
    BZOJ3674 可持久化并查集加强版
    BZOJ2588 Spoj 10628. Count on a tree
    BZOJ1300 [LLH邀请赛]大数计算器
    BZOJ3261 最大异或和
    BZOJ1605 [Usaco2008 Open]Crisis on the Farm 牧场危机
    BZOJ3524 [Poi2014]Couriers
    BZOJ2127 happiness
    JZOJ__Day 7:【普及模拟】蚂蚁
    JZOJ__Day 6:【普及模拟】神奇的项链(fett)
    JZOJ__Day 6:【普及模拟】团队背包(team)
  • 原文地址:https://www.cnblogs.com/drubber/p/6198393.html
Copyright © 2011-2022 走看看