2016-02-17 12 views
0

使用org.springframework.cache.ehcache.EhCacheCacheManager &假设我有方法:拉单个对象了springframework的的Ehcache名单

@Cacheable("imageData") // add key = xyz here? 
public List<ImageData> getProductIdAndColorCodeForSkus(List<Integer> skus) { 
    thread sleep 
    ...do some stuff... 
    return listOfImageData; 
} 

,我已经打这个方法用的SKU {111111,111222 }。我后来想拉一个的ImageData对象从缓存中,例如:

Cache imageDataCache = cacheManager.getCache("imageData"); 
imageDataCache.get(key, ImageData.class) // what do I use for key? 

我已经试过

new Integer(111111) 

但是这将返回一个空的结果。我也尝试过在那个sku的列表中传递。

另外,我知道这两个ImageData都被缓存,并且可以单独检索,因为当我使用单个sku:111111或111222的列表第二次调用此方法时,不会发生线程睡眠。

回答

2

缓存抽象默认使用SimpleKeyGenerator根据方法参数生成密钥。在你的情况下,你有一个List<Integer>,所以这将创建一个SimpleKey与列表。

如果您对此不满意,您可以自定义KeyGenerator。由于您直接在其他地方访问缓存,我​​会强烈建议您对密钥生成器进行控制,以使您的API保持一致。

更多信息in the documentation