Skip to content

Commit

Permalink
🎨 feat: 缓存优化
Browse files Browse the repository at this point in the history
  • Loading branch information
cokie committed Jan 24, 2024
1 parent af0859c commit 8ad92a1
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ interface IAuthRepository {

fun findAuthorityByExample(authorityDO: AuthorityDO): Uni<List<AuthorityDO>>

fun findAuthorityCacheByExample(authorityDO: AuthorityDO): Uni<List<AuthorityDO>>

fun saveAuthority(authorityDO: AuthorityDO): Uni<AuthorityDO>

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ interface AuthService {

fun getAuthorityByCode(code: String): Uni<AuthorityDO>

fun getAuthorityCacheByCode(code: String): Uni<AuthorityDO>

fun addAuthority(authorityDO: AuthorityDO): Uni<AuthorityDO>

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ class AuthServiceImpl(private val authRepository: IAuthRepository) : AuthService
}
}

@WithSession
override fun getAuthorityCacheByCode(code: String): Uni<AuthorityDO> {
return authRepository.findAuthorityCacheByExample(AuthorityDO().also { it.value = code })
.map {
if (it.isNotEmpty()) {
it.first()
} else {
null
}
}
}

@WithTransaction
override fun addAuthority(authorityDO: AuthorityDO): Uni<AuthorityDO> {
return authRepository.findAuthorityByExample(AuthorityDO().also { it.value = authorityDO.value })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.micro.server.auth.domain.repository.IAuthRepository
import io.micro.server.auth.infra.converter.AuthConverter
import io.micro.server.auth.infra.dao.impl.AuthorityDAO
import io.micro.server.auth.infra.dao.impl.UserDAO
import io.quarkus.cache.CacheResult
import io.quarkus.panache.common.Page
import io.smallrye.mutiny.Uni
import jakarta.enterprise.context.ApplicationScoped
Expand Down Expand Up @@ -94,6 +95,11 @@ class AuthRepository(
.map { it.converter(authConverter::authorityEntity2authorityDO) }
}

@CacheResult(cacheName = "authority")
override fun findAuthorityCacheByExample(authorityDO: AuthorityDO): Uni<List<AuthorityDO>> {
return findAuthorityByExample(authorityDO)
}

override fun saveAuthority(authorityDO: AuthorityDO): Uni<AuthorityDO> {
val entity = authConverter.authorityDO2authorityEntity(authorityDO).also { it.id = null }
return authorityDAO.persist(entity).map(authConverter::authorityEntity2authorityDO)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class FeatureFunctionDO {

var memberId: Long? = null

var isMenu: Boolean = false

var isUndefined: Boolean = false

companion object {
const val BRACES = "{}"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class RobotDO : BaseDomainEntity() {
it.args += args
}
} else {
null
FeatureFunctionDO().also { it.isUndefined = true }
}
} else {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ class RobotManagerServiceImpl(
val featureFunction = getFeatureFunction(event, latestRobot)
if (featureFunction != null) {
val authority = sessionFactory.withSession {
authService.getAuthorityByCode(featureFunction.code!!)
authService.getAuthorityCacheByCode(featureFunction.code!!)
}.runSubscriptionOn { vertxContext.runOnContext(it) }.awaitSuspending()
if (authority != null && authority.enabled == true) {
val switch = sessionFactory.withSession {
Expand Down

0 comments on commit 8ad92a1

Please sign in to comment.