diff --git a/plugins/github/src/org/jetbrains/plugins/github/GithubShareAction.kt b/plugins/github/src/org/jetbrains/plugins/github/GithubShareAction.kt index c779564b3027..20d48b0f11a1 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/GithubShareAction.kt +++ b/plugins/github/src/org/jetbrains/plugins/github/GithubShareAction.kt @@ -133,9 +133,10 @@ class GithubShareAction : DumbAwareAction("Share Project on GitHub", "Easily sha @Throws(IOException::class) private fun loadAccountInfo(indicator: ProgressIndicator, account: GithubAccount): Pair> { + val provider = service() return service().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 diff --git a/plugins/github/src/org/jetbrains/plugins/github/authentication/accounts/GithubAccountInformationProvider.kt b/plugins/github/src/org/jetbrains/plugins/github/authentication/accounts/GithubAccountInformationProvider.kt index 4865236eccc7..7033ae110cfd 100644 --- a/plugins/github/src/org/jetbrains/plugins/github/authentication/accounts/GithubAccountInformationProvider.kt +++ b/plugins/github/src/org/jetbrains/plugins/github/authentication/accounts/GithubAccountInformationProvider.kt @@ -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().asMap() + private val imageCache = cacheBuilder.build().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 { + token: String): Pair { 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 { + fun getAccountInformationWithPicture(indicator: ProgressIndicator, account: GithubAccount): Pair { 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)