Spring Boot 整合 Ehcache
 
修改 pom 文件
<!-- Spring Boot 缓存支持启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- Ehcache 坐标 -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
创建 Ehcache 的配置文件
 
文件名:ehcache.xml
位置:src/main/resources/ehcache.xml
 
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
<diskStore path="java.io.tmpdir"/> <!--defaultCache:echcache 的默认缓存策略 -->
<defaultCache maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache> <!-- 自定义缓存策略 -->
<cache name="users"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</cache>
</ehcache>
修改 application.properties 文件
 
#项目端口配置
server.port=8080
server.address=0.0.0.0 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=UTF8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root spring.datasouce.type=com.alibaba.druid.pool.DruidDataSource spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true spring.devtools.restart.enabled=true spring.cache.ehcache.config=classpath:ehcache.xml
 
修改启动类
package com.bjsxt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching; @SpringBootApplication
@EnableCaching//启用缓存机制
public class BootDataApplication { public static void main(String[] args) {
SpringApplication.run(BootDataApplication.class, args);
} }
 
 
 
创建业务层
package com.bjsxt.service.impl;

import com.bjsxt.dao.UserDao;
import com.bjsxt.pojo.Users;
import com.bjsxt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service; import java.util.List; @Service
public class UserServiceImpl implements UserService { @Autowired
UserDao ud; @Override
@CacheEvict(value = "users",allEntries = true)//清空缓存
public void addUser(Users users) {
ud.save(users);
} @Override
@Cacheable(value = "users")//配置缓存,查找缓存文件
public List<Users> findall() {
List<Users> usersList = ud.findAll();
return usersList;
} @Override
@Cacheable(value = "users",key = "#pageable.pageSize")//配置缓存,配置缓存的key
public Page<Users> findUserByPage(Pageable pageable) {
Page<Users> usersPage = ud.findAll(pageable);
return usersPage;
} }
 
 
修改实体类 Users
需要实现序列化接口
package com.bjsxt.pojo;

import lombok.Data;

import javax.persistence.*;
import java.io.Serializable; @Entity
@Table(name = "users")
@Data
public class Users implements Serializable { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "userid")
private int userid; @Column(name = "username")
private String username; @Column(name = "userage")
private int userage; public Users(){}
public Users(String username, int userage) {
this.username = username;
this.userage = userage;
}
}
 
 
 
 
测试
 
package com.bjsxt.test;
import com.bjsxt.BootDataApplication;
import com.bjsxt.dao.UserDao;
import com.bjsxt.pojo.Users;
import com.bjsxt.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List; @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = BootDataApplication.class)
public class UserTest { @Autowired
UserService us; @Autowired
UserDao ud; /**
* 添加用户
*/
@Test
public void TestAddUser(){
Users users=new Users();
users.setUsername("杨彪");
users.setUserage(27);
us.addUser(users);
} @Test
public void findByNameAndAge(){
Specification<Users> spec=new Specification<Users>() {
@Override
public Predicate toPredicate(Root<Users> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
/*List<Predicate> predicates=new ArrayList<>();
predicates.add(criteriaBuilder.equal(root.get("username"),"asd"));
predicates.add(criteriaBuilder.equal(root.get("userage"),"123"));
Predicate[] arr=new Predicate[predicates.size()];
return criteriaBuilder.and(predicates.toArray(arr));*/
return criteriaBuilder.or(criteriaBuilder.equal(root.get("username"),"asd"),criteriaBuilder.equal(root.get("userage"),"22"));
}
};
List<Users> users = ud.findAll(spec);
for (Users user : users) {
System.out.println(user);
}
} /**
* 测试使用缓存查询所有,第二次不会打印sql语句
*/
@Test
public void TestFindAll(){
System.out.println("第一次查询:");
List<Users> users = us.findall();
for (Users user : users) {
System.out.println("First:"+user);
}
Users use=new Users();
use.setUsername("杨彪3");
use.setUserage(22);
us.addUser(use); System.out.println("\n第二次查询:");
List<Users> u = us.findall();
for (Users user : u) {
System.out.println("Second:"+user);
} } @Test
public void findUserByPage(){
Pageable pageable=new PageRequest(0,2); System.out.println("第一次查询:");
Page<Users> userByPage = us.findUserByPage(pageable);
long totalElements = userByPage.getTotalElements();
System.out.println("First总条数:"+totalElements); System.out.println("\n第二次查询:");
Page<Users> userByPage2 = us.findUserByPage(pageable);
long totalElements2 = userByPage2.getTotalElements();
System.out.println("Second总条数:"+totalElements2); System.out.println("\n第三次查询:");
pageable=new PageRequest(1,2);
Page<Users> userByPage3 = us.findUserByPage(pageable);
long totalElements3 = userByPage3.getTotalElements();
System.out.println("Third总条数:"+totalElements3);
} }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Spring Boot缓存Ehcache的更多相关文章

  1. (37)Spring Boot集成EHCache实现缓存机制【从零开始学Spring Boot】

    [本文章是否对你有用以及是否有好的建议,请留言] 写后感:博主写这么一系列文章也不容易啊,请评论支持下. 如果看过我之前(35)的文章这一篇的文章就会很简单,没有什么挑战性了. 那么我们先说说这一篇文 ...

  2. Spring Boot集成EHCache实现缓存机制

    SpringBoot 缓存(EhCache 2.x 篇) SpringBoot 缓存 在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManag ...

  3. 另一种缓存,Spring Boot 整合 Ehcache

    用惯了 Redis ,很多人已经忘记了还有另一个缓存方案 Ehcache ,是的,在 Redis 一统江湖的时代,Ehcache 渐渐有点没落了,不过,我们还是有必要了解下 Ehcache ,在有的场 ...

  4. Spring Boot 集成 Ehcache 缓存,三步搞定!

    作者:谭朝红 www.ramostear.com/articles/spring_boot_ehcache.html 本次内容主要介绍基于Ehcache 3.0来快速实现Spring Boot应用程序 ...

  5. spring boot 使用Ehcache

    1-引入maven依赖: 2-增加ehcache.xml 3-bootstrap.yml配置ehcache.xml的路径 4-启动类加注解@EnableCaching 5-使用处加注解@Cacheab ...

  6. Spring Boot2 系列教程(三十)Spring Boot 整合 Ehcache

    用惯了 Redis ,很多人已经忘记了还有另一个缓存方案 Ehcache ,是的,在 Redis 一统江湖的时代,Ehcache 渐渐有点没落了,不过,我们还是有必要了解下 Ehcache ,在有的场 ...

  7. 3步轻松搞定Spring Boot缓存

    作者:谭朝红 前言 本次内容主要介绍基于Ehcache 3.0来快速实现Spring Boot应用程序的数据缓存功能.在Spring Boot应用程序中,我们可以通过Spring Caching来快速 ...

  8. Spring Boot整合EhCache

    本文讲解Spring Boot与EhCache的整合. 1 EhCache简介 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认CacheProvid ...

  9. Spring boot缓存初体验

    spring boot缓存初体验 1.项目搭建 使用MySQL作为数据库,spring boot集成mybatis来操作数据库,所以在使用springboot的cache组件时,需要先搭建一个简单的s ...

