简体   繁体   English

EhCache Spring优化

[英]EhCache Spring optimization

I'm using EhCache(2.10.1) and Spring (4.2.5) 我正在使用EhCache(2.10.1)和Spring(4.2.5)

I configured my BookDao's methods with cache like this: 我使用以下缓存配置了BookDao的方法:

@Cacheable(value = "bookByName", key = "#name")
public Book findByName(String name) {
    //find book by name in db 
}

@Cacheable(value = "bookById", key = "#id")
public Book findById(int id) {
    //find book by id in db
}

@Cacheable(value = "books", key = "??")
public List<Book> findAll() {
    //find books in db
}

First, would it be possible to combine findAll cache with findById cache or findByName cache? 首先,是否可以将findAll缓存与findById缓存或findByName缓存结合使用? What should I put in the key annotation parameter to do so? 为此,我应该在key annotation parameter添加什么?

Second, if I want to update or create one book I need to add/evict this book on each cache and evict the entire list of the findAll method. 其次,如果我要更新或创建一本书,则需要在每个缓存上添加/退出这本书,并退出findAll方法的整个列表。 Is there another way to do this better? 还有其他更好的方法吗?

Answering on a conceptual level. 在概念上回答。 It is basically the identical problem for any cache as well as for Spring caching annotations or JSR107 caching annotations. 对于任何缓存以及Spring缓存批注或JSR107缓存批注,基本上都是相同的问题。

First, would it be possible to combine findAll cache with findById cache or findByName cache? 首先,是否可以将findAll缓存与findById缓存或findByName缓存结合使用? What should I put in the key annotation parameter to do so? 为此,我应该在关键注释参数中添加什么?

No, this isn't possible with annotations. 不,注释无法实现。 Except clearing the cache, you cannot do bulk operations with annotations. 除了清除缓存之外,您无法对批注执行批量操作。

One possibility would be to retrieve all entries in the cache using the imperative cache interface, however, that's only the books in the cache, not all available books. 一种可能性是使用命令式缓存接口检索缓存中的所有条目,但是,这只是缓存中的书,而不是所有可用的书。 The cache doesn't know every book that is available, at least if it is really a cache. 缓存并不知道所有可用的书,至少是如果它确实是缓存。

Second, if I want to update or create one book I need to add/evict this book on each cache and evict the entire list of the findAll method. 其次,如果我要更新或创建一本书,则需要在每个缓存上添加/退出这本书,并退出findAll方法的整个列表。 Is there another way to do this better? 还有其他更好的方法吗?

If you have a list of all books, what do you need the cache for? 如果您有所有书籍的清单,那么需要什么缓存?

You could return a subset of all books in a list. 您可以返回列表中所有书籍的子集。 In this case the parameter could be a query. 在这种情况下,参数可以是查询。 The list size should be limited, so you do not run out of memory while processing and when caching it. 列表的大小应该受到限制,这样在处理和缓存它时不会耗尽内存。 Ideally you return List<Integer> with the ids or a List<Book> that is backed with an id array and retrieves the book objects from the cache. 理想情况下,您返回具有ID的List<Integer>或具有ID数组支持的List<Book>并从缓存中检索书籍对象。

If you want to be able to process all books then a method Iterator<Integer> findAll() or Iterator<Book> findAll() would be more appropriate. 如果您希望能够处理所有书籍,则使用Iterator<Integer> findAll()Iterator<Book> findAll()方法更为合适。 You shouldn't cache it, because: the result size is unbounded, the cache makes only sense when small parts of the data is accessed more often. 您不应该缓存它,因为:结果大小是无界的,只有在更频繁地访问一小部分数据时,缓存才有意义。

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

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