Spring Boot缓存Ehcache
<!-- 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 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>
#项目端口配置
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;
}
}
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的更多相关文章
- (37)Spring Boot集成EHCache实现缓存机制【从零开始学Spring Boot】
[本文章是否对你有用以及是否有好的建议,请留言] 写后感:博主写这么一系列文章也不容易啊,请评论支持下. 如果看过我之前(35)的文章这一篇的文章就会很简单,没有什么挑战性了. 那么我们先说说这一篇文 ...
- Spring Boot集成EHCache实现缓存机制
SpringBoot 缓存(EhCache 2.x 篇) SpringBoot 缓存 在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManag ...
- 另一种缓存,Spring Boot 整合 Ehcache
用惯了 Redis ,很多人已经忘记了还有另一个缓存方案 Ehcache ,是的,在 Redis 一统江湖的时代,Ehcache 渐渐有点没落了,不过,我们还是有必要了解下 Ehcache ,在有的场 ...
- Spring Boot 集成 Ehcache 缓存,三步搞定!
作者:谭朝红 www.ramostear.com/articles/spring_boot_ehcache.html 本次内容主要介绍基于Ehcache 3.0来快速实现Spring Boot应用程序 ...
- spring boot 使用Ehcache
1-引入maven依赖: 2-增加ehcache.xml 3-bootstrap.yml配置ehcache.xml的路径 4-启动类加注解@EnableCaching 5-使用处加注解@Cacheab ...
- Spring Boot2 系列教程(三十)Spring Boot 整合 Ehcache
用惯了 Redis ,很多人已经忘记了还有另一个缓存方案 Ehcache ,是的,在 Redis 一统江湖的时代,Ehcache 渐渐有点没落了,不过,我们还是有必要了解下 Ehcache ,在有的场 ...
- 3步轻松搞定Spring Boot缓存
作者:谭朝红 前言 本次内容主要介绍基于Ehcache 3.0来快速实现Spring Boot应用程序的数据缓存功能.在Spring Boot应用程序中,我们可以通过Spring Caching来快速 ...
- Spring Boot整合EhCache
本文讲解Spring Boot与EhCache的整合. 1 EhCache简介 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认CacheProvid ...
- Spring boot缓存初体验
spring boot缓存初体验 1.项目搭建 使用MySQL作为数据库,spring boot集成mybatis来操作数据库,所以在使用springboot的cache组件时,需要先搭建一个简单的s ...
随机推荐
- css3的过渡和动画的属性介绍
一.过渡 什么是过渡? 过渡是指:某元素的css属性值在一段时间内,平滑过渡到另外一个值,过渡主要观察的是过程和结果. 设置能够过渡的属性: 支持过渡的样式属性,颜色的属性,取值为数值,transfo ...
- jsp页面时间的转换js
/** * 日期 转换为 Unix时间戳 * @param <string> 2014-01-01 20:2 ...
- pat 1084 Broken Keyboard(20 分)
1084 Broken Keyboard(20 分) On a broken keyboard, some of the keys are worn out. So when you type som ...
- nyoj 36-最长公共子序列 (动态规划,DP, LCS)
36-最长公共子序列 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:18 submit:38 题目描述: 咱们就不拐弯抹角了,如题,需要你做的就是写 ...
- nyoj 803-A/B Problem
803-A/B Problem 内存限制:64MB 时间限制:1000ms 特判: No 通过数:2 提交数:4 难度:3 题目描述: 做了A+B Problem,A/B Problem不是什么问题了 ...
- nyoj 773-开方数 (pow)
773-开方数 内存限制:64MB 时间限制:1000ms 特判: No 通过数:3 提交数:8 难度:3 题目描述: 现在给你两个数 n 和 p ,让你求出 p 的开 n 次方. 输入描述: 每组数 ...
- C++程序的耦合性设计
声明:本文部分采用和参考<代码里的世界观-通往架构师之路>中内容,可以说是该书中耦合性一章的读后感,感谢该书的作者余叶老师的无私分享. 1.什么是耦合? 耦合其实就是程序之间的相关性. 程 ...
- php中 continue break exit return 的区别
php 中的循环有 for foreache while do{} whlie这几种. 1.continue continue是用来在循环结构中,控制程序放弃本次循环continue: 之后的语句,并 ...
- 扛把子组20191017-5 alpha week 2/2 Scrum立会报告+燃尽图 04
此作业要求参见[https://edu.cnblogs.com/campus/nenu/2019fall/homework/9801] 一.小组情况 队名:扛把子 组长:迟俊文 组员:宋晓丽 梁梦瑶 ...
- 30L,17L,13L容器分油,python递归,深度优先算法
伪代码: 全部代码: a=[] b=[] def f(x,y,z): b.append([x,y,z]) if x==15 and y==15: print(x,y,z) i=0; for x in ...