[github] Account information cache

This commit is contained in:
Ivan Semenov
2018-05-18 18:10:26 +03:00
parent 1459b7e0f6
commit 8ee5346dfe
2 changed files with 36 additions and 12 deletions
@@ -133,9 +133,10 @@ class GithubShareAction : DumbAwareAction("Share Project on GitHub", "Easily sha
@Throws(IOException::class)
private fun loadAccountInfo(indicator: ProgressIndicator, account: GithubAccount): Pair<Boolean, Set<String>> {
val provider = service<GithubAccountInformationProvider>()
return service<GithubApiTaskExecutor>().execute(indicator, account, GithubTask { connection ->
// ability to create private repos and list of repos
val user = GithubApiUtil.getCurrentUser(connection)
val user = provider.getAccountInformation(account, connection)
val canCreatePrivateRepo = user.canCreatePrivateRepo()
val names = GithubApiUtil.getUserRepos(connection).mapSmartSet { it.name }
canCreatePrivateRepo to names
@@ -1,37 +1,60 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.github.authentication.accounts
import com.google.common.cache.CacheBuilder
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.progress.ProgressIndicator
import org.jetbrains.plugins.github.api.GithubApiTaskExecutor
import org.jetbrains.plugins.github.api.GithubApiUtil
import org.jetbrains.plugins.github.api.GithubServerPath
import org.jetbrains.plugins.github.api.GithubTask
import org.jetbrains.plugins.github.api.data.GithubUserDetailed
import org.jetbrains.plugins.github.api.*
import org.jetbrains.plugins.github.api.data.GithubAuthenticatedUser
import java.awt.Image
import java.awt.image.BufferedImage
import java.io.IOException
import java.net.URL
import java.util.concurrent.TimeUnit
import javax.imageio.ImageIO
//TODO: caching
//TODO: load image with GithubApiTaskExecutor
class GithubAccountInformationProvider(private val apiTaskExecutor: GithubApiTaskExecutor) {
private val cacheBuilder = CacheBuilder.newBuilder().expireAfterAccess(30, TimeUnit.MINUTES)
private val informationCache = cacheBuilder.build<GithubAccount, GithubAuthenticatedUser>().asMap()
private val imageCache = cacheBuilder.build<GithubAccount, BufferedImage>().asMap()
init {
ApplicationManager.getApplication().messageBus
.connect()
.subscribe(GithubAccountManager.ACCOUNT_REMOVED_TOPIC, object : AccountRemovedListener {
override fun accountRemoved(removedAccount: GithubAccount) {
informationCache.remove(removedAccount)
imageCache.remove(removedAccount)
}
})
}
@Throws(IOException::class)
fun getAccountInformationWithPicture(indicator: ProgressIndicator,
server: GithubServerPath,
token: String): Pair<GithubUserDetailed, Image> {
token: String): Pair<GithubAuthenticatedUser, Image> {
val details = GithubApiTaskExecutor.execute(indicator, server, token, GithubTask { c -> GithubApiUtil.getCurrentUser(c) })
return details to ImageIO.read(URL(details.avatarUrl))
}
@Throws(IOException::class)
fun getAccountInformationWithPicture(indicator: ProgressIndicator, account: GithubAccount): Pair<GithubUserDetailed, Image> {
fun getAccountInformationWithPicture(indicator: ProgressIndicator, account: GithubAccount): Pair<GithubAuthenticatedUser, Image> {
val details = getAccountInformation(indicator, account)
return details to ImageIO.read(URL(details.avatarUrl))
return details to imageCache.getOrPut(account) { ImageIO.read(URL(details.avatarUrl)) }
}
@Throws(IOException::class)
fun getAccountInformation(indicator: ProgressIndicator, account: GithubAccount): GithubUserDetailed {
return apiTaskExecutor.execute(indicator, account, GithubTask { c -> GithubApiUtil.getCurrentUser(c) })
fun getAccountInformation(indicator: ProgressIndicator, account: GithubAccount): GithubAuthenticatedUser {
return informationCache.getOrPut(account) {
apiTaskExecutor.execute(indicator, account, GithubTask { c -> GithubApiUtil.getCurrentUser(c) })
}
}
@Throws(IOException::class)
fun getAccountInformation(account: GithubAccount, c: GithubConnection): GithubAuthenticatedUser {
return informationCache.getOrPut(account) { GithubApiUtil.getCurrentUser(c) }
}
@Throws(IOException::class)