Spring Boot 之 Ehcache 缓存技术

Spring Boot 之 Ehcache 缓存技术

1.在pom.xml文件添加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>

2.在src/main/resources/ 下创建 Ehcache 的配置文件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>
<!-- 自定义缓存策略  name的值随便起,后面用得上-->
<cache name="UserEncache"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxElementsOnDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</cache>
</ehcache>

3.为了springboot能读取到xml文件,需要配置application.properties 文件

spring.cache.ehcache.cofnig=ehcache.xml

4.配置信息弄之后,开始为类和方法添加注解

4.1启动类添加注解,开启缓存机制

@EnableCaching

在这里插入图片描述
4.2对于查询的方法做缓存处理,对当前查询的对象做缓存处理,xx就是刚才xml配置的自定义名称,而且这个UserEncache是一个实体类。(注:在service层进行操作)

附UserEncache实体类表(这里使用的是JPA):

    @Entity
    @Table(name="t_user")
    public class UserEncache implements Serializable {
    	
    	@Id
    	@GeneratedValue(strategy=GenerationType.IDENTITY)
    	@Column(name="")
    	private Integer id;
    	
    	@Column(name="name")
    	private String name;
    	
    	@Column(name="age")
    	private Integer age;
    
    	@Column(name="address")
    	private String address;
    	
		 //省略get,set,toString方法(自己右键生成就好)
    	}

在service层的注解

@Cacheable(value="UserEncache")

在这里插入图片描述
5.编写测试代码
注:App是启动类
xxx.size(); 是查询结果有几条

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=App.class)
public class ServiceTest {
    @Test
    public void testFindAll(){
    	//第一次查询
    	System.out.println(this.userEncacheServiceImpl.findUserAll().size());		
    	//第二次查询
    	System.out.println(this.userEncacheServiceImpl.findUserAll().size());
    }
}

5.1测试时结果
如果看见只有一条查询语句,那么就是使用缓存了,把结果缓存起来,下次再查询时就从缓存中获取。
在这里插入图片描述

附:
未使用缓存前的结果,代码一样,只是service层方法不添加 缓存注解@Cacheable(value="UserEncache")

在这里插入图片描述

测试结果
可见执行了两次HQL
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot技术栈包含了多个关键组件和技术,其中一些是: - Spring框架:Spring Boot建立在Spring框架之上,提供了简化和自动配置的功能,使开发更加容易和高效。 - Spring MVC:Spring MVC是一个基于Java的Web框架,用于构建Web应用程序。Spring Boot集成了Spring MVC,使得开发RESTful API和Web应用程序变得更加简单。 - Thymeleaf:Thymeleaf是一个模板引擎,用于构建动态的Web页面。Spring Boot支持Thymeleaf作为视图层技术,使得在Web应用程序中渲染动态内容更加方便。 - 数据访问技术Spring Boot支持多种数据访问技术,包括JPA、Spring Data JPA、MyBatis等,使得与数据库的交互更加便捷。 - 安全性:Spring Boot提供了一些安全性特性,如基于角色的访问控制和跨站点请求伪造(CSRF)防护等,帮助开发者保护应用程序的安全性。 - 日志记录:Spring Boot集成了常用的日志框架,如Logback和Log4j,使得开发者更容易记录和管理应用程序的日志。 - 缓存Spring Boot支持多种缓存技术,如Ehcache和Redis,帮助开发者提高应用程序的性能和响应速度。 - 测试:Spring Boot提供了丰富的测试支持,包括单元测试和集成测试,使得开发者可以更方便地编写和运行各种测试用例。 这些技术和组件的集成使得Spring Boot成为一个功能强大、开发效率高的框架,适用于构建各种类型的应用程序,包括Web应用、RESTful API、批处理应用等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值