首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >集合上的声明式缓存

集合上的声明式缓存
EN

Software Engineering用户
提问于 2015-01-23 16:51:01
回答 1查看 219关注 0票数 2

我对在我正在使用的模块上使用弹簧缓存抽象很感兴趣,但是所有的数据操作都在集合上,以避免多个数据源调用,所以我不确定我是否有一个合适的“切入点”来实现方法级的缓存。这不是Spring或Java特有的问题,而是更多的设计问题。

例如:

代码语言:javascript
复制
public class SomeDataAccessObject{
 private SomeBatchingDataSourceAbstraction datasource;

 //would ideally add a cache annotation to this method to cache based on input, but that   
 //clearly won't work in this scenario because we are loading multiple which may have 
 //some cache hits only
 public List<String> getNames(List<Integer> ids){
  //ideally I would load as many names as I can from the cache and 
  //then use the datasource to get the rest
  //I can achieve this with a programatic cache but it is very invasive
  return datasource.fetch(ids);
 }

}

有没有人想出一种好的方法来重新设计这样的场景,允许我使用Spring样式的方法级声明式缓存?请记住,为了确保线程安全,我希望有效地保持数据访问对象(上面)的无状态性。

EN

回答 1

Software Engineering用户

发布于 2015-01-23 18:30:19

我不相信这可以通过Spring的缓存抽象来实现,它的设计似乎不支持您的用例(除非我误解了您的意图)。

Spring的方法级缓存依赖于通过KeyGenerator生成唯一密钥的能力。这种简单的键值存储方法将无法工作,因为您希望根据传入的特定ID来缓存数据的子集。

如果您希望基于调用中If的子集进行缓存,那么利用JPA/Hibernate查询缓存可能满足您的需要。

缺省情况下,Spring 4使用新的SimpleKeyGenerator,它使用hashCode()和equals()契约生成SimpleKey对象--为具有多个参数的方法调用委托给Arrays.deepHashCode()和Arrays.deepEquals()。

票数 2
EN
页面原文内容由Software Engineering提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://softwareengineering.stackexchange.com/questions/270942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档