Ehcache缓存的使用

Ehcache缓存的使用

pom坐标

  <dependency>
   <groupId>net.sf.ehcache</groupId>
   <artifactId>ehcache</artifactId>
   <version>2.8.3</version>
  </dependency>

ehcache.xml 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
    updateCheck="false">
    <diskStore path="java.io.tmpdir/Tmp_EhCache" />
    
    <!-- defaultCache,是默认的缓存策略 -->
    <!-- 如果你指定的缓存策略没有找到,那么就用这个默认的缓存策略 -->
    <!-- external:缓存对象是否一直存在,如果设置为true的话,那么timeout就没有效果,缓存就会一直存在,一般默认就是false -->
    <!-- maxElementsInMemory:内存中可以缓存多少个缓存条目 -->
    <!-- overflowToDisk:如果内存不够的时候,是否溢出到磁盘 -->
    <!-- diskPersistent:是否启用磁盘持久化的机制,在jvm崩溃的时候和重启之间 -->
    <!-- timeToIdleSeconds:对象最大的闲置的时间,如果超出闲置的时间,可能就会过期  单位:秒 当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大-->
    <!-- timeToLiveSeconds:对象最多存活的时间  单位:秒 当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是存活时间无穷大-->
    <!-- memoryStoreEvictionPolicy:当缓存数量达到了最大的指定条目数的时候,需要采用一定的算法,从缓存中清除一批数据,LRU,最近最少使用算法,最近一段时间内,最少使用的那些数据,就被干掉了 -->
    <defaultCache
        eternal="false"
        maxElementsInMemory="1000"
        overflowToDisk="false"
        diskPersistent="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="0"
        memoryStoreEvictionPolicy="LRU" />
 
  <!-- 手动指定的缓存策略 -->
  <!-- 对不同的数据,缓存策略可以在这里配置多种 -->
    <cache
        name="local"  
        eternal="false"
        maxElementsInMemory="1000"
        overflowToDisk="false"
        diskPersistent="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="0"
        memoryStoreEvictionPolicy="LRU" />    
</ehcache>

测试读写

     import net.sf.ehcache.Cache;
     import net.sf.ehcache.CacheManager;
     import net.sf.ehcache.Element;
    
     @org.junit.Test  
     public void testEhcache (){  
      CacheManager create = CacheManager.create(this.getClass().getResourceAsStream("/ehcache.xml"));
      Cache cache = create.getCache("local");
      cache.put(new Element("key1", "value1"));
      Element element = cache.get("key1");
      System.out.println(element.getObjectKey()+" : "+element.getObjectValue());
     }

springboot集成Ehcache

EhcacheConfiguration.java

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
 
@Configuration
@EnableCaching
public class EhcacheConfiguration {
 @Bean
 public EhCacheManagerFactoryBean cacheManagerFactoryBean(){
  EhCacheManagerFactoryBean bean = new EhCacheManagerFactoryBean();
  bean.setConfigLocation(new ClassPathResource("ehcache.xml"));
  bean.setShared(true);
  return bean;
 }
 @Bean
    public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean bean){
      return new EhCacheCacheManager(bean.getObject());
    } 
}

使用

@Service
public class EhcahceServiceImpl implements EhcahceService {
 
 private static final String CACHE_STRATEGY = "local";
 
 @CachePut(value=CACHE_STRATEGY,key="#root.methodName+#info.getProduct_id()")
 @Override
 public ProductInfo saveProductInfo(ProductInfo info) throws Exception {
  return info;
 }
 //@CacheEvict(value="myCache", key="'get'+#userNo")  
 //allEntries为true表示清除value中的全部缓存,默认为false  
 // @CacheEvict(value="myCache", allEntries=true)  
 @Cacheable(value=CACHE_STRATEGY,key="#root.methodName+#id")
 @Override
 public ProductInfo getProductInfoById(Long id) throws Exception {
  return null;
 }}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值