0

I use technologies like spring boot, jpa and java 8. I have a question, how can I check if the cache is empty and I should send a query to the database to reload it (how to check that I need to reload the cache)?

Eric
  • 3
  • 2
  • This [guide](https://spring.io/guides/gs/caching/) describes lazy loading, to hit the database when first requested and cache the result. [This similar question](https://stackoverflow.com/questions/27940704/how-to-load-cache-on-startup-in-spring) describes approaches to preemptively load a cache. – Andrew S Dec 18 '18 at 15:36

1 Answers1

1

As your question is not clear about regarding what type of cache you are using ?? JPA uses the first level of caching is the persistence context. The Entity Manager guarantees that within a single Persistence Context, for any particular database row, there will be only one object instance. However the same entity could be managed in another User's transaction, so you should use either optimistic or pessimistic locking.

If you mean 2nd level cache ,This level of cache came due to performance reasons.this 2nd level cache sits between Entity Manager and the database. Persistence context shares the cache, making the second level cache available throughout the application. Database traffic is reduced considerably because entities are loaded in to the shared cache and made available from there. So actually laying you dont need to worry about reloading of data from database if cache miss happens.

Now if you are implementing your own logic to implement the cache , then you need to do more research on how actually caching works and different algorithms for caching like LRU,MRU etc. (which I would personally not recommend as you can use existing available providers like ehcache, redis,hazelcast just few names for 2nd level caching )

prasannajoshi
  • 147
  • 11