TelegramBotApiLibraries/cache/content/common/src/commonMain/kotlin/dev/inmo/tgbotapi/libraries/cache/media/common/MessagesFilesCache.kt

42 lines
1.2 KiB
Kotlin
Raw Normal View History

2022-03-22 12:52:23 +00:00
package dev.inmo.tgbotapi.libraries.cache.media.common
2022-05-17 14:05:53 +00:00
import dev.inmo.tgbotapi.requests.abstracts.MultipartFile
2022-04-11 15:28:53 +00:00
import io.ktor.utils.io.core.*
2022-03-22 12:52:23 +00:00
2022-04-11 17:32:06 +00:00
interface MessagesFilesCache<K> {
suspend fun set(k: K, filename: String, inputAllocator: suspend () -> Input)
2022-05-17 14:05:53 +00:00
suspend fun get(k: K): MultipartFile?
2022-04-11 17:32:06 +00:00
suspend fun remove(k: K)
suspend fun contains(k: K): Boolean
2022-03-22 12:52:23 +00:00
}
/**
* It is not recommended to use in production realization of [MessagesFilesCache] which has been created for fast
* start of application creation with usage of [MessageContentCache] with aim to replace this realization by some
* disks-oriented one
*/
2022-04-11 17:32:06 +00:00
class InMemoryMessagesFilesCache<K> : MessagesFilesCache<K> {
2022-05-17 14:05:53 +00:00
private val map = mutableMapOf<K, MultipartFile>()
2022-03-22 12:52:23 +00:00
2022-04-11 17:32:06 +00:00
override suspend fun set(k: K, filename: String, inputAllocator: suspend () -> Input) {
2022-05-17 14:05:53 +00:00
val input = inputAllocator()
map[k] = MultipartFile(
filename
) {
input
}
2022-03-22 12:52:23 +00:00
}
2022-05-17 14:05:53 +00:00
override suspend fun get(k: K): MultipartFile? {
2022-04-11 17:32:06 +00:00
return map[k]
2022-03-22 12:52:23 +00:00
}
2022-04-11 17:32:06 +00:00
override suspend fun remove(k: K) {
map.remove(k)
2022-03-22 12:52:23 +00:00
}
2022-04-11 17:32:06 +00:00
override suspend fun contains(k: K): Boolean {
return map.contains(k)
2022-03-22 12:52:23 +00:00
}
}