简体   繁体   中英

Spring Boot Hibernate Ehcache entity caching implementation

I need to query a DB table during application start and store table entries (entites) in ehcache. When ever request come for that row data (entity) need to fetch the data from cahce instead of going to 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.

I am using Spring boot 1.2.4 ehcache and Spring Boot Data JPA.

You can pre-load the data. Manually. Or using Cache.getAll with a loader-writer. Or Cache.loadAll from JSR 107.

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

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

And finally, loadAll is just cache.loadAll .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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