Skip to content

Commit 4f152c6

Browse files
committed
🐛 [useTableRequest]: Fix cache key conflict on page switch
- Auto-generate unique cache keys per page (e.g., users_p0_s10) - Use custom setCache/getCache to append page params to cacheKey - Change getCache return type to nullable for proper cache miss handling - Add caching example in UseTableRequestExample
1 parent b4b42f8 commit 4f152c6

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

app/src/commonMain/kotlin/xyz/junerver/composehooks/example/UseTableRequestExample.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import xyz.junerver.compose.hooks.userequest.useTableRequest
1919
import xyz.junerver.compose.hooks.usetable.core.column
2020
import xyz.junerver.compose.hooks.usetable.useTable
2121
import kotlin.math.ceil
22+
import kotlin.time.Duration.Companion.seconds
2223

2324
/**
2425
* Example demonstrating useTableRequest hook.
@@ -52,6 +53,9 @@ fun UseTableRequestExample() {
5253
optionsOf = {
5354
initialPageSize = 5
5455
requestOptions = {
56+
// Per-page caching: each page is cached independently
57+
cacheKey = "users-table"
58+
staleTime = 30.seconds
5559
onSuccess = { data, _ ->
5660
println("Loaded ${data?.rows?.size ?: 0} users")
5761
}

hooks/src/commonMain/kotlin/xyz/junerver/compose/hooks/userequest/UseRequestOptions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ data class UseRequestOptions<TParams, TData> internal constructor(
122122
@Stable
123123
var setCache: ((data: CachedData<TData>) -> Unit)? = null,
124124
@Stable
125-
var getCache: ((params: TParams) -> CachedData<TData>)? = null,
125+
var getCache: ((params: TParams) -> CachedData<TData>?)? = null,
126126
/**
127127
* By setting options.[loadingDelay], you can delay the time when [FetchState.loading] becomes true, effectively preventing flickering.
128128
* For example, when an interface normally returns quickly, if we use it conventionally, flickering will occur. After the request is initiated, it changes very quickly from false -> true -> false;

hooks/src/commonMain/kotlin/xyz/junerver/compose/hooks/userequest/UseTableRequest.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import androidx.compose.runtime.remember
77
import xyz.junerver.compose.hooks._useState
88
import xyz.junerver.compose.hooks.useEffect
99
import xyz.junerver.compose.hooks.useLatestRef
10+
import xyz.junerver.compose.hooks.userequest.utils.CachedData
11+
import xyz.junerver.compose.hooks.utils.CacheManager
1012

1113
/**
1214
* Paged request parameters.
@@ -163,6 +165,24 @@ fun <T> useTableRequest(
163165
manual = true
164166
defaultParams = PageParams(latestPage.current, latestPageSize.current)
165167
options.requestOptions(this)
168+
// Per-page caching: if user set cacheKey, use custom cache functions
169+
// to generate unique keys for each page
170+
if (cacheKey.isNotEmpty()) {
171+
val baseCacheKey = cacheKey
172+
setCache = { cachedData ->
173+
val params = cachedData.params as? PageParams
174+
val key = if (params != null) {
175+
"${baseCacheKey}_p${params.page}_s${params.pageSize}"
176+
} else {
177+
baseCacheKey
178+
}
179+
CacheManager.saveCache(key, cacheTime, cachedData)
180+
}
181+
getCache = { params ->
182+
val key = "${baseCacheKey}_p${params.page}_s${params.pageSize}"
183+
CacheManager.getCache<TableResult<T>>(key)
184+
}
185+
}
166186
}
167187
)
168188

0 commit comments

Comments
 (0)