问题描述
如何配置 Spring 缓存以接受 int 变量作为键?
我尝试了以下但没有奏效。我确实在 API 中读过,我看到键的值必须是字符串。所以我不是 100% 确定什么是正确的配置
我尝试在我的方法中使用这个注解:
@Cacheable(cacheNames="GETREQUIREMENTPRIVATE",key = "#RequirementID")
private Requirement getRequirement(final int RequirementID,final Connection connection)
我确实在我的 ehcache.xml 文件中设置了这个:
<cache name="GETREQUIREMENTPRIVATE"
statistics="true"
maxElementsInMemory="1000"
eternal="true"
memoryStoreevictionPolicy="LFU">
</cache>
解决方法
您可以使用 Integer
作为键,但不是原始 int
应该有对象,因为获取字符串键将使用 .toString() 方法。所以Integer
,而不是int
。