Spring Boot整合ehcache3.0 缓存
来源:互联网 发布:天猫魔盒看电视软件 编辑:程序博客网 时间:2024/06/06 13:57
Spring Boot整合ehcache3.0以上缓存
1、题记
- 目前市面上大多数文章都是ehcache2.0的版本,由于ehcache3.0改动较大,原来的配置方式已经不使用了,故小小研究了一番,笔以记之。
2、 先配置Spring boot
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mycompany</groupId> <artifactId>ehcache3-jsr107-spring-boot</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>ehcache3-jsr107-spring-boot</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <!--parent project to use spring boot --> <version>1.5.8.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> <!--Starter for using Spring Framework's caching support--> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- starter for using Spring MVC --> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <jvmArguments>-Xmx64m -XX:MaxDirectMemorySize=512m</jvmArguments> </configuration> </plugin> </plugins> </build></project>
添加我们今天的主角:
<dependency> <groupId>javax.cache</groupId> <!-- JSR-107 API--> <artifactId>cache-api</artifactId> </dependency> <dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> <version>3.4.0</version> </dependency>
cache-api也必须要引入,因为目前ehcache遵循jsr-107规范,所以必须引入
包就引入完毕了,看配置文件
3、ehcache配置文件
<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://www.ehcache.org/v3' xmlns:jsr107='http://www.ehcache.org/v3/jsr107'> <service> <jsr107:defaults> <jsr107:cache name="people" template="heap-cache"/> </jsr107:defaults> </service> <cache-template name="heap-cache"> <listeners> <listener> <class>org.vrzart.com.ehcache.EventLogger</class> <event-firing-mode>ASYNCHRONOUS</event-firing-mode> <event-ordering-mode>UNORDERED</event-ordering-mode> <events-to-fire-on>CREATED</events-to-fire-on> <events-to-fire-on>UPDATED</events-to-fire-on> <events-to-fire-on>EXPIRED</events-to-fire-on> <events-to-fire-on>REMOVED</events-to-fire-on> <events-to-fire-on>EVICTED</events-to-fire-on> </listener> </listeners> <resources> <heap unit="entries">2000</heap> <offheap unit="MB">100</offheap> <!-- unit of measure is case sensitive! --> </resources> </cache-template> <cache alias="peopleCache" uses-template="heap-cache"> <expiry> <ttl unit="seconds">600</ttl> </expiry> </cache></config>
配置spring 配置文件 application.properties
spring.cache.jcache.config=classpath:ehcache.xml ###attention,这里是jcacheserver.port=8090
4、使用代码
@Component@CacheDefaults(cacheName = "peopleCache")public class PersonService{ private static final Logger LOGGER = LoggerFactory.getLogger(PersonService.class); @CacheResult public Person getPerson(int ssn) { LOGGER.info("ssn " + ssn + " not found in cache. TimeStamp: {}", new Date()); switch (ssn) { case 123456789: return new Person(ssn, "Geoff", "Gibson"); case 987654321: return new Person(ssn, "Cory", "Beck"); default: return new Person(ssn,"John","Doe"); } }}
使用的jsr-107标准注解,没有使用Spring的一些注解(如cachable等)
5、测试
启动SpringJsr107Ehcache3Application类,浏览器输入[这个地址
]( http://localhost:8090/person/987654321)
多刷新自己就会发现从缓存中拿数据了
6、 代码下载
这里,大家可以自己下载下来跑一下,体验一下
阅读全文
1 0
- Spring Boot整合ehcache3.0 缓存
- spring boot spring cache ehcache3.x整合
- spring boot整合redis实现缓存机制
- Spring Boot 整合 Redis 实现缓存操作
- Spring Boot 整合 Redis 实现缓存操作
- Spring Boot 整合 Redis 实现缓存操作
- Spring Boot 整合 Redis 实现缓存操作
- Spring Boot 整合 Redis 实现缓存操作
- spring-boot | 整合Redis缓存数据
- Spring Boot整合Ehcache实现缓存功能
- 【spring-boot】spring-boot整合ehcache实现缓存机制
- spring boot 调用ehcache3.x(jsr107接口)原生api
- SpringBoot 整合Ehcache3
- SpringBoot 整合Ehcache3
- Springboot整合集成ehcache3
- spring-boot整合redis作为缓存(2)——spring-boot的缓存
- spring boot学习4之mybatis+redis缓存整合
- spring-boot整合redis作为缓存(4)——spring-boot引入Redis
- python实现pat 1033. To Fill or Not to Fill (25)
- Linux内核移植和根文件系统制作(详细步骤精讲)
- Android 3种坐标系、View在各坐标系下获取自身坐标的方法、View的滑动方式和scroll方法
- 创建xml文档并输出保存(第二种)
- 交互设计有没有标准化的流程
- Spring Boot整合ehcache3.0 缓存
- SCP 从内网到外网复制并跳转
- 深入推荐引擎相关算法
- svn更新产生的异常
- 北京SEO祭司:网站首页遭百度降权如何SEO优化回去?
- 服务器3D建模(一):PyOpenGL
- 听张晓翔谈设计师的职业规划
- 开发总结
- 接口测试中如何管理测试域名和接口