feat: キャッシュを導入

This commit is contained in:
usbharu 2023-10-11 15:34:12 +09:00
parent 30cd1274ac
commit f33e04faa5
4 changed files with 33 additions and 4 deletions

View File

@ -6,6 +6,7 @@ import dev.usbharu.hideout.query.UserQueryService
import dev.usbharu.hideout.service.core.Transaction
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.cache.*
import io.ktor.client.plugins.logging.*
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
@ -22,6 +23,9 @@ class HttpClientConfig {
logger = Logger.DEFAULT
level = LogLevel.INFO
}
install(HttpCache) {
}
expectSuccess = true
}

View File

@ -28,7 +28,7 @@ class APLikeServiceImpl(
like.`object` ?: throw IllegalActivityPubObjectException("object is null")
transaction.transaction {
val person = apUserService.fetchPersonWithEntity(actor)
apNoteService.fetchNote(like.`object` ?: return@transaction)
apNoteService.fetchNoteAsync(like.`object` ?: return@transaction).await()
val post = postQueryService.findByUrl(like.`object` ?: return@transaction)

View File

@ -24,8 +24,14 @@ import dev.usbharu.hideout.service.post.PostService
import io.ktor.client.*
import io.ktor.client.statement.*
import kjob.core.job.JobProps
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.cache.annotation.Cacheable
import org.springframework.stereotype.Service
import java.time.Instant
@ -34,6 +40,15 @@ interface APNoteService {
suspend fun createNote(post: Post)
suspend fun createNoteJob(props: JobProps<DeliverPostJob>)
@Cacheable("fetchNote")
fun fetchNoteAsync(url: String, targetActor: String? = null): Deferred<Note> {
return CoroutineScope(Dispatchers.IO).async {
newSuspendedTransaction {
fetchNote(url, targetActor)
}
}
}
suspend fun fetchNote(url: String, targetActor: String? = null): Note
suspend fun fetchNote(note: Note, targetActor: String? = null): Note
}

View File

@ -4,6 +4,7 @@ import dev.usbharu.hideout.domain.model.hideout.entity.Reaction
import dev.usbharu.hideout.query.ReactionQueryService
import dev.usbharu.hideout.repository.ReactionRepository
import dev.usbharu.hideout.service.ap.APReactionService
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
@Service
@ -14,9 +15,14 @@ class ReactionServiceImpl(
) : ReactionService {
override suspend fun receiveReaction(name: String, domain: String, userId: Long, postId: Long) {
if (reactionQueryService.reactionAlreadyExist(postId, userId, 0).not()) {
reactionRepository.save(
Reaction(reactionRepository.generateId(), 0, postId, userId)
)
try {
reactionRepository.save(
Reaction(reactionRepository.generateId(), 0, postId, userId)
)
} catch (_: Exception) {
LOGGER.warn("FAILED Failure to persist reaction information.")
}
}
}
@ -34,4 +40,8 @@ class ReactionServiceImpl(
override suspend fun removeReaction(userId: Long, postId: Long) {
reactionQueryService.deleteByPostIdAndUserId(postId, userId)
}
companion object {
val LOGGER = LoggerFactory.getLogger(ReactionServiceImpl::class.java)
}
}