Skip to content

Commit

Permalink
Fixes #993
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorpamplona committed Jul 31, 2024
1 parent b29ccd9 commit 231af1d
Showing 1 changed file with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,35 @@ import com.vitorpamplona.amethyst.commons.richtext.RichTextViewerState
import com.vitorpamplona.quartz.events.ImmutableListOfLists

object CachedRichTextParser {
val richTextCache = LruCache<String, RichTextViewerState>(50)
val richTextCache = LruCache<Int, RichTextViewerState>(50)

fun getCached(content: String): RichTextViewerState? = richTextCache[content]
// fun getCached(content: String): RichTextViewerState? = richTextCache[content]

fun hashCodeCache(
content: String,
tags: ImmutableListOfLists<String>,
callbackUri: String?,
): Int {
var result = content.hashCode()
result = 31 * result + tags.lists.hashCode()
if (callbackUri != null) {
result = 31 * result + callbackUri.hashCode()
}
return result
}

fun parseText(
content: String,
tags: ImmutableListOfLists<String>,
callbackUri: String? = null,
): RichTextViewerState {
val cached = richTextCache[content]
val key = hashCodeCache(content, tags, callbackUri)
val cached = richTextCache[key]
return if (cached != null) {
cached
} else {
val newUrls = RichTextParser().parseText(content, tags, callbackUri)
richTextCache.put(content, newUrls)
richTextCache.put(key, newUrls)
newUrls
}
}
Expand Down

0 comments on commit 231af1d

Please sign in to comment.