zoukankan      html  css  js  c++  java
  • Sprng ecache

    Ehcache是一种广泛使用的开源java分布式缓存,它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持REST和SOAP api等特点。

    主要的特性有:
    1. 快速
    2. 简单
    3. 多种缓存策略
    4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题
    5. 缓存数据会在虚拟机重启的过程中写入磁盘
    6. 可以通过RMI、可插入API等方式进行分布式缓存
    7. 具有缓存和缓存管理器的侦听接口
    8. 支持多缓存管理器实例,以及一个实例的多个缓存区域
    9. 提供Hibernate的缓存实现

    1. 依赖

    <dependency>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache</artifactId>
                <version>2.7.4</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
            </dependency>

    2. xml文件中增加命名空间

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

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

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

    3. 添加如下声明

    <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->
        <cache:annotation-driven cache-manager="cacheManager" />
        <!-- cacheManager工厂类,指定ehcache.xml的位置 -->
        <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
            p:config-location="classpath:ehcache.xml"
            p:shared="true"
             />
        <!-- 声明cacheManager -->
        <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
            p:cache-manager-ref="ehcache" />

    4. ehcache.xml

    <?xml version="1.0" encoding="UTF-8"?>

    <ehcache name="functionRestCache" updateCheck="false">
        <!--  
            diskStore path:用来配置磁盘缓存使用的物理路径  
            name:   缓存名称,cache的唯一标识(ehcache会把这个cache放到HashMap里)  
            eternal="false"   元素是否永恒,如果是就永不过期(必须设置)  
            maxElementsOnDisk====磁盘缓存中最多可以存放的元素数量,0表示无穷大   
            maxElementsInMemory="1000" 内存缓存中最多可以存放的元素数量(必须设置)  
            timeToIdleSeconds="0"   导致元素过期的访问间隔(秒为单位). 0表示可以永远空闲,默认为0  
            timeToLiveSeconds="600" 元素在缓存里存在的时间(秒为单位). 0 表示永远存在不过期  
            overflowToDisk="false"  当缓存达到maxElementsInMemory值是,是否允许溢出到磁盘(必须设置)  
            diskPersistent="false"  磁盘缓存在VM重新启动时是否保持(默认为false)  
            diskExpiryThreadIntervalSeconds="100" 磁盘失效线程运行时间间隔,默认是120秒  
            memoryStoreEvictionPolicy="LFU" 内存存储与释放策略.当达到maxElementsInMemory时  
                   共有三种策略,分别为LRU(最近最少使用)、LFU(最常用的)、FIFO(先进先出)默认使用"最近使用"策略  
        -->  
        <cache name="cache5s" eternal="false" maxElementsInMemory="9999"
            overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
            timeToLiveSeconds="5" memoryStoreEvictionPolicy="LFU" />

        <cache name="cache1m" eternal="false" maxElementsInMemory="9999"
            overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
            timeToLiveSeconds="60" memoryStoreEvictionPolicy="LFU" />

        <cache name="cache5m" eternal="false" maxElementsInMemory="19999"
            overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
            timeToLiveSeconds="300" memoryStoreEvictionPolicy="LFU" />
            
        <cache name="cache30m" eternal="false" maxElementsInMemory="19999"
            overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
            timeToLiveSeconds="1800" memoryStoreEvictionPolicy="LFU" />

    </ehcache>

    5. 常见异常

    spring错误-在spring里面找不到org.springframework.cache.ehcache.EhCacheManagerFactoryBean

    缺少spring-context-support-3.2.0.jar这个jar包

    Caused by: net.sf.ehcache.CacheException: Another CacheManager with same name '' already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
    1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
    2. Shutdown the earlier cacheManager before creating new one with same name.
    The source of the existing CacheManager is: DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ]

    6. @Cacheable支持参数

    value:缓存位置名称,不能为空,就是ehcache.xml中声明的cache的name

    key:缓存的key,默认为空,既表示使用方法的参数类型及参数值作为key,支持SpEL

    condition:触发条件,只有满足条件的情况才会加入缓存,默认为空,既表示全部都加入缓存,支持SpEL

    unless: 否定,符合条件不加入缓存 unless = "#result.getReturnCode() != 200" result为方法返回结果对象

     调用一次,过期时间内再次调用不走方法内部.

    注解存储(可以使用方法中的参数)

    代码添加获取

  • 相关阅读:
    序列化组件
    restful_framework之视图组件
    如何优化MYSQL数据库
    pycharm如何显示工具栏
    cmd常用命令
    pycharm如何回到过去某个时间
    RESTful API设计规范
    MyBatis之传入参数——parameterType(转)
    Spring3事务管理——使用@Transactional 注解(转)
    Eclipse 启动问题:'Initilizing Java Tooling' has encountered a problem(。。。)
  • 原文地址:https://www.cnblogs.com/yangfei-beijing/p/6204072.html
Copyright © 2011-2022 走看看