Skip to content

Commit f94c537

Browse files
committed
Add LRU cache for content
1 parent f1ae3ea commit f94c537

3 files changed

Lines changed: 43 additions & 15 deletions

File tree

composeApp/src/androidMain/kotlin/com/linuxcommandlibrary/app/platform/AndroidAssetReader.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class AndroidAssetReader(private val context: Context) : AssetReader {
1313
}
1414

1515
override fun readFile(path: String): String? = try {
16-
context.assets.open(path).bufferedReader().readText()
16+
context.assets.open(path).bufferedReader().use { it.readText() }
1717
} catch (e: Exception) {
1818
null
1919
}

composeApp/src/commonMain/kotlin/com/linuxcommandlibrary/app/data/BasicsRepository.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ import com.linuxcommandlibrary.shared.platform.AssetReader
77

88
class BasicsRepository(private val assetReader: AssetReader) {
99

10+
private var cachedCategories: List<BasicCategory>? = null
11+
private val cachedGroupsAndCommands = mutableMapOf<String, Pair<List<BasicGroup>, Map<Long, List<BasicCommand>>>>()
12+
1013
fun getCategories(): List<BasicCategory> {
14+
cachedCategories?.let { return it }
15+
1116
val files = assetReader.listFiles("basics")
1217

13-
return files
18+
val categories = files
1419
.filter { it.endsWith(".md") }
1520
.mapNotNull { filename ->
1621
val id = filename.removeSuffix(".md")
@@ -22,6 +27,9 @@ class BasicsRepository(private val assetReader: AssetReader) {
2227
}
2328
}
2429
.sortedBy { basicsSortOrder.indexOf(it.title) }
30+
31+
cachedCategories = categories
32+
return categories
2533
}
2634

2735
private fun readCategoryTitle(filename: String): String? = try {
@@ -32,6 +40,8 @@ class BasicsRepository(private val assetReader: AssetReader) {
3240
}
3341

3442
fun getGroupsAndCommands(categoryId: String): Pair<List<BasicGroup>, Map<Long, List<BasicCommand>>> {
43+
cachedGroupsAndCommands[categoryId]?.let { return it }
44+
3545
val groups = mutableListOf<BasicGroup>()
3646
val commandsByGroupId = mutableMapOf<Long, MutableList<BasicCommand>>()
3747

@@ -63,7 +73,9 @@ class BasicsRepository(private val assetReader: AssetReader) {
6373
// Return empty on error
6474
}
6575

66-
return Pair(groups, commandsByGroupId)
76+
val result = Pair<List<BasicGroup>, Map<Long, List<BasicCommand>>>(groups, commandsByGroupId)
77+
cachedGroupsAndCommands[categoryId] = result
78+
return result
6779
}
6880

6981
fun getBasicInfo(categoryId: String): BasicInfo? = try {

composeApp/src/commonMain/kotlin/com/linuxcommandlibrary/app/data/CommandsRepository.kt

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ class CommandsRepository(private val assetReader: AssetReader) {
88
private var cachedCommands: List<CommandInfo>? = null
99
private var cachedCommandNames: Set<String>? = null
1010

11+
private val sectionsCache = linkedMapOf<String, List<CommandSectionInfo>>()
12+
private val sectionsCacheMaxSize = 50
13+
1114
fun getCommands(): List<CommandInfo> {
1215
cachedCommands?.let { return it }
1316

@@ -51,18 +54,31 @@ class CommandsRepository(private val assetReader: AssetReader) {
5154
return name in names
5255
}
5356

54-
fun getSections(commandName: String): List<CommandSectionInfo> = try {
55-
val content = assetReader.readFile("commands/$commandName.md") ?: return emptyList()
57+
fun getSections(commandName: String): List<CommandSectionInfo> {
58+
sectionsCache.remove(commandName)?.let {
59+
sectionsCache[commandName] = it
60+
return it
61+
}
5662

57-
MarkdownParser.splitByHeaders(content, "# ").map { (title, sectionContent) ->
58-
CommandSectionInfo(
59-
id = (commandName + title).hashCode().toLong(),
60-
title = title,
61-
content = sectionContent,
62-
)
63-
}.filter { it.title.uppercase() != "TAGLINE" }
64-
.sortedBy { it.getSortPriority() }
65-
} catch (e: Exception) {
66-
emptyList()
63+
val sections = try {
64+
val content = assetReader.readFile("commands/$commandName.md") ?: return emptyList()
65+
66+
MarkdownParser.splitByHeaders(content, "# ").map { (title, sectionContent) ->
67+
CommandSectionInfo(
68+
id = (commandName + title).hashCode().toLong(),
69+
title = title,
70+
content = sectionContent,
71+
)
72+
}.filter { it.title.uppercase() != "TAGLINE" }
73+
.sortedBy { it.getSortPriority() }
74+
} catch (e: Exception) {
75+
emptyList()
76+
}
77+
78+
sectionsCache[commandName] = sections
79+
if (sectionsCache.size > sectionsCacheMaxSize) {
80+
sectionsCache.remove(sectionsCache.keys.first())
81+
}
82+
return sections
6783
}
6884
}

0 commit comments

Comments
 (0)