Spring Boot如何使用EhCache演示

网友投稿 250 2022-12-24


Spring Boot如何使用EhCache演示

一、EhCache使用演示

EhCache是一个纯java的进程内缓存框架,具有快速、精干等特点,Hibernate中的默认Cache就是使用的EhCache。

本章节示例是在Spring Boot集成Spring Cache的源码基础上进行改造。源码地址:https://github.com/imyanger/springboot-project/tree/master/p20-springboot-cache

使用EhCache作为缓存,我们先引入相关依赖。

org.springframework.boot

spring-boot-starter-web

net.sf.ehcache

ehcache

org.springframework.boot

spring-boot-starter-cache

然后创建EhCache的配置文件ehcache.xml。

xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"

updateCheck="false">

timeToLiveSeconds="600" overflowToDisk="true" />

timeToLiveSeconds="600" overflowToDisk="true" />

xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"

updateCheck="false">

timeToLiveSeconds="600" overflowToDisk="true" />

timeToLiveSeconds="600" overflowToDisk="true" />

timeToLiveSeconds="600" overflowToDisk="true" />

timeToLiveSeconds="600" overflowToDisk="true" />

timeToLiveSeconds="600" overflowToDisk="true" />

然后SpringBoot配置文件中,指明缓存类型并声明Ehcache配置文件的位置。

server:

port: 10900

spring:

profiles:

active: dev

cache:

type: ehcache

ehcache:

config: classpath:/ehcache.xml

这样就可以开始使用Ehcache了,测试代码与Spring Boot集成Spring Cache一致。

SpringBoot启动类,@EnableCaching开启Spring Cache缓存功能。

@EnableCaching

@SpringBootApplication

public class SpringbootApplication {

public static void main(String[] args) {

String tmpDir = System.getProperty("java.io.tmpdir");

System.out.println("临时路径:" + tmpDir);

SpringApplication.run(SpringbootApplication.class, args);

}

}

CacheApi接口调用类,方便调用进行测试。

@RestController

@RequestMapping("cache")

public class CacheApi {

@Autowired

private CacheService cacheService;

@GetMapping("get")

public User get(@RequestParam int id){

return cacheServichttp://e.get(id);

}

@PostMapping("set")

public User set(@RequestParam int id, @RequestParam String code, @RequestParam String name){

User u = new User(code, name);

return cacheService.set(id, u);

}

@DeleteMapping("del")

public void del(@RequestParam int id){

cacheService.del(id);

}

}

CacheService缓存业务处理类,添加缓存,更新缓存和删除。

@Slf4j

@Service

public class CacheService {

private Map dataMap = new HashMap (){

{

for (int i = 1; i < 100 ; i++) {

User u = new User("code" + i, "name" + i);

put(i, u);

}

}

};

// 获取数据

@Cacheable(value = "cache", key = "'user:' + #id")

public User get(int id){

log.info("通过id{}查询获取", id);

return dataMap.get(id);

}

// 更新数据

@CachePut(value = "cache", key = "'user:' + #id")

public User set(int id, User u){

log.info("更新id{}数据", id);

dataMap.put(id, u);

return u;

}

//删除数据

@CacheEvict(value = "cache", key = "'user:' + #id")

public void del(int id){

log.info("删除id{}数据", id);

dataMap.remove(id);

}

}

源码地址:https://github.com/imyanger/springboot-project/tree/master/p22-springboot-cache-ehcache


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java猜数字小游戏案例
下一篇:@Controller、@RestController注解区别详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~