From 22d128a7a9031ee71e600201956f7240fb1ccbe9 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Mon, 27 Feb 2023 00:52:48 +0900 Subject: [PATCH 1/7] =?UTF-8?q?feat:=20=E3=82=AD=E3=83=A3=E3=83=83?= =?UTF-8?q?=E3=82=B7=E3=83=A5API=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dev/usbharu/multim/cache/Cacheable.kt | 5 ++ .../multim/cache/CacheableAccountApi.kt | 48 +++++++++++++++++++ .../dev/usbharu/multim/cache/CacheableApi.kt | 48 +++++++++++++++++++ .../dev/usbharu/multim/model/Account.kt | 4 +- .../dev/usbharu/multim/model/StatusId.kt | 4 +- 5 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 core/src/main/kotlin/dev/usbharu/multim/cache/Cacheable.kt create mode 100644 core/src/main/kotlin/dev/usbharu/multim/cache/CacheableAccountApi.kt create mode 100644 core/src/main/kotlin/dev/usbharu/multim/cache/CacheableApi.kt diff --git a/core/src/main/kotlin/dev/usbharu/multim/cache/Cacheable.kt b/core/src/main/kotlin/dev/usbharu/multim/cache/Cacheable.kt new file mode 100644 index 0000000..29c7884 --- /dev/null +++ b/core/src/main/kotlin/dev/usbharu/multim/cache/Cacheable.kt @@ -0,0 +1,5 @@ +package dev.usbharu.multim.cache + +interface Cacheable { + val cacheKey:String +} diff --git a/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableAccountApi.kt b/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableAccountApi.kt new file mode 100644 index 0000000..7654826 --- /dev/null +++ b/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableAccountApi.kt @@ -0,0 +1,48 @@ +package dev.usbharu.multim.cache + +import com.github.michaelbull.result.Result +import dev.usbharu.multim.api.AccountApi +import dev.usbharu.multim.error.MultiMError +import dev.usbharu.multim.model.* + +class CacheableAccountApi( + private val accountApi: AccountApi, + private val cacheableApi: CacheableApi +) : + CacheableApi by cacheableApi, AccountApi by accountApi { + override suspend fun userTimeline( + account: Account, + since: StatusId?, + until: StatusId? + ): Result, MultiMError> { + return cacheableApi.cacheOrGet( + CacheableApi.generateKey( + account, + since, + until + ) + ) { accountApi.userTimeline(account, since, until) } + } + override suspend fun profile(account: Account): Result { + return cacheableApi.cacheOrGet(account) { accountApi.profile(account) } + } + + override suspend fun statuses( + account: Account, + includeRepost: Boolean + ): Result, MultiMError> { + return cacheableApi.cacheOrGet(CacheableApi.generateKey(account) + includeRepost) { + accountApi.statuses( + account, + includeRepost + ) + } + } + + override suspend fun relationships( + myself: Account, + other: Account + ): Result { + return cacheableApi.cacheOrGet(myself, other) { accountApi.relationships(myself, other) } + } +} diff --git a/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableApi.kt b/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableApi.kt new file mode 100644 index 0000000..6bacb7f --- /dev/null +++ b/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableApi.kt @@ -0,0 +1,48 @@ +package dev.usbharu.multim.cache + +import kotlinx.datetime.Clock + +interface CacheableApi { + + companion object { + fun generateKey(vararg objects: Cacheable?): String { + var key: String = Clock.System.now().toString() + for (cacheable in objects) { + key += cacheable?.cacheKey + } + return key + } + } + + fun purge() + fun disable() + fun enable() + fun withNoCache(block: () -> Unit) { + disable() + try { + block() + } finally { + enable() + } + } + + fun cache(key: String, value: T): T + + fun get(key: String): T? + suspend fun cacheOrGet(key: String, block: suspend () -> T): T { + val get = get(key) + if (get != null) { + return get + } + return cache(key, block()) + } + + suspend fun cacheOrGet(vararg objects: Cacheable?, block: suspend () -> T): T { + val key = generateKey(*objects) + val get = get(key) + if (get != null) { + return get + } + return cache(key, block()) + } +} diff --git a/core/src/main/kotlin/dev/usbharu/multim/model/Account.kt b/core/src/main/kotlin/dev/usbharu/multim/model/Account.kt index 4c6a7c7..c7cc553 100644 --- a/core/src/main/kotlin/dev/usbharu/multim/model/Account.kt +++ b/core/src/main/kotlin/dev/usbharu/multim/model/Account.kt @@ -1,8 +1,10 @@ package dev.usbharu.multim.model +import dev.usbharu.multim.cache.Cacheable + abstract class Account( val screenName: String, val accountName: String, val isBot: Boolean? = false, val avatar: Avatar -) +) : Cacheable diff --git a/core/src/main/kotlin/dev/usbharu/multim/model/StatusId.kt b/core/src/main/kotlin/dev/usbharu/multim/model/StatusId.kt index 748d141..1a31b27 100644 --- a/core/src/main/kotlin/dev/usbharu/multim/model/StatusId.kt +++ b/core/src/main/kotlin/dev/usbharu/multim/model/StatusId.kt @@ -1,6 +1,8 @@ package dev.usbharu.multim.model -abstract class StatusId { +import dev.usbharu.multim.cache.Cacheable + +abstract class StatusId : Cacheable { abstract override fun equals(other: Any?): Boolean abstract override fun hashCode(): Int abstract fun getUrl(): String From 22345e34b3bf22c3f4cd15ed31a2c69c1962351b Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Mon, 27 Feb 2023 11:26:54 +0900 Subject: [PATCH 2/7] =?UTF-8?q?feat:=20=E3=82=AD=E3=83=A3=E3=83=83?= =?UTF-8?q?=E3=82=B7=E3=83=A5API=E3=81=AE=E3=82=A4=E3=83=B3=E3=82=BF?= =?UTF-8?q?=E3=83=95=E3=82=A7=E3=83=BC=E3=82=B9=E3=82=92=E3=81=99=E3=81=B9?= =?UTF-8?q?=E3=81=A6=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dev/usbharu/multim/api/AccountApi.kt | 23 ++++++++++++++ .../kotlin/dev/usbharu/multim/api/EmojiApi.kt | 5 +++ .../dev/usbharu/multim/api/StatusApi.kt | 28 ++++++++++++++++- .../dev/usbharu/multim/api/TimelineApi.kt | 8 +++++ .../multim/cache/CacheableAccountApi.kt | 15 +++++++-- .../dev/usbharu/multim/cache/CacheableApi.kt | 20 +++++++----- .../usbharu/multim/cache/CacheableEmojiApi.kt | 16 ++++++++++ .../multim/cache/CacheableStatusApi.kt | 31 +++++++++++++++++++ .../multim/cache/CacheableTimelineApi.kt | 13 ++++++++ 9 files changed, 147 insertions(+), 12 deletions(-) create mode 100644 core/src/main/kotlin/dev/usbharu/multim/cache/CacheableEmojiApi.kt create mode 100644 core/src/main/kotlin/dev/usbharu/multim/cache/CacheableStatusApi.kt create mode 100644 core/src/main/kotlin/dev/usbharu/multim/cache/CacheableTimelineApi.kt diff --git a/core/src/main/kotlin/dev/usbharu/multim/api/AccountApi.kt b/core/src/main/kotlin/dev/usbharu/multim/api/AccountApi.kt index 74a7689..966d1e9 100644 --- a/core/src/main/kotlin/dev/usbharu/multim/api/AccountApi.kt +++ b/core/src/main/kotlin/dev/usbharu/multim/api/AccountApi.kt @@ -16,13 +16,36 @@ interface AccountApi { until: StatusId? = null ): Result,MultiMError> + val FOLLOW: String + get() = "account/follow" + suspend fun follow(account: Account): Result + + val UNFOLLOW: String + get() = "account/unfollow" suspend fun unfollow(account: Account): Result + + val PROFILE: String + get() = "account/profile" suspend fun profile(account: Account): Result + + val STATUSES:String + get() = "account/statuses" suspend fun statuses(account: Account, includeRepost: Boolean = false): Result,MultiMError> + + val RELEATIONSHIPS:String + get() = "account/relationships" suspend fun relationships(myself: Account, other: Account): Result + + val REQUEST_CANCEL:String + get() = "account/requestCancel" suspend fun requestCancel(account: Account): Result + + val REQUEST_ACCEPT:String + get() = "account/requestAccept" suspend fun requestAccept(account: Account): Result + val REQUEST_REJECT:String + get() = "account/requestReject" suspend fun requestReject(account: Account): Result } diff --git a/core/src/main/kotlin/dev/usbharu/multim/api/EmojiApi.kt b/core/src/main/kotlin/dev/usbharu/multim/api/EmojiApi.kt index 1fdc3ac..836192e 100644 --- a/core/src/main/kotlin/dev/usbharu/multim/api/EmojiApi.kt +++ b/core/src/main/kotlin/dev/usbharu/multim/api/EmojiApi.kt @@ -8,8 +8,13 @@ import dev.usbharu.multim.error.MultiMResult import dev.usbharu.multim.model.Emoji interface EmojiApi { + val GET:String + get() = "emoji/get" suspend fun get(name:String):MultiMResult + val FIND_BY_NAME:String + get() = "emoji/findByName" + suspend fun findByName(name:String):MultiMResult> } diff --git a/core/src/main/kotlin/dev/usbharu/multim/api/StatusApi.kt b/core/src/main/kotlin/dev/usbharu/multim/api/StatusApi.kt index 7e4acce..fd3c921 100644 --- a/core/src/main/kotlin/dev/usbharu/multim/api/StatusApi.kt +++ b/core/src/main/kotlin/dev/usbharu/multim/api/StatusApi.kt @@ -12,12 +12,22 @@ import kotlinx.datetime.TimeZone import kotlinx.datetime.toLocalDateTime interface StatusApi { + val POST:String + get() = "status/post" suspend fun post(status: StatusForPost): Result + + val DELETE:String + get() = "status/delete" suspend fun delete(id: StatusId): Result + val FIND_BY_ID:String + get() = "status/findById" suspend fun findById(id: StatusId): Result + val ADD_REACTION:String + get() = "status/addReaction" suspend fun addReaction(id: StatusId, reaction: Reaction): Result - + val REMOVE_REACTION:String + get() = "status/remvoeReaction" /** * Remove reaction * @@ -26,13 +36,29 @@ interface StatusApi { * @return */ suspend fun removeReaction(id: StatusId, reaction: Reaction?): Result + val REACTIONS:String + get() = "status/reactions" suspend fun reactions(id: StatusId): Result,MultiMError> + val REPLIES:String + get() = "status/replies" suspend fun replies(id: StatusId): Result,MultiMError> + val REPOST:String + get() = "status/repost" suspend fun repost(id: StatusId): Result + val UN_REPOST:String + get() = "status/unRepost" suspend fun unRepost(id: StatusId): Result + val REPLY_TO:String + get() = "status/replyTo" suspend fun replyTo(id: StatusId, status: StatusForPost): Result + val ADD_TO_BOOKMARKS:String + get() = "status/addBookMarks" suspend fun addToBookmarks(id: StatusId): Result + val REMOVE_FROM_BOOKMARKS:String + get() = "status/removeFromBookmarks" suspend fun removeFromBookmarks(id: StatusId): Result + val GET_PREVIOUS_AND_NEXT:String + get() = "status/getPreviousAndNext" suspend fun getPreviousAndNext(id: StatusId): Result fun getUniqueId(status: Status):Int{ diff --git a/core/src/main/kotlin/dev/usbharu/multim/api/TimelineApi.kt b/core/src/main/kotlin/dev/usbharu/multim/api/TimelineApi.kt index c79d192..58d9770 100644 --- a/core/src/main/kotlin/dev/usbharu/multim/api/TimelineApi.kt +++ b/core/src/main/kotlin/dev/usbharu/multim/api/TimelineApi.kt @@ -9,12 +9,20 @@ import dev.usbharu.multim.model.Status import dev.usbharu.multim.model.Timeline interface TimelineApi { + val AVAILABLE_TIMELINES:String + get() = "timeline/availableTimelines" suspend fun availableTimelines(): Result,MultiMError> + val LISTEN:String + get() = "timeline/listen" suspend fun listen(timeline: Timeline,callback: (List) -> Unit):Result + val CONNECT:String + get() = "timeline/connect" // todo 返り値を詳細にする suspend fun connect(timeline: Timeline): Result + val DISCONNECT:String + get() = "timeline/disconnect" /** * Disconnect diff --git a/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableAccountApi.kt b/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableAccountApi.kt index 7654826..9cfd588 100644 --- a/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableAccountApi.kt +++ b/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableAccountApi.kt @@ -23,15 +23,19 @@ class CacheableAccountApi( ) ) { accountApi.userTimeline(account, since, until) } } + override suspend fun profile(account: Account): Result { - return cacheableApi.cacheOrGet(account) { accountApi.profile(account) } + return cacheableApi.cacheOrGet(PROFILE, account) { accountApi.profile(account) } } override suspend fun statuses( account: Account, includeRepost: Boolean ): Result, MultiMError> { - return cacheableApi.cacheOrGet(CacheableApi.generateKey(account) + includeRepost) { + return cacheableApi.cacheOrGet( + STATUSES, + CacheableApi.generateKey(account) + includeRepost + ) { accountApi.statuses( account, includeRepost @@ -43,6 +47,11 @@ class CacheableAccountApi( myself: Account, other: Account ): Result { - return cacheableApi.cacheOrGet(myself, other) { accountApi.relationships(myself, other) } + return cacheableApi.cacheOrGet(RELEATIONSHIPS, myself, other) { + accountApi.relationships( + myself, + other + ) + } } } diff --git a/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableApi.kt b/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableApi.kt index 6bacb7f..839fa0c 100644 --- a/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableApi.kt +++ b/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableApi.kt @@ -26,23 +26,27 @@ interface CacheableApi { } } - fun cache(key: String, value: T): T + fun cache(method: String, key: String, value: T): T - fun get(key: String): T? - suspend fun cacheOrGet(key: String, block: suspend () -> T): T { - val get = get(key) + fun get(method: String, key: String): T? + suspend fun cacheOrGet(method: String, key: String, block: suspend () -> T): T { + val get = get(method, key) if (get != null) { return get } - return cache(key, block()) + return cache(method, key, block()) } - suspend fun cacheOrGet(vararg objects: Cacheable?, block: suspend () -> T): T { + suspend fun cacheOrGet( + method: String, + vararg objects: Cacheable?, + block: suspend () -> T + ): T { val key = generateKey(*objects) - val get = get(key) + val get = get(method, key) if (get != null) { return get } - return cache(key, block()) + return cache(method, key, block()) } } diff --git a/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableEmojiApi.kt b/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableEmojiApi.kt new file mode 100644 index 0000000..9b65a01 --- /dev/null +++ b/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableEmojiApi.kt @@ -0,0 +1,16 @@ +package dev.usbharu.multim.cache + +import dev.usbharu.multim.api.EmojiApi +import dev.usbharu.multim.error.MultiMResult +import dev.usbharu.multim.model.Emoji + +class CacheableEmojiApi(val cacheableApi: CacheableApi, val emojiApi: EmojiApi) : + CacheableApi by cacheableApi, EmojiApi by emojiApi { + override suspend fun get(name: String): MultiMResult { + return cacheableApi.cacheOrGet(GET, name) { emojiApi.get(name) } + } + + override suspend fun findByName(name: String): MultiMResult> { + return cacheableApi.cacheOrGet(FIND_BY_NAME, name) { emojiApi.findByName(name) } + } +} diff --git a/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableStatusApi.kt b/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableStatusApi.kt new file mode 100644 index 0000000..3995a4e --- /dev/null +++ b/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableStatusApi.kt @@ -0,0 +1,31 @@ +package dev.usbharu.multim.cache + +import com.github.michaelbull.result.Result +import dev.usbharu.multim.api.StatusApi +import dev.usbharu.multim.error.MultiMError +import dev.usbharu.multim.model.PreviousAndNextPosts +import dev.usbharu.multim.model.Reaction +import dev.usbharu.multim.model.Status +import dev.usbharu.multim.model.StatusId + +class CacheableStatusApi(val cacheableApi: CacheableApi, val statusApi: StatusApi) : + CacheableApi by cacheableApi, StatusApi by statusApi { + override suspend fun findById(id: StatusId): Result { + return cacheableApi.cacheOrGet(FIND_BY_ID, id) { statusApi.findById(id) } + } + + override suspend fun reactions(id: StatusId): Result, MultiMError> { + return cacheableApi.cacheOrGet(REACTIONS, id) { statusApi.reactions(id) } + } + + override suspend fun replies(id: StatusId): Result, MultiMError> { + return cacheableApi.cacheOrGet(REPLIES, id) { statusApi.replies(id) } + } + + override suspend fun getPreviousAndNext(id: StatusId): Result { + return cacheableApi.cacheOrGet( + GET_PREVIOUS_AND_NEXT, + id + ) { statusApi.getPreviousAndNext(id) } + } +} diff --git a/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableTimelineApi.kt b/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableTimelineApi.kt new file mode 100644 index 0000000..da083ce --- /dev/null +++ b/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableTimelineApi.kt @@ -0,0 +1,13 @@ +package dev.usbharu.multim.cache + +import com.github.michaelbull.result.Result +import dev.usbharu.multim.api.TimelineApi +import dev.usbharu.multim.error.MultiMError +import dev.usbharu.multim.model.Timeline + +class CacheableTimelineApi(val cacheableApi: CacheableApi, val timelineApi: TimelineApi) : + CacheableApi by cacheableApi, TimelineApi by timelineApi { + override suspend fun availableTimelines(): Result, MultiMError> { + return cacheableApi.cacheOrGet(AVAILABLE_TIMELINES) { timelineApi.availableTimelines() } + } +} From 474cfb89ef6e5306f0b739c16081d38ec3f29429 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Mon, 27 Feb 2023 12:03:49 +0900 Subject: [PATCH 3/7] =?UTF-8?q?feat:=20=E3=82=A4=E3=83=B3=E3=83=A1?= =?UTF-8?q?=E3=83=A2=E3=83=AA=E3=82=AD=E3=83=A3=E3=83=83=E3=82=B7=E3=83=A5?= =?UTF-8?q?=E3=82=92=E5=AE=9F=E8=A3=85=E3=80=81=E3=81=9D=E3=82=8C=E3=81=AB?= =?UTF-8?q?=E4=BC=B4=E3=81=86=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 1 + .../dev/usbharu/multim/cache/CacheableApi.kt | 6 ++-- .../usbharu/multim/cache/CacheableEmojiApi.kt | 2 +- .../multim/cache/CacheableStatusApi.kt | 2 +- .../multim/cache/CacheableTimelineApi.kt | 2 +- .../usbharu/multim/cache/ImMemoryLRUCache.kt | 34 +++++++++++++++++++ 6 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 core/src/main/kotlin/dev/usbharu/multim/cache/ImMemoryLRUCache.kt diff --git a/build.gradle.kts b/build.gradle.kts index 5bc975b..f889441 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -94,6 +94,7 @@ subprojects { "implementation"("io.ktor:ktor-serialization-kotlinx-json:$ktor_version") "implementation"("dev.usbharu:kmp-logger:1.1.0") "implementation"("com.michael-bull.kotlin-result:kotlin-result:1.1.16") + "implementation"("io.github.reactivecircus.cache4k:cache4k:0.9.0") "testImplementation"("org.junit.jupiter:junit-jupiter-api:5.9.0") "testImplementation"("org.junit.jupiter:junit-jupiter-params:5.9.0") diff --git a/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableApi.kt b/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableApi.kt index 839fa0c..ece8879 100644 --- a/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableApi.kt +++ b/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableApi.kt @@ -26,10 +26,10 @@ interface CacheableApi { } } - fun cache(method: String, key: String, value: T): T + fun cache(method: String, key: String, value: T): T fun get(method: String, key: String): T? - suspend fun cacheOrGet(method: String, key: String, block: suspend () -> T): T { + suspend fun cacheOrGet(method: String, key: String, block: suspend () -> T): T { val get = get(method, key) if (get != null) { return get @@ -37,7 +37,7 @@ interface CacheableApi { return cache(method, key, block()) } - suspend fun cacheOrGet( + suspend fun cacheOrGet( method: String, vararg objects: Cacheable?, block: suspend () -> T diff --git a/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableEmojiApi.kt b/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableEmojiApi.kt index 9b65a01..96f87e7 100644 --- a/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableEmojiApi.kt +++ b/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableEmojiApi.kt @@ -4,7 +4,7 @@ import dev.usbharu.multim.api.EmojiApi import dev.usbharu.multim.error.MultiMResult import dev.usbharu.multim.model.Emoji -class CacheableEmojiApi(val cacheableApi: CacheableApi, val emojiApi: EmojiApi) : +class CacheableEmojiApi(private val cacheableApi: CacheableApi,private val emojiApi: EmojiApi) : CacheableApi by cacheableApi, EmojiApi by emojiApi { override suspend fun get(name: String): MultiMResult { return cacheableApi.cacheOrGet(GET, name) { emojiApi.get(name) } diff --git a/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableStatusApi.kt b/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableStatusApi.kt index 3995a4e..8219d35 100644 --- a/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableStatusApi.kt +++ b/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableStatusApi.kt @@ -8,7 +8,7 @@ import dev.usbharu.multim.model.Reaction import dev.usbharu.multim.model.Status import dev.usbharu.multim.model.StatusId -class CacheableStatusApi(val cacheableApi: CacheableApi, val statusApi: StatusApi) : +class CacheableStatusApi(private val cacheableApi: CacheableApi,private val statusApi: StatusApi) : CacheableApi by cacheableApi, StatusApi by statusApi { override suspend fun findById(id: StatusId): Result { return cacheableApi.cacheOrGet(FIND_BY_ID, id) { statusApi.findById(id) } diff --git a/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableTimelineApi.kt b/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableTimelineApi.kt index da083ce..f40c197 100644 --- a/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableTimelineApi.kt +++ b/core/src/main/kotlin/dev/usbharu/multim/cache/CacheableTimelineApi.kt @@ -5,7 +5,7 @@ import dev.usbharu.multim.api.TimelineApi import dev.usbharu.multim.error.MultiMError import dev.usbharu.multim.model.Timeline -class CacheableTimelineApi(val cacheableApi: CacheableApi, val timelineApi: TimelineApi) : +class CacheableTimelineApi(private val cacheableApi: CacheableApi,private val timelineApi: TimelineApi) : CacheableApi by cacheableApi, TimelineApi by timelineApi { override suspend fun availableTimelines(): Result, MultiMError> { return cacheableApi.cacheOrGet(AVAILABLE_TIMELINES) { timelineApi.availableTimelines() } diff --git a/core/src/main/kotlin/dev/usbharu/multim/cache/ImMemoryLRUCache.kt b/core/src/main/kotlin/dev/usbharu/multim/cache/ImMemoryLRUCache.kt new file mode 100644 index 0000000..ae6fc1b --- /dev/null +++ b/core/src/main/kotlin/dev/usbharu/multim/cache/ImMemoryLRUCache.kt @@ -0,0 +1,34 @@ +package dev.usbharu.multim.cache + +import io.github.reactivecircus.cache4k.Cache +import kotlin.ClassCastException + +class ImMemoryLRUCache(maxSize:Long) : CacheableApi { + private val cache = Cache.Builder().maximumCacheSize(maxSize).build() + private var enable: Boolean = true + override fun purge() { + cache.invalidateAll() + } + + override fun disable() { + enable = false + } + + override fun enable() { + enable = true + } + + override fun cache(method: String, key: String, value: T): T { + cache.put(method+key, value) + return value + } + + override fun get(method: String, key: String): T? { + val any = cache.get(method + key) + return try { + any as T? + }catch (e:ClassCastException){ + null + } + } +} From 0b90e6142c76d0028ecbd2f253288865b8370a8a Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Mon, 27 Feb 2023 12:06:10 +0900 Subject: [PATCH 4/7] =?UTF-8?q?fix:=20InMemory=E3=81=8CImMemory=E3=81=AB?= =?UTF-8?q?=E3=81=AA=E3=81=A3=E3=81=A6=E3=81=9F=E3=81=AE=E3=81=A7=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../multim/cache/{ImMemoryLRUCache.kt => InMemoryLRUCache.kt} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename core/src/main/kotlin/dev/usbharu/multim/cache/{ImMemoryLRUCache.kt => InMemoryLRUCache.kt} (93%) diff --git a/core/src/main/kotlin/dev/usbharu/multim/cache/ImMemoryLRUCache.kt b/core/src/main/kotlin/dev/usbharu/multim/cache/InMemoryLRUCache.kt similarity index 93% rename from core/src/main/kotlin/dev/usbharu/multim/cache/ImMemoryLRUCache.kt rename to core/src/main/kotlin/dev/usbharu/multim/cache/InMemoryLRUCache.kt index ae6fc1b..c0cc5ef 100644 --- a/core/src/main/kotlin/dev/usbharu/multim/cache/ImMemoryLRUCache.kt +++ b/core/src/main/kotlin/dev/usbharu/multim/cache/InMemoryLRUCache.kt @@ -3,7 +3,7 @@ package dev.usbharu.multim.cache import io.github.reactivecircus.cache4k.Cache import kotlin.ClassCastException -class ImMemoryLRUCache(maxSize:Long) : CacheableApi { +class InMemoryLRUCache(maxSize:Long) : CacheableApi { private val cache = Cache.Builder().maximumCacheSize(maxSize).build() private var enable: Boolean = true override fun purge() { From 48ebe47d9a7abf8d6f917b964a45c1f723c58c84 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Mon, 27 Feb 2023 12:09:15 +0900 Subject: [PATCH 5/7] =?UTF-8?q?feat:=20=E7=84=A1=E5=8A=B9=E5=8C=96?= =?UTF-8?q?=E3=81=97=E3=81=9F=E3=81=A8=E3=81=8D=E3=80=81=E7=84=A1=E5=8A=B9?= =?UTF-8?q?=E5=8C=96=E3=81=95=E3=82=8C=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../usbharu/multim/cache/InMemoryLRUCache.kt | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/core/src/main/kotlin/dev/usbharu/multim/cache/InMemoryLRUCache.kt b/core/src/main/kotlin/dev/usbharu/multim/cache/InMemoryLRUCache.kt index c0cc5ef..b88b14f 100644 --- a/core/src/main/kotlin/dev/usbharu/multim/cache/InMemoryLRUCache.kt +++ b/core/src/main/kotlin/dev/usbharu/multim/cache/InMemoryLRUCache.kt @@ -1,10 +1,9 @@ package dev.usbharu.multim.cache import io.github.reactivecircus.cache4k.Cache -import kotlin.ClassCastException -class InMemoryLRUCache(maxSize:Long) : CacheableApi { - private val cache = Cache.Builder().maximumCacheSize(maxSize).build() +class InMemoryLRUCache(maxSize: Long) : CacheableApi { + private val cache = Cache.Builder().maximumCacheSize(maxSize).build() private var enable: Boolean = true override fun purge() { cache.invalidateAll() @@ -19,16 +18,21 @@ class InMemoryLRUCache(maxSize:Long) : CacheableApi { } override fun cache(method: String, key: String, value: T): T { - cache.put(method+key, value) + if (enable) { + cache.put(method + key, value) + } return value } override fun get(method: String, key: String): T? { - val any = cache.get(method + key) - return try { - any as T? - }catch (e:ClassCastException){ - null + if (enable) { + val any = cache.get(method + key) + return try { + any as T? + } catch (e: ClassCastException) { + null + } } + return null } } From 209fdc45063d722cae2180e2ece06b846a68bd97 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Mon, 27 Feb 2023 13:53:58 +0900 Subject: [PATCH 6/7] =?UTF-8?q?feat:=20=E3=82=AD=E3=83=A3=E3=83=83?= =?UTF-8?q?=E3=82=B7=E3=83=A5=E3=82=AD=E3=83=BC=E5=AE=9F=E8=A3=85=E5=BF=98?= =?UTF-8?q?=E3=82=8C=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dev/usbharu/multim/multi/model/MultiAccountAccount.kt | 5 ++++- .../dev/usbharu/multim/multi/model/MultiAccountStatusId.kt | 3 +++ .../dev/usbharu/multim/misskey/v12/common/MisskeyAccount.kt | 5 ++++- .../dev/usbharu/multim/misskey/v12/common/MisskeyStatusId.kt | 3 +++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/core/src/main/kotlin/dev/usbharu/multim/multi/model/MultiAccountAccount.kt b/core/src/main/kotlin/dev/usbharu/multim/multi/model/MultiAccountAccount.kt index c030ded..12b828c 100644 --- a/core/src/main/kotlin/dev/usbharu/multim/multi/model/MultiAccountAccount.kt +++ b/core/src/main/kotlin/dev/usbharu/multim/multi/model/MultiAccountAccount.kt @@ -12,4 +12,7 @@ class MultiAccountAccount( innerData.accountName, innerData.isBot, innerData.avatar -) +) { + override val cacheKey: String + get() = innerData.cacheKey +} diff --git a/core/src/main/kotlin/dev/usbharu/multim/multi/model/MultiAccountStatusId.kt b/core/src/main/kotlin/dev/usbharu/multim/multi/model/MultiAccountStatusId.kt index 1b7d86a..b6d3c3e 100644 --- a/core/src/main/kotlin/dev/usbharu/multim/multi/model/MultiAccountStatusId.kt +++ b/core/src/main/kotlin/dev/usbharu/multim/multi/model/MultiAccountStatusId.kt @@ -16,4 +16,7 @@ class MultiAccountStatusId(override val innerData: StatusId, override val hashCo override fun getUrl(): String { return innerData.getUrl() } + + override val cacheKey: String + get() = innerData.cacheKey } diff --git a/impl/misskey/src/main/kotlin/dev/usbharu/multim/misskey/v12/common/MisskeyAccount.kt b/impl/misskey/src/main/kotlin/dev/usbharu/multim/misskey/v12/common/MisskeyAccount.kt index 53159c4..07eea5d 100644 --- a/impl/misskey/src/main/kotlin/dev/usbharu/multim/misskey/v12/common/MisskeyAccount.kt +++ b/impl/misskey/src/main/kotlin/dev/usbharu/multim/misskey/v12/common/MisskeyAccount.kt @@ -9,4 +9,7 @@ class MisskeyAccount( accountName: String, isBot: Boolean? = false, avatar: Avatar -) : Account(screenName, accountName, isBot, avatar) +) : Account(screenName, accountName, isBot, avatar){ + override val cacheKey: String + get() = accountName +} diff --git a/impl/misskey/src/main/kotlin/dev/usbharu/multim/misskey/v12/common/MisskeyStatusId.kt b/impl/misskey/src/main/kotlin/dev/usbharu/multim/misskey/v12/common/MisskeyStatusId.kt index 66b96fa..b86daa6 100644 --- a/impl/misskey/src/main/kotlin/dev/usbharu/multim/misskey/v12/common/MisskeyStatusId.kt +++ b/impl/misskey/src/main/kotlin/dev/usbharu/multim/misskey/v12/common/MisskeyStatusId.kt @@ -14,4 +14,7 @@ class MisskeyStatusId(val id: String, private val url: String) : StatusId() { override fun getUrl(): String { return url } + + override val cacheKey: String + get() = id } From e3d00c6d1253a09633ba6e621398fc9ae1c527f7 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Mon, 27 Feb 2023 14:07:12 +0900 Subject: [PATCH 7/7] =?UTF-8?q?fix:=20=E3=83=AD=E3=82=AC=E3=83=BC=E3=81=AE?= =?UTF-8?q?=E5=A4=89=E6=9B=B4=E3=81=AB=E8=BF=BD=E5=BE=93=E3=81=A7=E3=81=8D?= =?UTF-8?q?=E3=81=A6=E3=81=84=E3=81=AA=E3=81=84=E3=82=82=E3=81=AE=E3=82=92?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/dev/usbharu/multim/api/MisskeyAccountApiTest.kt | 5 ----- 1 file changed, 5 deletions(-) diff --git a/impl/misskey/src/test/kotlin/dev/usbharu/multim/api/MisskeyAccountApiTest.kt b/impl/misskey/src/test/kotlin/dev/usbharu/multim/api/MisskeyAccountApiTest.kt index 691164c..a938218 100644 --- a/impl/misskey/src/test/kotlin/dev/usbharu/multim/api/MisskeyAccountApiTest.kt +++ b/impl/misskey/src/test/kotlin/dev/usbharu/multim/api/MisskeyAccountApiTest.kt @@ -14,8 +14,6 @@ import dev.usbharu.multim.misskey.v12.common.MisskeyAvatar import dev.usbharu.multim.misskey.v12.common.api.MisskeyAccountApi import dev.usbharu.multim.misskey.v12.common.api.MisskeyApiClient import dev.usbharu.multim.model.SingleTokenAuth -import io.github.aakira.napier.DebugAntilog -import io.github.aakira.napier.Napier import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.runTest import org.junit.jupiter.api.Assertions.* @@ -98,7 +96,6 @@ class MisskeyAccountApiTest : AccountApiTest() { if (it is MultiMHttpError) { }else { - Napier.base(DebugAntilog()) Logger.error("Account Api Test","想定されたエラーではない",it) fail("想定されたエラーではない",) } @@ -146,7 +143,6 @@ class MisskeyAccountApiTest : AccountApiTest() { if (it is MultiMHttpError) { }else { - Napier.base(DebugAntilog()) Logger.error("Account Api Test","想定されたエラーではない",it) fail("想定されたエラーではない",) } @@ -221,7 +217,6 @@ class MisskeyAccountApiTest : AccountApiTest() { @Test override fun relationships_illegalAccount_returnErr() = runTest { - Napier.base(DebugAntilog()) val myself = MisskeyAccount( "9bg1zu54y7", "test",