Spring Boot缓存实战 EhCache示例
程序员文章站
2024-02-17 18:07:46
spring boot默认使用的是simplecacheconfiguration,即使用concurrentmapcachemanager来实现缓存。但是要切换到其他缓存...
spring boot默认使用的是simplecacheconfiguration,即使用concurrentmapcachemanager来实现缓存。但是要切换到其他缓存实现也很简单
pom文件
在pom中引入相应的jar包
<dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-jpa</artifactid> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> </dependency> <dependency> <groupid>org.apache.commons</groupid> <artifactid>commons-dbcp2</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-cache</artifactid> </dependency> <dependency> <groupid>net.sf.ehcache</groupid> <artifactid>ehcache</artifactid> </dependency> </dependencies>
配置文件
ehcache所需要的配置文件,只需要放到类路径下,spring boot会自动扫描。
<?xml version="1.0" encoding="utf-8"?> <ehcache> <cache name="people" maxelementsinmemory="1000"/> </ehcache>
也可以通过在application.properties文件中,通过配置来指定ehcache配置文件的位置,如:
spring.cache.ehcache.config= # ehcache配置文件地址
spring boot会自动为我们配置ehcachecachemannager的bean。
关键service
package com.xiaolyuh.service.impl; import com.xiaolyuh.entity.person; import com.xiaolyuh.repository.personrepository; import com.xiaolyuh.service.personservice; import org.springframework.beans.factory.annotation.autowired; import org.springframework.cache.annotation.cacheevict; import org.springframework.cache.annotation.cacheput; import org.springframework.cache.annotation.cacheable; import org.springframework.stereotype.service; @service public class personserviceimpl implements personservice { @autowired personrepository personrepository; @override @cacheput(value = "people", key = "#person.id") public person save(person person) { person p = personrepository.save(person); system.out.println("为id、key为:" + p.getid() + "数据做了缓存"); return p; } @override @cacheevict(value = "people")//2 public void remove(long id) { system.out.println("删除了id、key为" + id + "的数据缓存"); //这里不做实际删除操作 } @override @cacheable(value = "people", key = "#person.id")//3 public person findone(person person) { person p = personrepository.findone(person.getid()); system.out.println("为id、key为:" + p.getid() + "数据做了缓存"); return p; } }
controller
package com.xiaolyuh.controller; import com.xiaolyuh.entity.person; import com.xiaolyuh.service.personservice; import org.springframework.beans.factory.annotation.autowired; import org.springframework.cache.cachemanager; import org.springframework.web.bind.annotation.requestbody; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.restcontroller; @restcontroller public class cachecontroller { @autowired personservice personservice; @autowired cachemanager cachemanager; @requestmapping("/put") public long put(@requestbody person person) { person p = personservice.save(person); return p.getid(); } @requestmapping("/able") public person cacheable(person person) { system.out.println(cachemanager.tostring()); return personservice.findone(person); } @requestmapping("/evit") public string evit(long id) { personservice.remove(id); return "ok"; } }
启动类
@springbootapplication @enablecaching// 开启缓存,需要显示的指定 public class springbootstudentcacheapplication { public static void main(string[] args) { springapplication.run(springbootstudentcacheapplication.class, args); } }
测试类
package com.xiaolyuh; import static org.springframework.test.web.servlet.request.mockmvcrequestbuilders.post; import static org.springframework.test.web.servlet.result.mockmvcresultmatchers.content; import static org.springframework.test.web.servlet.result.mockmvcresultmatchers.status; import java.util.hashmap; import java.util.map; import org.junit.before; 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.http.mediatype; import org.springframework.test.context.junit4.springrunner; import org.springframework.test.web.servlet.mockmvc; import org.springframework.test.web.servlet.mvcresult; import org.springframework.test.web.servlet.setup.mockmvcbuilders; import org.springframework.web.context.webapplicationcontext; import net.minidev.json.jsonobject; @runwith(springrunner.class) @springboottest public class springbootstudentcacheapplicationtests { @test public void contextloads() { } private mockmvc mockmvc; // 模拟mvc对象,通过mockmvcbuilders.webappcontextsetup(this.wac).build()初始化。 @autowired private webapplicationcontext wac; // 注入webapplicationcontext // @autowired // private mockhttpsession session;// 注入模拟的http session // // @autowired // private mockhttpservletrequest request;// 注入模拟的http request\ @before // 在测试开始前初始化工作 public void setup() { this.mockmvc = mockmvcbuilders.webappcontextsetup(this.wac).build(); } @test public void testable() throws exception { for (int i = 0; i < 2; i++) { mvcresult result = mockmvc.perform(post("/able").param("id", "2")) .andexpect(status().isok())// 模拟向testrest发送get请求 .andexpect(content().contenttype(mediatype.application_json_utf8))// 预期返回值的媒体类型text/plain; // charset=utf-8 .andreturn();// 返回执行请求的结果 system.out.println(result.getresponse().getcontentasstring()); } } }
打印日志
从上面可以看出第一次走的是数据库,第二次走的是缓存
源码:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
Spring Boot缓存实战 EhCache示例
-
spring-boot集成spring-security的oauth2实现github登录网站的示例
-
Spring Boot中使用 Spring Security 构建权限系统的示例代码
-
详解Spring boot使用Redis集群替换mybatis二级缓存
-
详解spring boot集成ehcache 2.x 用于hibernate二级缓存
-
spring boot实现软删除的示例代码
-
Spring Boot修改内置Tomcat默认端口号的示例
-
Spring Boot 简单使用EhCache缓存框架的方法
-
Spring Boot集成ElasticSearch实现搜索引擎的示例
-
详解Spring Boot中整合Sharding-JDBC读写分离示例