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. jquery设置html5音量的方法

    jquery设置html5音量的方法<pre> setTimeout(function() { alert(1); $('#music1')[0].volume = 0; setTimeo ...

  2. mysql group by使用方法注意

    mysql group by使用方法注意 group by 后面只用能用having 不能加 where等域名

  3. Groovy单元测试框架spock数据驱动Demo

    spock是一款全能型的单元测试框架. 上次文章分享了spock框架的基础功能的使用,在此基础上,我根据自己写的Groovy的封装方法.数据驱动以及一些Groovy的高级语法做了一些尝试.发现还是有一 ...

  4. glsl shader简明教程系列1

    glsl shader简明教程系列1 底层的东西我就不说了(自己去百度翻基础教程)  我直接说上层了(片段着色器) web编辑器还在开发中 有了编辑器 到时候可以把代码复制上去可以看到效果了 1  实 ...

  5. Windows下mysql的下载和安装

    下载: 1.下载地址:https://www.mysql.com/downloads/ 2.选择社区版:MySQL Community (GPL) Downloads » 3.MySQL Commun ...

  6. 比较一下inner join(可直接简写为join)和where直接关联

    SELECT * FROM A ,B WHERE A.ID = B.ID 是比较常用的2个表关联.之前一直用这个,后来换了家公司发现这家公司的报表大多数都是用inner join,稍微研究了一下.查阅 ...

  7. 接口测试之-postman

    在使用postman进行接口测试的时候,对于有些接口字段需要时间戳加密,这个时候我们就遇到2个问题,其一是接口中的时间戳如何得到?其二就是对于现在常用的md5加密操作如何在postman中使用代码实现 ...

  8. Swoft 源码剖析 - Swoole和Swoft的那些事 (Http/Rpc服务篇)

    前言 Swoft在PHPer圈中是一个门槛较高的Web框架,不仅仅由于框架本身带来了很多新概念和前沿的设计,还在于Swoft是一个基于Swoole的框架.Swoole在PHPer圈内学习成本最高的工具 ...

  9. nginx支持wss配置

    nginx证书 nginx.conf配置

  10. Learning Markov Clustering Networks for Scene Text Detection

    Learning Markov Clustering Networks for Scene Text Detection 论文下载:https://arxiv.org/pdf/1805.08365v1 ...