简体   繁体   English

Spring Boot Hibernate Ehcache实体缓存实现

[英]Spring Boot Hibernate Ehcache entity caching implementation

I need to query a DB table during application start and store table entries (entites) in ehcache. 我需要在应用程序启动期间查询数据库表,并将表条目(实体)存储在ehcache中。 When ever request come for that row data (entity) need to fetch the data from cahce instead of going to DB. 每当有针对该行的请求时,数据(实体)都需要从cahce中获取数据,而不是去DB。

I have implemented it using method level caching.But when it's not useful as whenever method param changes there is a hit going to DB.How to avoid it is there a example for entity level caching. 我已经使用方法级缓存实现了它,但是当方法参数改变时它没有用的时候,DB就会受到打击,如何避免它是实体级缓存的一个例子。

I am using Spring boot 1.2.4 ehcache and Spring Boot Data JPA. 我正在使用Spring Boot 1.2.4 ehcache和Spring Boot Data JPA。

You can pre-load the data. 您可以预加载数据。 Manually. 手动。 Or using Cache.getAll with a loader-writer. 或与loader-writer一起使用Cache.getAll Or Cache.loadAll from JSR 107. 或来自JSR 107的Cache.loadAll

One easy way is just to 一种简单的方法是

List<MyEntity> entities = entityManager.getAll();
entities.forEach(e -> cache.put(e.getId(), e));

To use a loader-writer (see ehcache doc it is 要使用加载程序编写器(请参阅ehcache文档

List<Long> entities = entityManager.getAllIds();
entities.forEach(id -> cache.get(id));

And finally, loadAll is just cache.loadAll . 最后,loadAll只是cache.loadAll

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM