Spring Cache 配置变量 int 作为 @Cacheable 注释中的键

问题描述

如何配置 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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...