springboot 配置Ehcache

Ehcache的基本配置说明我就不说了.小编记录一下在springboot中使用Ehcache的使用方法.

第一步:在classpath下引入配置文件ehcache.xml

代码如下:

复制代码

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:noNamespaceSchemaLocation="ehcache.xsd">  
    <cache name="demo"  
           maxEntriesLocalHeap="200"  
           timeToLiveSeconds="600">  
    </cache>  
</ehcache> 

复制代码

 

第二步springboot开启对缓存的支持,你需要在springboot启动的main方法上配置@EnableCaching注解即可

 

第三步就是代码使用demo了.代码如下:

首先我们建一个实体类:

复制代码

public class Thing {

    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
    
    
}

复制代码

然后我们注入一个service,模拟数据crud

复制代码

@Service
public class CacheDemoServiceImpl {

    private static Map<Long, Thing> data = new HashMap<>();//用与缓存模拟数据

    /**
     * 缓存的key
     */
    public static final String THING_ALL_KEY = "\"thing_all\"";
    /**
     * value属性表示使用哪个缓存策略,缓存策略在ehcache.xml
     */
    public static final String DEMO_CACHE_NAME = "demo";

    @CacheEvict(value = DEMO_CACHE_NAME, key = THING_ALL_KEY)
    public void create(Thing thing) {
        thing.setId(thing.getId());
        data.put(thing.getId(), thing);
    }

    @Cacheable(value = DEMO_CACHE_NAME, key = "#thing.id")
    public Thing findById(Thing thing) {
        Long id=thing.getId();
        System.err.println("没有走缓存!" + id);
        return data.get(id);
    }

    @Cacheable(value = DEMO_CACHE_NAME, key = THING_ALL_KEY)
    public List<Thing> findAll() {
        return Lists.newArrayList(data.values());
    }

    @CachePut(value = DEMO_CACHE_NAME, key = "#thing.id")
    @CacheEvict(value = DEMO_CACHE_NAME, key = THING_ALL_KEY)
    public Thing update(Thing thing) {
        System.out.println(thing);
        data.put(thing.getId(), thing);
        return thing;
    }

    @CacheEvict(value = DEMO_CACHE_NAME)
    public void delete(Long id) {
        data.remove(id);
    }

}

复制代码

最后我们建立一个控制层来访问数据做测试:

复制代码

    @Autowired
    private CacheDemoServiceImpl cacheDemoServiceImpl;
    
    @RequestMapping("/test/add")
    public void test(@NotNull Long id) {
        Thing t=new Thing();
        t.setId(id);
        cacheDemoServiceImpl.create(t);
    
    }
    
    @RequestMapping("/test/list")
    public JsonResult testlist() {
        List<Thing> list=cacheDemoServiceImpl.findAll();
        return result(200,"",list);
    }
    
    @RequestMapping("/test/one")
    public JsonResult testfind(@NotNull Long id) {
        Thing t=new Thing();
        t.setId(id);
        Thing tt=cacheDemoServiceImpl.findById(t);
        return result(200,"测试缓存",tt);
    
    }
    
    @RequestMapping("/test/delete")
    public void testdelete(@NotNull Long id) {
        cacheDemoServiceImpl.delete(id);
    
    }

复制代码

 

先执行/test/add, 然后/test/list,其次/test/one,你最后会发现的/test/one 当参数传入相同的时候时,数据是从缓存中拿了.

 

付:下面是springboot不要Ehcache配置文件的注入方法:

复制代码

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.CacheResolver;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.cache.interceptor.SimpleKeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import net.sf.ehcache.config.CacheConfiguration;

@Configuration
@EnableCaching
public class CachingConfiguration implements CachingConfigurer {
    @Bean(destroyMethod="shutdown")
    public net.sf.ehcache.CacheManager ehCacheManager() {
        CacheConfiguration cacheConfiguration = new CacheConfiguration();
        cacheConfiguration.setName("demo");
        cacheConfiguration.setMemoryStoreEvictionPolicy("LRU");
        cacheConfiguration.setMaxEntriesLocalHeap(1000);
        net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();
        config.addCache(cacheConfiguration);
        return net.sf.ehcache.CacheManager.newInstance(config);
    }

    @Bean
    @Override
    public CacheManager cacheManager() {
        return new EhCacheCacheManager(ehCacheManager());
    }

    @Bean
    @Override
    public KeyGenerator keyGenerator() {
        return new SimpleKeyGenerator();
    }

    @Override
    public CacheResolver cacheResolver() {
        
        return null;
    }

    @Override
    public CacheErrorHandler errorHandler() {
        return null;
    }
    
}

复制代码

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值