2017-02-16 24 views
0

我只是试图将项目的结果存储在缓存中,并且除了密钥以外都一切顺利。春季数据中的咖啡因缓存密钥jpa

SimpleKeyGenerator通常会保存基于参数的对象或结果,在我的情况下,我想要将对象存储在缓存中,并且键应该是列表中对象的属性,这里是一个示例。

public class Item{ 
    private Long id; 
    private Long reference; 
    private Integer status; 
    //setter and getter 
} 

public interface ItemRepository extends JpaRepository<Long,Item>{ 

    @Cachable("items") 
    List<Item> findByReferenceAndStatus(Long reference, Integer status); 
} 

现在我要存储在基于item.id缓存每个对象,我知道我们可以使用注释的关键属性,但我怎么能访问ID为利用规划环境地政司的每个项目。 不幸的是,创建自定义密钥生成器将无济于事,因为它们都基于参数,目标类和方法。

有什么建议吗?

回答

0

我相信你想优化高速缓存存储以后通过项目ID进行查询。

如果您正确地为您的实体配置JPA第二级别,那么当您使用JPA自动使用第二级别时,通过ID进行良好配置的负载实体时,您无需这样做。

要一个结果只是使用缓存规划环境地政司#result关键属性:

https://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html#cache-spel-context-tbl

@Cacheable(cacheNames="item", key="#result.id")