Skip to content

Commit

Permalink
feat: LRUキャッシュを利用するようにした。
Browse files Browse the repository at this point in the history
  • Loading branch information
pantasystem committed Nov 23, 2023
1 parent f19e52f commit e39e23e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.pantasystem.milktea.data.infrastructure.instance

import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import net.pantasystem.milktea.common.collection.LRUCache
import net.pantasystem.milktea.model.instance.MastodonInstanceInfo
import javax.inject.Inject
import javax.inject.Singleton
Expand All @@ -12,18 +11,13 @@ import javax.inject.Singleton
@Singleton
class MastodonInstanceInfoCache @Inject constructor(){

private val lock = Mutex()
private var map = mapOf<String, MastodonInstanceInfo>()
private val lruCache = LRUCache<String, MastodonInstanceInfo>(100)

suspend fun put(instanceBaseURL: String, instanceInfo: MastodonInstanceInfo) {
lock.withLock {
map = map.toMutableMap().also { m ->
m[instanceBaseURL] = instanceInfo
}
}
fun put(instanceBaseURL: String, instanceInfo: MastodonInstanceInfo) {
lruCache[instanceBaseURL] = instanceInfo
}

fun get(instanceBaseURL: String): MastodonInstanceInfo? {
return map[instanceBaseURL]
return lruCache[instanceBaseURL]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import kotlinx.coroutines.withContext
import net.pantasystem.milktea.common.runCancellableCatching
import net.pantasystem.milktea.common_android.hilt.IODispatcher
import net.pantasystem.milktea.data.api.mastodon.MastodonAPIProvider
import net.pantasystem.milktea.data.infrastructure.instance.db.*
import net.pantasystem.milktea.data.infrastructure.instance.db.FedibirdCapabilitiesRecord
import net.pantasystem.milktea.data.infrastructure.instance.db.MastodonInstanceInfoDAO
import net.pantasystem.milktea.data.infrastructure.instance.db.MastodonInstanceInfoRecord
import net.pantasystem.milktea.data.infrastructure.instance.db.PleromaMetadataFeatures
import net.pantasystem.milktea.data.infrastructure.instance.db.from
import net.pantasystem.milktea.data.infrastructure.instance.db.toModel
import net.pantasystem.milktea.data.infrastructure.toModel
import net.pantasystem.milktea.model.instance.MastodonInstanceInfo
import net.pantasystem.milktea.model.instance.MastodonInstanceInfoRepository
Expand All @@ -26,7 +31,7 @@ class MastodonInstanceInfoRepositoryImpl @Inject constructor(
override suspend fun find(instanceDomain: String): Result<MastodonInstanceInfo> =
runCancellableCatching {
withContext(ioDispatcher) {
var info = get(instanceDomain)
var info = cache.get(instanceDomain)
if (info != null) {
return@withContext info
}
Expand All @@ -43,9 +48,6 @@ class MastodonInstanceInfoRepositoryImpl @Inject constructor(
}
}

override fun get(instanceDomain: String): MastodonInstanceInfo? {
return cache.get(instanceDomain)
}

override fun observe(instanceDomain: String): Flow<MastodonInstanceInfo?> {
return mastodonInstanceInfoDAO.observeBy(URL(instanceDomain).host).map {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.pantasystem.milktea.data.infrastructure.instance

import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import net.pantasystem.milktea.common.collection.LRUCache
import net.pantasystem.milktea.model.instance.Meta
import javax.inject.Inject
import javax.inject.Singleton
Expand All @@ -12,18 +11,13 @@ import javax.inject.Singleton
@Singleton
class MetaCache @Inject constructor(){

private val lock = Mutex()
private var map = mapOf<String, Meta>()
private val lruCache = LRUCache<String, Meta>(100)

suspend fun put(instanceBaseURL: String, meta: Meta) {
lock.withLock {
map = map.toMutableMap().also { m ->
m[instanceBaseURL] = meta
}
}
fun put(instanceBaseURL: String, meta: Meta) {
lruCache[instanceBaseURL] = meta
}

fun get(instanceBaseURL: String): Meta? {
return map[instanceBaseURL]
return lruCache[instanceBaseURL]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@ interface MastodonInstanceInfoRepository {

fun observe(instanceDomain: String): Flow<MastodonInstanceInfo?>

fun get(instanceDomain: String): MastodonInstanceInfo?

suspend fun find(instanceDomain: String): Result<MastodonInstanceInfo>
}

0 comments on commit e39e23e

Please sign in to comment.