Rename Cassandra TTL Property for Clarity

Rename `timeToLiveSeconds` to `timeToLive` in
`CassandraChatMemoryProperties` and associated code to prevent
misleading interpretation. This change addresses issue #1728, as
the property previously suggested values in seconds but expected
milliseconds.

Improve readability and ensure accurate configuration by aligning
the name with its behavior. Include updates to test cases to
validate the new property name and verify ISO 8601 format handling.

Fixes #1728

Signed-off-by: Jihoon Kim <pigberger70@gmail.com>
This commit is contained in:
jitokim
2024-11-15 02:42:31 +09:00
committed by Mark Pollack
parent 432954dad7
commit a7a1804fac
4 changed files with 38 additions and 11 deletions

View File

@@ -31,6 +31,7 @@ import org.springframework.context.annotation.Bean;
* {@link AutoConfiguration Auto-configuration} for {@link CassandraChatMemory}.
*
* @author Mick Semb Wever
* @author Jihoon Kim
* @since 1.0.0
*/
@AutoConfiguration(after = CassandraAutoConfiguration.class)
@@ -52,8 +53,8 @@ public class CassandraChatMemoryAutoConfiguration {
if (!properties.isInitializeSchema()) {
builder = builder.disallowSchemaChanges();
}
if (null != properties.getTimeToLiveSeconds()) {
builder = builder.withTimeToLive(properties.getTimeToLiveSeconds());
if (null != properties.getTimeToLive()) {
builder = builder.withTimeToLive(properties.getTimeToLive());
}
return CassandraChatMemory.create(builder.build());

View File

@@ -30,6 +30,7 @@ import org.springframework.lang.Nullable;
* Configuration properties for Cassandra chat memory.
*
* @author Mick Semb Wever
* @author Jihoon Kim
* @since 1.0.0
*/
@ConfigurationProperties(CassandraChatMemoryProperties.CONFIG_PREFIX)
@@ -47,7 +48,7 @@ public class CassandraChatMemoryProperties extends CommonChatMemoryProperties {
private String userColumn = CassandraChatMemoryConfig.DEFAULT_USER_COLUMN_NAME;
private Duration timeToLiveSeconds = null;
private Duration timeToLive = null;
public String getKeyspace() {
return this.keyspace;
@@ -82,12 +83,12 @@ public class CassandraChatMemoryProperties extends CommonChatMemoryProperties {
}
@Nullable
public Duration getTimeToLiveSeconds() {
return this.timeToLiveSeconds;
public Duration getTimeToLive() {
return this.timeToLive;
}
public void setTimeToLiveSeconds(Duration timeToLiveSeconds) {
this.timeToLiveSeconds = timeToLiveSeconds;
public void setTimeToLive(Duration timeToLive) {
this.timeToLive = timeToLive;
}
}

View File

@@ -16,6 +16,7 @@
package org.springframework.ai.autoconfigure.chat.memory.cassandra;
import java.time.Duration;
import java.util.List;
import com.datastax.driver.core.utils.UUIDs;
@@ -37,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Mick Semb Wever
* @author Jihoon Kim
* @since 1.0.0
*/
@Testcontainers
@@ -57,7 +59,7 @@ class CassandraChatMemoryAutoConfigurationIT {
this.contextRunner.withPropertyValues("spring.cassandra.contactPoints=" + getContactPointHost())
.withPropertyValues("spring.cassandra.port=" + getContactPointPort())
.withPropertyValues("spring.cassandra.localDatacenter=" + cassandraContainer.getLocalDatacenter())
.withPropertyValues("spring.ai.chat.memory.cassandra.time-to-live=" + getTimeToLive())
.run(context -> {
CassandraChatMemory memory = context.getBean(CassandraChatMemory.class);
@@ -84,6 +86,20 @@ class CassandraChatMemoryAutoConfigurationIT {
.isEqualTo(MessageType.ASSISTANT);
assertThat(memory.get(sessionId, Integer.MAX_VALUE).get(0).getContent()).isEqualTo("test answer");
CassandraChatMemoryProperties properties = context.getBean(CassandraChatMemoryProperties.class);
assertThat(properties.getTimeToLive()).isEqualTo(getTimeToLive());
});
}
@Test
void compareTimeToLive_ISO8601Format() {
this.contextRunner.withPropertyValues("spring.cassandra.contactPoints=" + getContactPointHost())
.withPropertyValues("spring.cassandra.port=" + getContactPointPort())
.withPropertyValues("spring.cassandra.localDatacenter=" + cassandraContainer.getLocalDatacenter())
.withPropertyValues("spring.ai.chat.memory.cassandra.time-to-live=" + getTimeToLiveString())
.run(context -> {
CassandraChatMemoryProperties properties = context.getBean(CassandraChatMemoryProperties.class);
assertThat(properties.getTimeToLive()).isEqualTo(Duration.parse(getTimeToLiveString()));
});
}
@@ -95,4 +111,12 @@ class CassandraChatMemoryAutoConfigurationIT {
return String.valueOf(cassandraContainer.getContactPoint().getPort());
}
private Duration getTimeToLive() {
return Duration.ofSeconds(12000);
}
private String getTimeToLiveString() {
return "PT1M";
}
}

View File

@@ -26,6 +26,7 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Mick Semb Wever
* @author Jihoon Kim
* @since 1.0.0
*/
class CassandraChatMemoryPropertiesTest {
@@ -37,7 +38,7 @@ class CassandraChatMemoryPropertiesTest {
assertThat(props.getTable()).isEqualTo(CassandraChatMemoryConfig.DEFAULT_TABLE_NAME);
assertThat(props.getAssistantColumn()).isEqualTo(CassandraChatMemoryConfig.DEFAULT_ASSISTANT_COLUMN_NAME);
assertThat(props.getUserColumn()).isEqualTo(CassandraChatMemoryConfig.DEFAULT_USER_COLUMN_NAME);
assertThat(props.getTimeToLiveSeconds()).isNull();
assertThat(props.getTimeToLive()).isNull();
assertThat(props.isInitializeSchema()).isTrue();
}
@@ -48,14 +49,14 @@ class CassandraChatMemoryPropertiesTest {
props.setTable("my_table");
props.setAssistantColumn("my_assistant_column");
props.setUserColumn("my_user_column");
props.setTimeToLiveSeconds(Duration.ofDays(1));
props.setTimeToLive(Duration.ofDays(1));
props.setInitializeSchema(false);
assertThat(props.getKeyspace()).isEqualTo("my_keyspace");
assertThat(props.getTable()).isEqualTo("my_table");
assertThat(props.getAssistantColumn()).isEqualTo("my_assistant_column");
assertThat(props.getUserColumn()).isEqualTo("my_user_column");
assertThat(props.getTimeToLiveSeconds()).isEqualTo(Duration.ofDays(1));
assertThat(props.getTimeToLive()).isEqualTo(Duration.ofDays(1));
assertThat(props.isInitializeSchema()).isFalse();
}