随机推荐

  1. 文件输入输出函数fgetc/fputc及fgets/fputs等文件指针位置的变化

    文件打开后才可以对文件进行操作.也就是说,文件必须经历打开-操作-关闭的过程.如前所述,C语言对文件的操作都是通过调用标准I/O库函数来实现的.文件操作实际是指对文件的读写.文件的读操作就是从文件中读 ...

  2. K8S入门系列之集群二进制部署-->master篇(二)

    组件版本和配置策略 组件版本 Kubernetes 1.16.2 Docker 19.03-ce Etcd 3.3.17 https://github.com/etcd-io/etcd/release ...

  3. 总结一下 IEnumerable 的例子

    本篇将围绕 <试试 IEnumerable 的 10 个小例子>和<试试 IEnumerable 的另外 6 个小例子>给出的例子,总结一下对于 IEnumerable 接口的 ...

  4. activemq 的延迟队列和幂等性检查

    一. 延迟消息队列 1. 在提交支付之后,可以发送一个延迟检查的队列,来主动查询用户在支付宝上的支付状态 在mq的配置/config/activeMq.xml的broker实例上配置 schedule ...

  5. CSS如何修改tr边框属性

    有很多时候,我们都要自定义为表格合并边框,这个只要 table{ border-collapse:collapse; } 就可以了 参数: separate 默认值.边框会被分开.不会忽略border ...

  6. 理解Spark运行模式(三)(STANDALONE和Local)

    前两篇介绍了Spark的yarn client和yarn cluster模式,本篇继续介绍Spark的STANDALONE模式和Local模式. 下面具体还是用计算PI的程序来说明,examples中 ...

  7. Python3.7.1学习(五) 将列表中的元素转化为数字并排序

    # 本文实例讲述了Python中列表元素转为数字的方法.分享给大家供大家参考,具体如下: # 有一个数字字符的列表: numbers = ['2', '4', '1', '3']print(numbe ...

  8. ubuntu18 拨号连接宽带网络方法

    1.打开终端查看以太网网卡编号 2.打开/usr/share/applications/ 3.点击network connections 4.点击左下角的+号 5.连接类型选择DSL/PPPoE 6. ...

  9. 深度剖析Javascript执行环境、作用域链

    一.执行环境 执行环境(也叫做执行上下文,Execution Context)是Javascript中最为重要的一个概念.执行环境定义了变量或函数有权访问其他数据,决定了它们各自的行为.每个执行环境都 ...

  10. ZeroC ICE中的对象

    在ZeroC Ice中定义了三种基本对象类型. 它们分别是IceProxy::Ice::Object(于Ice/Proxy.h),Ice::Object(于Ice/Object.h)和Ice::Loc ...