Spring + Ehcache:如何缓存查找所有结果

Roy*_*han 2 java spring ehcache

我使用 Spring + Ehcache 作为我的缓存层。(通过代理)

我想知道您是否可以在同一个缓存中同时缓存“f​​indAll”结果和“findById”结果,然后 CacheEvict 特定项目和“findAll”结果(保持项目的其余部分不变)并在更新时将其加载回来再次缓存“findById”时?

(或另一种方法是将 findAll 和 findById 保留在 2 个缓存中,并在为 findAll 缓存和 findById 缓存上的特定项目更新 CacheEvict allEntries 时)

这可能吗?

Tom*_*icz 5

I will explain how with its second level and query cache work to give you a general idea. First of all Hibernate caches all single entities (e.g. retrieved by findById type of operations) in so-called second level cache.

If you retrieve all entities using findAll it puts primary keys of all entities in query cache (under one key) and all concrete entities in second level cache. When you call findAll again, it first retrieves all primary keys from query cache and then all entities from second level cache (or from database).

Invalidation is rather simple: INSERT and DELETE operations should invalidate the whole findAll query cache, while UPDATEs are transparent.

This should guide you how can you implement this in your solution. This is possible in Spring, but you might need to code a little bit, especially the part mapping from query cache to second level cache.