mirror of
https://github.com/InsanusMokrassar/TelegramBotApiLibraries.git
synced 2024-12-22 16:47:16 +00:00
add opportunity to use custom keys
This commit is contained in:
parent
be1b13debb
commit
0f05779e0d
@ -7,20 +7,18 @@ import dev.inmo.tgbotapi.requests.get.GetFile
|
|||||||
import dev.inmo.tgbotapi.requests.send.media.*
|
import dev.inmo.tgbotapi.requests.send.media.*
|
||||||
import dev.inmo.tgbotapi.types.ChatId
|
import dev.inmo.tgbotapi.types.ChatId
|
||||||
import dev.inmo.tgbotapi.types.InputMedia.*
|
import dev.inmo.tgbotapi.types.InputMedia.*
|
||||||
import dev.inmo.tgbotapi.types.MessageIdentifier
|
|
||||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaContent
|
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaContent
|
||||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
|
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
|
||||||
import dev.inmo.tgbotapi.utils.asInput
|
import dev.inmo.tgbotapi.utils.asInput
|
||||||
import io.ktor.utils.io.cancel
|
|
||||||
import io.ktor.utils.io.core.Input
|
import io.ktor.utils.io.core.Input
|
||||||
|
|
||||||
class DefaultMessageContentCache(
|
class DefaultMessageContentCache<K>(
|
||||||
private val bot: TelegramBot,
|
private val bot: TelegramBot,
|
||||||
private val filesRefreshingChatId: ChatId,
|
private val filesRefreshingChatId: ChatId,
|
||||||
private val simpleMessageContentCache: MessagesSimpleCache = InMemoryMessagesSimpleCache(),
|
private val simpleMessageContentCache: MessagesSimpleCache<K>,
|
||||||
private val messagesFilesCache: MessagesFilesCache = InMemoryMessagesFilesCache()
|
private val messagesFilesCache: MessagesFilesCache<K> = InMemoryMessagesFilesCache()
|
||||||
) : MessageContentCache {
|
) : MessageContentCache<K> {
|
||||||
override suspend fun save(chatId: ChatId, messageId: MessageIdentifier, content: MessageContent): Boolean {
|
override suspend fun save(content: MessageContent): K {
|
||||||
return when (content) {
|
return when (content) {
|
||||||
is MediaContent -> {
|
is MediaContent -> {
|
||||||
val extendedInfo = bot.execute(
|
val extendedInfo = bot.execute(
|
||||||
@ -32,42 +30,37 @@ class DefaultMessageContentCache(
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
save(chatId, messageId, content, extendedInfo.fileName) {
|
save(content, extendedInfo.fileName) {
|
||||||
allocator.invoke().asInput()
|
allocator.invoke().asInput()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else -> simpleMessageContentCache.runCatching {
|
else -> simpleMessageContentCache.add(content)
|
||||||
set(chatId, messageId, content)
|
|
||||||
}.isSuccess
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun save(
|
override suspend fun save(
|
||||||
chatId: ChatId,
|
|
||||||
messageId: MessageIdentifier,
|
|
||||||
content: MediaContent,
|
content: MediaContent,
|
||||||
filename: String,
|
filename: String,
|
||||||
inputAllocator: suspend () -> Input
|
inputAllocator: suspend () -> Input
|
||||||
): Boolean {
|
): K {
|
||||||
|
val key = simpleMessageContentCache.add(content)
|
||||||
runCatching {
|
runCatching {
|
||||||
messagesFilesCache.set(chatId, messageId, filename, inputAllocator)
|
messagesFilesCache.set(key, filename, inputAllocator)
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
return false
|
simpleMessageContentCache.remove(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
return simpleMessageContentCache.runCatching {
|
return key
|
||||||
set(chatId, messageId, content)
|
|
||||||
}.isSuccess
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun get(chatId: ChatId, messageId: MessageIdentifier): MessageContent? {
|
override suspend fun get(k: K): MessageContent? {
|
||||||
val savedSimpleContent = simpleMessageContentCache.get(chatId, messageId) ?: return null
|
val savedSimpleContent = simpleMessageContentCache.get(k) ?: return null
|
||||||
|
|
||||||
if (savedSimpleContent is MediaContent) {
|
if (savedSimpleContent is MediaContent) {
|
||||||
runCatching {
|
runCatching {
|
||||||
bot.execute(GetFile(savedSimpleContent.media.fileId))
|
bot.execute(GetFile(savedSimpleContent.media.fileId))
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
val savedFileContentAllocator = messagesFilesCache.get(chatId, messageId) ?: error("Unexpected absence of $chatId:$messageId file for content ($simpleMessageContentCache)")
|
val savedFileContentAllocator = messagesFilesCache.get(k) ?: error("Unexpected absence of $k file for content ($simpleMessageContentCache)")
|
||||||
val newContent = bot.execute(
|
val newContent = bot.execute(
|
||||||
when (savedSimpleContent.asInputMedia()) {
|
when (savedSimpleContent.asInputMedia()) {
|
||||||
is InputMediaAnimation -> SendAnimation(
|
is InputMediaAnimation -> SendAnimation(
|
||||||
@ -108,19 +101,28 @@ class DefaultMessageContentCache(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
simpleMessageContentCache.set(chatId, messageId, newContent.content)
|
simpleMessageContentCache.update(k, newContent.content)
|
||||||
return newContent.content
|
return newContent.content
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return savedSimpleContent
|
return savedSimpleContent
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun contains(chatId: ChatId, messageId: MessageIdentifier): Boolean {
|
override suspend fun contains(k: K): Boolean {
|
||||||
return simpleMessageContentCache.contains(chatId, messageId)
|
return simpleMessageContentCache.contains(k)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun remove(chatId: ChatId, messageId: MessageIdentifier) {
|
override suspend fun remove(k: K) {
|
||||||
simpleMessageContentCache.remove(chatId, messageId)
|
simpleMessageContentCache.remove(k)
|
||||||
messagesFilesCache.remove(chatId, messageId)
|
messagesFilesCache.remove(k)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
operator fun invoke(
|
||||||
|
bot: TelegramBot,
|
||||||
|
filesRefreshingChatId: ChatId,
|
||||||
|
simpleMessageContentCache: MessagesSimpleCache<String> = InMemoryMessagesSimpleCache(),
|
||||||
|
messagesFilesCache: MessagesFilesCache<String> = InMemoryMessagesFilesCache()
|
||||||
|
) = DefaultMessageContentCache(bot, filesRefreshingChatId, simpleMessageContentCache, messagesFilesCache)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,17 @@
|
|||||||
package dev.inmo.tgbotapi.libraries.cache.media.common
|
package dev.inmo.tgbotapi.libraries.cache.media.common
|
||||||
|
|
||||||
import dev.inmo.tgbotapi.types.ChatId
|
|
||||||
import dev.inmo.tgbotapi.types.MessageIdentifier
|
|
||||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaContent
|
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaContent
|
||||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
|
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
|
||||||
import io.ktor.utils.io.core.Input
|
import io.ktor.utils.io.core.Input
|
||||||
|
|
||||||
interface MessageContentCache {
|
interface MessageContentCache<K> {
|
||||||
suspend fun save(chatId: ChatId, messageId: MessageIdentifier, content: MessageContent): Boolean
|
suspend fun save(content: MessageContent): K
|
||||||
suspend fun save(
|
suspend fun save(
|
||||||
chatId: ChatId,
|
|
||||||
messageId: MessageIdentifier,
|
|
||||||
content: MediaContent,
|
content: MediaContent,
|
||||||
filename: String,
|
filename: String,
|
||||||
inputAllocator: suspend () -> Input
|
inputAllocator: suspend () -> Input
|
||||||
): Boolean
|
): K
|
||||||
suspend fun get(chatId: ChatId, messageId: MessageIdentifier): MessageContent?
|
suspend fun get(k: K): MessageContent?
|
||||||
suspend fun contains(chatId: ChatId, messageId: MessageIdentifier): Boolean
|
suspend fun contains(k: K): Boolean
|
||||||
suspend fun remove(chatId: ChatId, messageId: MessageIdentifier)
|
suspend fun remove(k: K)
|
||||||
}
|
}
|
||||||
|
@ -5,16 +5,11 @@ import dev.inmo.tgbotapi.types.MessageIdentifier
|
|||||||
import dev.inmo.tgbotapi.utils.StorageFile
|
import dev.inmo.tgbotapi.utils.StorageFile
|
||||||
import io.ktor.utils.io.core.*
|
import io.ktor.utils.io.core.*
|
||||||
|
|
||||||
interface MessagesFilesCache {
|
interface MessagesFilesCache<K> {
|
||||||
suspend fun set(
|
suspend fun set(k: K, filename: String, inputAllocator: suspend () -> Input)
|
||||||
chatId: ChatId,
|
suspend fun get(k: K): StorageFile?
|
||||||
messageIdentifier: MessageIdentifier,
|
suspend fun remove(k: K)
|
||||||
filename: String,
|
suspend fun contains(k: K): Boolean
|
||||||
inputAllocator: suspend () -> Input
|
|
||||||
)
|
|
||||||
suspend fun get(chatId: ChatId, messageIdentifier: MessageIdentifier): StorageFile?
|
|
||||||
suspend fun remove(chatId: ChatId, messageIdentifier: MessageIdentifier)
|
|
||||||
suspend fun contains(chatId: ChatId, messageIdentifier: MessageIdentifier): Boolean
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -22,31 +17,25 @@ interface MessagesFilesCache {
|
|||||||
* start of application creation with usage of [MessageContentCache] with aim to replace this realization by some
|
* start of application creation with usage of [MessageContentCache] with aim to replace this realization by some
|
||||||
* disks-oriented one
|
* disks-oriented one
|
||||||
*/
|
*/
|
||||||
class InMemoryMessagesFilesCache : MessagesFilesCache {
|
class InMemoryMessagesFilesCache<K> : MessagesFilesCache<K> {
|
||||||
private val map = mutableMapOf<Pair<ChatId, MessageIdentifier>, StorageFile>()
|
private val map = mutableMapOf<K, StorageFile>()
|
||||||
|
|
||||||
override suspend fun set(
|
override suspend fun set(k: K, filename: String, inputAllocator: suspend () -> Input) {
|
||||||
chatId: ChatId,
|
map[k] = StorageFile(
|
||||||
messageIdentifier: MessageIdentifier,
|
|
||||||
filename: String,
|
|
||||||
inputAllocator: suspend () -> Input
|
|
||||||
) {
|
|
||||||
map[chatId to messageIdentifier] = StorageFile(
|
|
||||||
filename,
|
filename,
|
||||||
inputAllocator().readBytes()
|
inputAllocator().readBytes()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun get(chatId: ChatId, messageIdentifier: MessageIdentifier): StorageFile? {
|
override suspend fun get(k: K): StorageFile? {
|
||||||
return map[chatId to messageIdentifier]
|
return map[k]
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun remove(chatId: ChatId, messageIdentifier: MessageIdentifier) {
|
override suspend fun remove(k: K) {
|
||||||
map.remove(chatId to messageIdentifier)
|
map.remove(k)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun contains(chatId: ChatId, messageIdentifier: MessageIdentifier): Boolean {
|
override suspend fun contains(k: K): Boolean {
|
||||||
return map.contains(chatId to messageIdentifier)
|
return map.contains(k)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,14 @@
|
|||||||
package dev.inmo.tgbotapi.libraries.cache.media.common
|
package dev.inmo.tgbotapi.libraries.cache.media.common
|
||||||
|
|
||||||
import dev.inmo.tgbotapi.types.ChatId
|
import com.benasher44.uuid.uuid4
|
||||||
import dev.inmo.tgbotapi.types.MessageIdentifier
|
|
||||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
|
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
|
||||||
import dev.inmo.tgbotapi.utils.StorageFile
|
|
||||||
import io.ktor.utils.io.core.*
|
|
||||||
|
|
||||||
interface MessagesSimpleCache {
|
interface MessagesSimpleCache<K> {
|
||||||
suspend fun set(
|
suspend fun add(content: MessageContent): K
|
||||||
chatId: ChatId,
|
suspend fun update(k: K, content: MessageContent): Boolean
|
||||||
messageIdentifier: MessageIdentifier,
|
suspend fun get(k: K): MessageContent?
|
||||||
content: MessageContent
|
suspend fun remove(k: K)
|
||||||
)
|
suspend fun contains(k: K): Boolean
|
||||||
suspend fun get(chatId: ChatId, messageIdentifier: MessageIdentifier): MessageContent?
|
|
||||||
suspend fun remove(chatId: ChatId, messageIdentifier: MessageIdentifier)
|
|
||||||
suspend fun contains(chatId: ChatId, messageIdentifier: MessageIdentifier): Boolean
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -22,27 +16,48 @@ interface MessagesSimpleCache {
|
|||||||
* start of application creation with usage of [MessageContentCache] with aim to replace this realization by some
|
* start of application creation with usage of [MessageContentCache] with aim to replace this realization by some
|
||||||
* disks-oriented one
|
* disks-oriented one
|
||||||
*/
|
*/
|
||||||
class InMemoryMessagesSimpleCache : MessagesSimpleCache {
|
class InMemoryMessagesSimpleCache<K>(
|
||||||
private val map = mutableMapOf<Pair<ChatId, MessageIdentifier>, MessageContent>()
|
private val keyGenerator: () -> K
|
||||||
|
) : MessagesSimpleCache<K> {
|
||||||
|
private val map = mutableMapOf<K, MessageContent>()
|
||||||
|
|
||||||
override suspend fun set(
|
override suspend fun add(
|
||||||
chatId: ChatId,
|
|
||||||
messageIdentifier: MessageIdentifier,
|
|
||||||
content: MessageContent
|
content: MessageContent
|
||||||
) {
|
): K {
|
||||||
map[chatId to messageIdentifier] = content
|
val key = keyGenerator()
|
||||||
|
map[key] = content
|
||||||
|
return key
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun get(chatId: ChatId, messageIdentifier: MessageIdentifier): MessageContent? {
|
override suspend fun update(
|
||||||
return map[chatId to messageIdentifier]
|
k: K,
|
||||||
|
content: MessageContent
|
||||||
|
): Boolean {
|
||||||
|
return map.runCatching {
|
||||||
|
if (contains(k)) {
|
||||||
|
put(k, content)
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}.getOrDefault(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun remove(chatId: ChatId, messageIdentifier: MessageIdentifier) {
|
override suspend fun get(k: K): MessageContent? {
|
||||||
map.remove(chatId to messageIdentifier)
|
return map[k]
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun contains(chatId: ChatId, messageIdentifier: MessageIdentifier): Boolean {
|
override suspend fun remove(k: K) {
|
||||||
return map.contains(chatId to messageIdentifier)
|
map.remove(k)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override suspend fun contains(k: K): Boolean {
|
||||||
|
return map.contains(k)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
operator fun invoke() = InMemoryMessagesSimpleCache {
|
||||||
|
uuid4().toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package dev.inmo.tgbotapi.libraries.cache.media.common
|
package dev.inmo.tgbotapi.libraries.cache.media.common
|
||||||
|
|
||||||
import dev.inmo.tgbotapi.types.ChatId
|
|
||||||
import dev.inmo.tgbotapi.types.MessageIdentifier
|
|
||||||
import dev.inmo.tgbotapi.utils.*
|
import dev.inmo.tgbotapi.utils.*
|
||||||
import io.ktor.utils.io.core.Input
|
import io.ktor.utils.io.core.Input
|
||||||
import io.ktor.utils.io.core.copyTo
|
import io.ktor.utils.io.core.copyTo
|
||||||
@ -9,12 +7,13 @@ import io.ktor.utils.io.streams.asInput
|
|||||||
import io.ktor.utils.io.streams.asOutput
|
import io.ktor.utils.io.streams.asOutput
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
class InFilesMessagesFilesCache(
|
class InFilesMessagesFilesCache<K>(
|
||||||
private val folderFile: File
|
private val folderFile: File,
|
||||||
) : MessagesFilesCache {
|
private val filePrefixBuilder: (K) -> String
|
||||||
private val Pair<ChatId, MessageIdentifier>.storageFile: StorageFile?
|
) : MessagesFilesCache<K> {
|
||||||
|
private val K.storageFile: StorageFile?
|
||||||
get() {
|
get() {
|
||||||
val prefix = filePrefix(first, second)
|
val prefix = filePrefix(this)
|
||||||
val filename = folderFile.list() ?.firstOrNull { it.startsWith(prefix) } ?: return null
|
val filename = folderFile.list() ?.firstOrNull { it.startsWith(prefix) } ?: return null
|
||||||
val file = File(folderFile, filename)
|
val file = File(folderFile, filename)
|
||||||
val storageFileFilename = file.name.removePrefix("$prefix ")
|
val storageFileFilename = file.name.removePrefix("$prefix ")
|
||||||
@ -31,21 +30,14 @@ class InFilesMessagesFilesCache(
|
|||||||
folderFile.mkdirs()
|
folderFile.mkdirs()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun filePrefix(chatId: ChatId, messageIdentifier: MessageIdentifier): String {
|
private fun filePrefix(k: K): String = filePrefixBuilder(k)
|
||||||
return "${chatId.chatId} $messageIdentifier"
|
|
||||||
|
private fun fileName(k: K, filename: String): String {
|
||||||
|
return "${filePrefix(k)} $filename"
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun fileName(chatId: ChatId, messageIdentifier: MessageIdentifier, filename: String): String {
|
override suspend fun set(k: K, filename: String, inputAllocator: suspend () -> Input) {
|
||||||
return "${chatId.chatId} $messageIdentifier $filename"
|
val fullFileName = fileName(k, filename)
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun set(
|
|
||||||
chatId: ChatId,
|
|
||||||
messageIdentifier: MessageIdentifier,
|
|
||||||
filename: String,
|
|
||||||
inputAllocator: suspend () -> Input
|
|
||||||
) {
|
|
||||||
val fullFileName = fileName(chatId, messageIdentifier, filename)
|
|
||||||
val file = File(folderFile, fullFileName).apply {
|
val file = File(folderFile, fullFileName).apply {
|
||||||
delete()
|
delete()
|
||||||
}
|
}
|
||||||
@ -56,12 +48,12 @@ class InFilesMessagesFilesCache(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun get(chatId: ChatId, messageIdentifier: MessageIdentifier): StorageFile? {
|
override suspend fun get(k: K): StorageFile? {
|
||||||
return (chatId to messageIdentifier).storageFile
|
return k.storageFile
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun remove(chatId: ChatId, messageIdentifier: MessageIdentifier) {
|
override suspend fun remove(k: K) {
|
||||||
val prefix = filePrefix(chatId, messageIdentifier)
|
val prefix = filePrefix(k)
|
||||||
folderFile.listFiles() ?.forEach {
|
folderFile.listFiles() ?.forEach {
|
||||||
if (it.name.startsWith(prefix)) {
|
if (it.name.startsWith(prefix)) {
|
||||||
it.delete()
|
it.delete()
|
||||||
@ -69,8 +61,14 @@ class InFilesMessagesFilesCache(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun contains(chatId: ChatId, messageIdentifier: MessageIdentifier): Boolean {
|
override suspend fun contains(k: K): Boolean {
|
||||||
val prefix = filePrefix(chatId, messageIdentifier)
|
val prefix = filePrefix(k)
|
||||||
return folderFile.list() ?.any { it.startsWith(prefix) } == true
|
return folderFile.list() ?.any { it.startsWith(prefix) } == true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
operator fun invoke(folderFile: File) = InFilesMessagesFilesCache<String>(
|
||||||
|
folderFile
|
||||||
|
) { it }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package dev.inmo.tgbotapi.libraries.cache.media.micro_utils
|
package dev.inmo.tgbotapi.libraries.cache.media.micro_utils
|
||||||
|
|
||||||
|
import com.benasher44.uuid.uuid4
|
||||||
import dev.inmo.micro_utils.repos.*
|
import dev.inmo.micro_utils.repos.*
|
||||||
import dev.inmo.micro_utils.repos.mappers.withMapper
|
import dev.inmo.micro_utils.repos.mappers.withMapper
|
||||||
import dev.inmo.tgbotapi.libraries.cache.media.common.MessagesSimpleCache
|
import dev.inmo.tgbotapi.libraries.cache.media.common.MessagesSimpleCache
|
||||||
@ -14,23 +15,44 @@ import kotlinx.serialization.modules.SerializersModule
|
|||||||
import kotlin.js.JsName
|
import kotlin.js.JsName
|
||||||
import kotlin.jvm.JvmName
|
import kotlin.jvm.JvmName
|
||||||
|
|
||||||
class SimpleKeyValueMessageContentCache(
|
class SimpleKeyValueMessageContentCache<K>(
|
||||||
private val keyValueRepo: KeyValueRepo<Pair<ChatId, MessageIdentifier>, MessageContent>
|
private val keyValueRepo: KeyValueRepo<K, MessageContent>,
|
||||||
) : MessagesSimpleCache {
|
private val keyGenerator: () -> K
|
||||||
override suspend fun set(chatId: ChatId, messageIdentifier: MessageIdentifier, content: MessageContent) {
|
) : MessagesSimpleCache<K> {
|
||||||
keyValueRepo.set(chatId to messageIdentifier, content)
|
override suspend fun add(content: MessageContent): K {
|
||||||
|
val key = keyGenerator()
|
||||||
|
keyValueRepo.set(key, content)
|
||||||
|
|
||||||
|
return key
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun get(chatId: ChatId, messageIdentifier: MessageIdentifier): MessageContent? {
|
override suspend fun update(k: K, content: MessageContent): Boolean {
|
||||||
return keyValueRepo.get(chatId to messageIdentifier)
|
return keyValueRepo.runCatching {
|
||||||
|
if (contains(k)) {
|
||||||
|
keyValueRepo.set(k, content)
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}.getOrDefault(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun contains(chatId: ChatId, messageIdentifier: MessageIdentifier): Boolean {
|
override suspend fun get(k: K): MessageContent? {
|
||||||
return keyValueRepo.contains(chatId to messageIdentifier)
|
return keyValueRepo.get(k)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun remove(chatId: ChatId, messageIdentifier: MessageIdentifier) {
|
override suspend fun contains(k: K): Boolean {
|
||||||
keyValueRepo.unset(chatId to messageIdentifier)
|
return keyValueRepo.contains(k)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun remove(k: K) {
|
||||||
|
keyValueRepo.unset(k)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
operator fun invoke(
|
||||||
|
keyValueRepo: KeyValueRepo<String, MessageContent>
|
||||||
|
) = SimpleKeyValueMessageContentCache(keyValueRepo) { uuid4().toString() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user