我对在我正在使用的模块上使用弹簧缓存抽象很感兴趣,但是所有的数据操作都在集合上,以避免多个数据源调用,所以我不确定我是否有一个合适的“切入点”来实现方法级的缓存。这不是Spring或Java特有的问题,而是更多的设计问题。
例如:
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样式的方法级声明式缓存?请记住,为了确保线程安全,我希望有效地保持数据访问对象(上面)的无状态性。
发布于 2015-01-23 18:30:19
我不相信这可以通过Spring的缓存抽象来实现,它的设计似乎不支持您的用例(除非我误解了您的意图)。
Spring的方法级缓存依赖于通过KeyGenerator生成唯一密钥的能力。这种简单的键值存储方法将无法工作,因为您希望根据传入的特定ID来缓存数据的子集。
如果您希望基于调用中If的子集进行缓存,那么利用JPA/Hibernate查询缓存可能满足您的需要。
缺省情况下,Spring 4使用新的SimpleKeyGenerator,它使用hashCode()和equals()契约生成SimpleKey对象--为具有多个参数的方法调用委托给Arrays.deepHashCode()和Arrays.deepEquals()。
https://softwareengineering.stackexchange.com/questions/270942
复制相似问题