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;
} }
每天就进步一点点就可以了...不要想太多
参考:http://www.cnblogs.com/lic309/p/4072848.html
springboot 配置Ehcache的更多相关文章
- 【spring-boot】spring-boot 整合 ehcache 实现缓存机制
方式一:老 不推荐 参考:https://www.cnblogs.com/lic309/p/4072848.html /*************************第一种 引入 ehcach ...
- SpringBoot配置属性之NOSQL
SpringBoot配置属性系列 SpringBoot配置属性之MVC SpringBoot配置属性之Server SpringBoot配置属性之DataSource SpringBoot配置属性之N ...
- spring boot学习(十三)SpringBoot缓存(EhCache 2.x 篇)
SpringBoot 缓存(EhCache 2.x 篇) SpringBoot 缓存 在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManag ...
- springboot整合Ehcache
首先引入maven包: <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...
- 转载-Springboot整合ehcache缓存
转载:https://www.cnblogs.com/xzmiyx/p/9897623.html EhCache是一个比较成熟的Java缓存框架,最早从hibernate发展而来, 是进程中的缓存系统 ...
- Springcloud 中 SpringBoot 配置全集 (收藏版)
Springcloud 中 SpringBoot 配置全集 (收藏版) 疯狂创客圈 Java 高并发[ 亿级流量聊天室实战]实战系列 [博客园总入口 ] 前言 疯狂创客圈(笔者尼恩创建的高并发研习社群 ...
- Springboot使用ehcache缓存
本文部分步骤继承于springboot使用cache缓存,如果有不清楚的,请移驾springboot使用cache缓存 ehcache是一种广泛使用的开源Java分布式缓存.主要面向通用缓存,Java ...
- Springboot + Mybatis + Ehcache
最近在做一个项目,为处理并发性较差的问题,使用了Mybatis二级缓存 但在多表联合查询的情况下,Mybatis二级缓存是存在着数据脏读的问题的 两天就是在想办法解决这个数据脏读的问题 考虑到简易性. ...
- 【Springboot】Springboot整合Ehcache
刚刚项目上线了,记录下使用的技术...... EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. Ehcache的特点 ( ...
随机推荐
- 前端遇上Go: 静态资源增量更新的新实践
前端遇上Go: 静态资源增量更新的新实践https://mp.weixin.qq.com/s/hCqQW1F8FngPPGZAisAWUg 前端遇上Go: 静态资源增量更新的新实践 原创: 洋河 美团 ...
- 您可能试图从server上的安全浏览器訪问此站点。请启用脚本然后又一次载入此页。
您可能试图从server上的安全浏览器訪问此站点.请启用脚本然后又一次载入此页. 我使用域Admin组的账户登入SharePoint2010的server,打开SharePoint首页 ...
- Swift(一)简单值
Swift的源文件扩展名是.swift 按照国际惯例,学习一门新语言写的第一个程序都是在屏幕上输出一句 “Hello, world!” .在Swift里,一行代码就搞定了: 如果你以前写过C或者Obj ...
- codeforces 399B. Red and Blue Balls 解题报告
题目链接:http://codeforces.com/problemset/problem/399/B 题目意思:给出 n 个只由 R 和 B 组成的字符串(由上到下排列,相当于栈),问最多可以操作多 ...
- codeforces 443 B. Kolya and Tandem Repeat 解题报告
题目链接:http://codeforces.com/contest/443/problem/B 题目意思:给出一个只有小写字母的字符串s(假设长度为len),在其后可以添加 k 个长度的字符,形成一 ...
- RandomUtils
package com.cc.hkjc.util; import java.util.Random; public class RandomUtils { /** * 获取count个随 ...
- codeforces 669D D. Little Artem and Dance(乱搞题)
题目链接: D. Little Artem and Dance time limit per test 2 seconds memory limit per test 256 megabytes in ...
- 如何快糙好猛的使用Shiqi.Yu老师的公开人脸检测库(附源码)
前言 本次编写所用的库为于仕祺老师免费提供的人脸检测库.真心好用,识别率和识别速度完全不是Opencv自带的程序能够比拟的.将其配合Opencv的EigenFace算法,基本上可以形成一个小型的毕业设 ...
- 嵌入式Linux学习方法——给那些彷徨者(上)
要想学好嵌入式Linux,首先要解决两个重要问题: 1. 学什么? 2. 怎么学? 首先解决第一个问题. 嵌入式Linux的系统架构包括软件和硬件两个部分,如下图: 再来看看一个成熟的嵌入式产品的开发 ...
- NYOJ6——喷水装置(一)
喷水装置(一) 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述:现有一块草坪,长为20米,宽为2米,要在横中心线上放置半径为Ri的喷水装置,每个喷水装置的效果都会让以它 ...