redis / redis-om-spring

Spring Data Redis extensions for better search, documents models, and more
MIT License
609 stars 94 forks source link

Time to live not set when using save when using `@PostConstruct` #458

Open GohKianSeng opened 5 months ago

GohKianSeng commented 5 months ago

hello this is my entity model. set the time to live but in redis insight, show no limit.

image image

`@Document("TestResultRedis", timeToLive = 5 * 60) data class TestResultRedisModel( @Id val id: Long?,

@Searchable
val uuid: String?,

@Searchable
val uatId: String?,

@Searchable
val msgType: String?,

@Searchable
val filename: String?,

@Searchable
val status: String?,

@Searchable
val result: String?,

val overrideStatus: String?,

val remarksSnapshotId: Long?,

@Searchable
val actualCodes: List<String>,

val mappedActualCodes: List<String>,

@Searchable
val expectedCodes: List<String>,

val mappedExpectedCodes: List<String>,

@Searchable
val expectedUserCodes: List<String>,

val batchJobStatus: String?,

val expectedBatchJobStatus: String?,

val expectedUserBatchJobStatus: String?,

@Searchable
val remark: String?,

val remarkDate: String?,

val remarkUuid: String?,

@Searchable
val remarkHashTags: List<String>?,

@Searchable
val assignee: String?,

val user: String?,

@Searchable
var kivIndicator: Boolean?,

@Searchable
val manualIndicator: Boolean?,

val overwrite: Boolean?

)`

GohKianSeng commented 5 months ago

just realised if i used saveAll. the Time to live is set. is that a bug?

image
bsbodden commented 5 months ago

Sounds like a bug! Did you just used save?

GohKianSeng commented 5 months ago

yes i used repository.save

GohKianSeng commented 5 months ago

hello @bsbodden for saveAll also has issue i guess.

the controller save() has TTL set.

image

but using PostConstruct doesn't work

image

Redis Insight

image
bsbodden commented 5 months ago

@GohKianSeng I can't seem to be able to replicate the issue, could you create a simple reproducer app? Just models, repository and a CommandLineRunner to run your scenario?

bsbodden commented 5 months ago

@GohKianSeng I don't think we have added support for @PostConstruct, that might be the root of the issue here

nexlink-giangbui commented 2 months ago

Hi @bsbodden,

I discovered that in a Spring Boot project, when using spring-boot-devtools, the application runs with the org.springframework.boot.devtools.restart.classloader.RestartClassLoader class loader, instead of the jdk.internal.loader.ClassLoaders class loader (which is used by Spring OM). This difference causes an issue where Spring OM cannot load the KeyspaceConfiguration for an entity in the method:

com.redis.om.spring.RedisJSONKeyValueAdapter.getTTLForEntity(Object entity)

The problem arises because the line:

entityClassKey = ClassLoader.getSystemClassLoader().loadClass(entity.getClass().getTypeName())

returns an entityClassKey that is not present in the KeyspaceConfiguration.settingsMap

In the context of @SpringBootTest, the jdk.internal.loader.ClassLoaders class loader is used, which is why TTL works in the unit test environment, but fails in the application context when spring-boot-devtools is enabled.

A temporary fix for this issue is to disable spring-boot-devtools.

bsbodden commented 2 months ago

@nexlink-giangbui thanks for the investigation! The RestartClassLoader getting in the way of low level stuff is a recurring theme, I'm looking into ways to get to the correct classloader in that scenario. Stay tuned!