2022-03-22 12:52:23 +00:00
|
|
|
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.StorageFile
|
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)
|
|
|
|
suspend fun get(k: K): StorageFile?
|
|
|
|
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> {
|
|
|
|
private val map = mutableMapOf<K, StorageFile>()
|
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) {
|
|
|
|
map[k] = StorageFile(
|
2022-03-22 12:52:23 +00:00
|
|
|
filename,
|
2022-04-11 15:28:53 +00:00
|
|
|
inputAllocator().readBytes()
|
2022-03-22 12:52:23 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-04-11 17:32:06 +00:00
|
|
|
override suspend fun get(k: K): StorageFile? {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|