refactor(cache): move LargeSoftCache to commons/jvmAndroid, fix ICacheProvider types

- Move LargeSoftCache from amethyst/model to commons/jvmAndroid/model/cache
  so both Android and Desktop share the same WeakReference cache implementation
- Change ICacheProvider return types from Any? to proper types (User?, Note?)
  since these model classes already live in commons
- Remove unnecessary casts in ThreadAssembler, ChatNewMessageState, SearchBarState
- Improve LargeSoftCache.cleanUp() to use single-pass iterator (zero allocation)
- Update Android imports in LocalCache, LargeSoftCacheAddressExt, and test

Per Vitor's feedback on PR #1905: BoundedLargeCache evicts arbitrarily.
LargeSoftCache uses WeakReferences so GC respects the reference graph.
This commit is contained in:
nrobi144
2026-03-24 09:09:29 +02:00
parent 3b489fb3cb
commit 00b06a0e2a
8 changed files with 19 additions and 21 deletions
@@ -52,7 +52,7 @@ class ThreadAssembler(
?.getOrNull(1)
if (markedAsRoot != null) {
// Check to see if there is an error in the tag and the root has replies
val rootNote = cache.getNoteIfExists(markedAsRoot) as? Note
val rootNote = cache.getNoteIfExists(markedAsRoot)
if (rootNote?.replyTo?.isEmpty() == true) {
return cache.checkGetOrCreateNote(markedAsRoot)
}
@@ -57,7 +57,7 @@ interface ICacheProvider {
* @param pubkey The user's public key in hex format
* @return The User if exists in cache, null otherwise
*/
fun getUserIfExists(pubkey: HexKey): Any?
fun getUserIfExists(pubkey: HexKey): User?
/**
* Counts users matching a predicate.
@@ -75,7 +75,7 @@ interface ICacheProvider {
* @param hexKey The note's ID in hex format
* @return The Note if exists in cache, null otherwise
*/
fun getNoteIfExists(hexKey: HexKey): Any?
fun getNoteIfExists(hexKey: HexKey): Note?
/**
* Gets an existing Note or creates a new one if it doesn't exist.
@@ -123,7 +123,7 @@ interface ICacheProvider {
fun findUsersStartingWith(
prefix: String,
limit: Int = 50,
): List<Any> = emptyList()
): List<User> = emptyList()
/**
* Gets or creates a User by public key hex.
@@ -132,7 +132,7 @@ interface ICacheProvider {
* @param pubkey The user's public key in hex format
* @return The User (existing or newly created)
*/
fun getOrCreateUser(pubkey: HexKey): Any?
fun getOrCreateUser(pubkey: HexKey): User?
fun justConsumeMyOwnEvent(event: Event): Boolean
}
@@ -24,7 +24,6 @@ import androidx.compose.runtime.Stable
import androidx.compose.ui.text.input.TextFieldValue
import com.vitorpamplona.amethyst.commons.model.IAccount
import com.vitorpamplona.amethyst.commons.model.Note
import com.vitorpamplona.amethyst.commons.model.User
import com.vitorpamplona.amethyst.commons.model.cache.ICacheProvider
import com.vitorpamplona.quartz.nip01Core.tags.hashtags.hashtags
import com.vitorpamplona.quartz.nip01Core.tags.references.references
@@ -95,7 +94,7 @@ class ChatNewMessageState(
if (currentRoom != null) {
_recipientsMissingDmRelays.value =
currentRoom.users.any { hexKey ->
val user = cache.getOrCreateUser(hexKey) as? User
val user = cache.getOrCreateUser(hexKey)
user?.dmInboxRelays().isNullOrEmpty()
}
} else {
@@ -141,7 +140,7 @@ class ChatNewMessageState(
) {
val pTags =
room.users.mapNotNull { hexKey ->
(cache.getOrCreateUser(hexKey) as? User)?.toPTag()
cache.getOrCreateUser(hexKey)?.toPTag()
}
val replyHint = _replyTo.value?.toEventHint<BaseDMGroupEvent>()
@@ -88,8 +88,7 @@ class SearchBarState(
.debounce(debounceMs)
.onEach { query ->
if (query.length >= 2 && _bech32Results.value.isEmpty()) {
@Suppress("UNCHECKED_CAST")
_cachedUserResults.value = cache.findUsersStartingWith(query, 20) as List<User>
_cachedUserResults.value = cache.findUsersStartingWith(query, 20)
} else {
_cachedUserResults.value = emptyList()
}