美文网首页
EhCache缓存

EhCache缓存

作者: Jorvi | 来源:发表于2018-08-07 19:58 被阅读0次

EhCache+Spring实现缓存

参考:
https://blog.csdn.net/u012106290/article/details/52154241
https://www.cnblogs.com/mxmbk/articles/5162813.html

首先,需要知道:
Spring本身没有实现缓存解决方案,但是对缓存管理功能提供了声明式的支持,能够与多种流行的缓存实现进行集成。

Spring内置多种缓存管理器,例如:SimpleCacheManager、EhCacheCacheManager、RedisCacheManager等。虽然底层的缓存方案各不相同,但是Spring声明缓存的方式没有什么差别(注解驱动缓存和XML配置缓存)。

Spring缓存机制:当我们在调用一个缓存方法时会把该方法参数和返回结果作为一个键值存放在缓存中,等到下次利用同样的参数调用该方法时将直接从缓存中获取结果。


示例

  1. ehcache的缓存配置(ehcache.xml)
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
  <!-- 磁盘缓存位置 -->
  <diskStore path="java.io.tmpdir" />

  <!-- 默认缓存 -->
  <defaultCache 
      maxElementsInMemory="1000" 
      eternal="false" 
      timeToIdleSeconds="120" 
      timeToLiveSeconds="120"
      overflowToDisk="false" />
    
  <!-- 自定义缓存 -->
  <cache 
      name="optionResourceCache" 
      maxElementsInMemory="1000" 
      maxElementsOnDisk="1000" 
      eternal="false" 
      timeToIdleSeconds="300"
      timeToLiveSeconds="300" 
      overflowToDisk="false" 
      memoryStoreEvictionPolicy="LRU" />
</ehcache>
  • name:缓存名称。
  • maxElementsInMemory:内存中最大缓存对象数。
  • maxElementsOnDisk:硬盘中最大缓存对象数,若是0表示无穷大。
  • eternal:true表示对象永不过期,此时忽略timeToIdleSeconds和timeToLiveSeconds属性,默认为false。
  • timeToIdleSeconds:设定允许对象处于空闲状态的最长时间,以秒为单位。当对象自从最近一次被访问后,如果处于空闲状态的时间超过了设定值,这个对象就会过期。如果该属性值为0,则表示对象可以无限期地处于空闲状态。
  • timeToLiveSeconds:设定对象允许存在于缓存中的最长时间,以秒为单位。当对象自从被存放到缓存中后,如果处于缓存中的时间超过了设定值,这个对象就会过期。如果该属性值为0,则表示对象可以无限期地存在于缓存中。timeToLiveSeconds必须大于等于timeToIdleSeconds属性,才有意义。
  • overflowToDisk:true表示当内存缓存的对象数目达到了maxElementsInMemory界限后,会把溢出的对象写到硬盘缓存中。注意:如果缓存的对象要写入到硬盘中的话,则该对象必须实现了Serializable接口才行。
  • memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。可选策略有:LRU(最近最少使用,默认策略)、FIFO(先进先出)、LFU(最少访问次数)。
  1. Spring配置ehcache(spring.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:cache="http://www.springframework.org/schema/cache"
  xsi:schemaLocation="
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
  ">

  <!-- 开启注解功能 -->
  <cache:annotation-driven cache-manager="myCacheManager" />
  <!-- cache manager -->
  <bean id="myCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager" ref="myCacheManagerFactory"></property>
  </bean>
  <!-- cache manager Factory -->
  <bean id="myCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:/com/ehcache.xml" />
  </bean>

</beans>
  1. 利用注解使用缓存
public class OptionResourceService {
   /**
    * 查询数据并缓存
    */
    @Cacheable(value = "optionResourceCache", key = "#spaceName + '-' + #fieldName")
    public OptionResourceVO getField(String spaceName, String fieldName) {
        // 查询数据库获取数据
        OptionResourceVO result = dao.getField(spaceName,fieldName);
        return result;
    }
}

注解中:
value="optionResourceCache"必须与ehcache.xml中配置的缓存名保持一致,key="#spaceName + '-' + #fieldName" 为缓存的key,
函数的返回值OptionResourceVO为缓存的value。

第一次调用该方法时,会去查询数据库并将结果加入缓存(K-V),下次调用该方法时会先去缓存中根据key查找是否存在记录,如果存在直接返回结果,否则查询数据库并加入缓存。

相关文章

网友评论

      本文标题:EhCache缓存

      本文链接:https://www.haomeiwen.com/subject/bfoavftx.html